天天看点

[Windows PowerShell09]-PowerShell 函数

       PowerShell 函数跟普通函数非常类似,同样支持无参数、一个参数、多个参数传入,无返回值和返回一个值的情形。除此之外,PowerShell支持函数默认值,这样可以模仿.net的函数重载的情形。PowerShell函数参数的重载除了使用([Type] [Value1],[Type] [Value2]...[Type] [Valuen])的形式之外,可以使用param([Type] [Value1],[Type] [Value2]...[Type] [Valuen])的形式传递。PowerShell除了可以给函数传递参数之外,还可以给脚本块(脚本文件传递参数),注:PowerShell支持脚本文件的互相调用,在调用的时候支持给脚本文件传递参数。

 1)函数调用函数,函数参数传递

#Region Parameter
$LogPath = "E:\PowerShellScript\Log\Log.txt"
#EndRegion

#Region functions

# Auth:Justin
# Date:2014-10-3
# Desc:Get current date with string format yy/mm/dd hh:mm:ss
function GetCurrentTime()
{
	return (Get-Date).ToString()
}

# Auth:Justin
# Date:2014-10-3
# Desc:Write log with format([Time]|[Type]|[Message]) to .txt file.
function WriteLog($Msg,$Type)
{
	$CurrentDate =  GetCurrentTime
	
	if(Test-Path $LogPath)
	{
		$CurrentDate+"|"+$Type+"|"+$Msg >> $LogPath
	}
	else
	{
		$CurrentDate+"|"+$Type+"|"+$Msg > $LogPath
	}
}
#EndRegion
           

       这个例子演示了函数的参数传递形式,函数的调用,写文件的方法(> 和 >>),Region的使用,函数返回值的使用及字符串操作等几个小知识点,虽然函数功能很简单,但麻雀虽小五脏俱全。函数可以使用Param的形式进行传递。注:下面两个函数式解压缩zip文件的

# Auth:Justin
# Date:2014-10-3
# Desc:To extract zip file to specific location
function ExtractZip
{
	param([string]$zipfilename, [string] $destination)

	if(Test-Path $zipfilename)
	{	
		$shellApplication = new-object -com shell.application
		$zipPackage = $shellApplication.NameSpace($zipfilename)
		$destinationFolder = $shellApplication.NameSpace($destination)
		$destinationFolder.CopyHere($zipPackage.Items())
	}
}

# Auth:Justin
# Date:2014-10-3
# Desc:To add zip file to specific location
function AddZip
{
	param([string]$sourcefiles, [string]$zipfilename)
    
	dir $sourcefiles | foreach-object {
	 if(!$zipfilename) {
		$zipfile = $_.FullName + ".zip";
	 }
	else {
		$zipfiles = $zipfilename;
	}
        
	if(!(test-path($zipfile))) {
		set-content $zipfile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18));
		(dir $zipfile).IsReadOnly = $false;
    	}
        
	$shellApplication = new-object -com shell.application;
	$zipPackage = $shellApplication.NameSpace($zipfile);
	$zipPackage.CopyHere(($_.FullName));
	}
}
           

这两个函数演示了param的使用,调用的时候可以直接传入一个参数,也可以传入两个参数。

ExtractZip "E:\PowerShellScript\Log\Log.zip" "E:\PowerShellScript\Log\"
AddZip "E:\PowerShellScript\Log\Log.txt"
           

PowerShell 函数还支持默认值。

function GetComputerInfo($ComputerName = "localhost")
{
	Get-WmiObject -Class win32_bios -ComputerName $ComputerName
}

#Call function
GetComputerInfo
GetComputerInfo "Justin-pc"
           

运行结果:

SMBIOSBIOSVersion : HEET42WW (1.23 )
Manufacturer      : LENOVO
Name              : Phoenix BIOS SC-T v2.2
SerialNumber      : PF1DY83
Version           : ACRSYS - 1230

SMBIOSBIOSVersion : HEET42WW (1.23 )
Manufacturer      : LENOVO
Name              : Phoenix BIOS SC-T v2.2
SerialNumber      : PF1DY83
Version           : ACRSYS - 1230
           

PowerShell 支持给脚本文件传值。

TestFileParam.ps1:

param
(
    [string]$UserName
)

function PrintUserName()
{
    Write-Host "Hello "$UserName
}

PrintUserName $UserName
           

调用过程如下:

[Windows PowerShell09]-PowerShell 函数

这样PowerShell就可以做很多流程处理的事情,工作流由于耦合性太高,通常使用PowerShell来替代工作流。

继续阅读