function Test-MrParameterValidation { [CmdletBinding()] param ( [Parameter(Mandatory)] [string]$ComputerName ) Write-Output $ComputerName } Now that the ComputerName is required, if one isn't specified, the function prompts for one. PowerShell Copy Test-MrParameterValidation Output Copy cm...
function Add-Extension { param ([string]$Name, [string]$Extension = "txt") $Name = $Name + "." + $Extension $Name # .EXTERNALHELP C:\ps-test\Add-Extension.xml } 示例5:重定向到其他帮助主题以下代码是 PowerShell 中内置 help 函数开头的摘录,该函数一次显示一个帮助文本屏幕。 由于 Get-He...
FunctionTest-ScriptCmdlet{ [CmdletBinding(SupportsShouldProcess=$True)]Param($Parameter1)begin{}process{}end{} } 备注 这些块适用于所有函数,而不仅仅是使用CmdletBinding属性的函数。 begin 此块用于为函数提供可选的一次性预处理。 PowerShell 运行时会为管道中函数的每个实例使用此块中的代码一次。
param($ComputerName= $(throw"ComputerName parameter is required."))functionCanPing {$Error.Clear()$tmp=Test-Connection$ComputerName-ErrorActionSilentlyContinueif(!$?) {Write-Host"Ping failed:$ComputerName.";return$false}else{Write-Host"Ping succeeded:$ComputerName";return$true} }functionCanRemote...
function Send-Greeting { [CmdletBinding()] Param( [Parameter(Mandatory=$true)] [string] $Name ) Process { Write-Host ("Hello " + $Name + "!") } } 函数的参数是使用 特性声明的Parameter。 此属性可以单独使用,也可以与 Alias 属性或多个其他参数验证属性结合使用。 有关如何声明参数 (...
functionGet-ComObject { <#.Synopsis Returns a list of ComObjects .DESCRIPTION Thisfunctionhas two parameter sets, it can eitherreturnall ComObject or a sub-section by thefilterparameter. This information is gathered from the HKLM:\Software\Classes container. ...
+ FullyQualifiedErrorId : Date required可以在定义函数时跳过参数声明,而在函数体中声明。函数体本身以脚本块的形式存在,可以包含param语句。下例中的Format-Date函数在脚本块中声明变量:展开表 PS C:\PowerShell> function Format-Date >> { >> param ($date = $(throw "Date required"), $format = "...
param($ComputerName= $(throw"ComputerName parameter is required."))functionCanPing {$Error.Clear()$tmp=Test-Connection$ComputerName-ErrorActionSilentlyContinueif(!$?) {Write-Host"Ping failed:$ComputerName.";return$false}else{Write-Host"Ping succeeded:$ComputerName";return$true} }functionCanRemote...
Required:False Accept pipeline input:False Accept wildcard characters:False -ExcelAutomaticVolatileFunctionCacheLifeTime Specifies the maximum time, in seconds, that a computed value for a volatile function is cached for automatic recalculations. -1: Calculates once when the workbook loads. 0: Always...
function Get-MyAcl { param($Path) Invoke-Expression “Get-Acl $Path“ } If $Path contains input such as “; Write-Host Pwnd”, the attacker can now execute the Write-Host cmdlet (or much worse!) as well. The Invoke-Expression cmdlet should almost always be avoided, as PowerShell (...