function Get-Power([int]$x, [int]$y) { if ($y -gt 0) { return $x * (Get-Power $x (--$y)) } else { return 1 } } Examples:PowerShell 複製 New-Object 'int[,]' 3,2 New-Object -ArgumentList 3,2 -TypeName 'int[,]' dir e:\PowerShell\Scripts\*statement*.ps1 | Forea...
可以使用return语句在退出函数的同时返回值,下例中的函数在集合中搜索对象:展开表 PS C:\> function Find-Object($target, $haystack) >> { >> foreach ($item in $haystack) >> { >> if($item -eq $target) >> { >> return $item >> } >> } >> } >> PS C:\> Find-Object 5 (2...
Using return statement with condition To return Boolean from function in PowerShell: Use the function to create a function with two integer parameters. Use the -eq operator to compare the two provided numbers and return the Boolean output value. Use the function by calling its name along with ...
function inc ([parameter(ValueFromPipeline)]$x) {return $x + 1} Write-Host (3 | inc) #输出为:4 五、使用引用 函数参数可使用引用类型,使用引用类型之后便可以在函数中修改外部变量的数值。 在参数前使用[ref]指定使用引用类型。如function f ([ref]$x)。传参时,要求把传入数值转换为引用类型,转换...
# be added or removed from TrustedHosts [parameter(mandatory)] [string] $Ensure, # Credentials needd to manage WinRM TrustedHosts configuration [parameter(mandatory)] [PSCredential] $Credential ) # Boolean return variable from this function
A function can also be as complex as a cmdlet or an application. Like cmdlets, functions can have parameters. The parameters can be named, positional, switch, or dynamic parameters. Function parameters can be read from the command line or from the pipeline. Functions can return values that ca...
$a="Hello"functionfoo {"`$a =$a""`$global:a =$global:a"} 現在,我們會建立全域變數$a,併為其指定值並呼叫函式foo。 PowerShell $a="Goodbye"foo 模組會在模組範圍中宣告變數$a,然後函式 foo會輸出這兩個範圍中的變數值。 Output $a = Hello $global:a = Goodbye ...
NAME New-Module SYNOPSIS Creates a new dynamic module that exists only in memory. SYNTAX New-Module [-Name] <String> [-ScriptBlock] <ScriptBlock> [-ArgumentList <Object[]>] [-AsCustomObject] [-Cmdlet <String[]>] [-Function <String[]>] [-ReturnResult] [<CommonParameters>] DESCRIPTION ...
‘No GPO named $gpoName found’; return} {$_ -gt 1} {throw ‘More than one GPO named $gpoName found’; return} } if ($migrationTable) { $gpmMigrationTable = $gpm.GetMigrationTable($migrationTable) } $gpmTargetDomain = $gpm.GetDomain($targetDomain, “”, $gpmConstants.UseAnyDC) ...
‘No GPO named $gpoName found’; return} {$_ -gt 1} {throw ‘More than one GPO named $gpoName found’; return} } if ($migrationTable) { $gpmMigrationTable = $gpm.GetMigrationTable($migrationTable) } $gpmTargetDomain = $gpm.GetDomain($targetDomain, “”, $gpmConstants.UseAnyDC) ...