Example 4 - Enumeration as a parameter In the following example, the function ConvertTo-LineEndingRegex defines the InputObject parameter with the type EndOfLine. PowerShell Copy enum EndOfLine { CR = 1 LF = 2 CRLF = 3 } function ConvertTo-LineEndingRegex { [CmdletBinding()] param ( ...
PowerShell复制 $Env:Foo='An example' 由于环境变量始终是字符串,因此可以像使用包含字符串的任何其他变量一样使用它们。 例如: PowerShell复制 "The 'Foo' environment variable is set to:$Env:Foo"$Env:Foo+='!'$Env:Foo Output复制 The 'Foo' environment variable is set to: An example An example!
Optional parameters have a default value, which is the value that is used or assumed when the parameter is not specified in the command. For example, the default value of theComputerNameparameter of many cmdlets is the name of the local computer. As a result, the local computer name is us...
FunctionTest-ScriptCmdlet{ [CmdletBinding(SupportsShouldProcess=$True)]Param($Parameter1)begin{}process{}end{} } 备注 这些块适用于所有函数,而不仅仅是使用CmdletBinding属性的函数。 begin 此块用于为函数提供可选的一次性预处理。 PowerShell 运行时会为管道中函数的每个实例使用此块中的代码一次。
Calls the static properties and methods of a .NET class. To find the static properties and methods of an object, use the Static parameter of theGet-Membercmdlet. The member name may be an expression. PowerShell [datetime]::Now'MinValue','MaxValue'|ForEach-Object{ [int]::$_} ...
This command starts an interactive session with the Server01 computer. It uses thePortparameter to specify the port and theCredentialparameter to specify the account of a user who has permission to connect to the remote computer. Example 5: Stop an interactive session ...
The Identity parameter specifies the remote domain that you want to modify. You can use any value that uniquely identifies the remote domain. For example: Name Distinguished name (DN) GUID Type:RemoteDomainIdParameter Position:1 Default value:None ...
Most other cmdlets (for example, New-* and Set-* cmdlets) don't have a built-in pause. For these cmdlets, specifying the Confirm switch without a value introduces a pause that forces you acknowledge the command before proceeding. Type:SwitchParameter ...
Adding enums The advantage of using an enum with your Windows PowerShell 5.0 class is that you have automatic parameter validation in Windows PowerShell. This is essential for solving the old-fashioned problem of the “garbage in garbage out” maxim of data processing. When applied as a type...
Methods support parameters, parameter validation, and can also have the same name as long as their parameters differ: [string] ToString() { ... } [string] ToString([int] $MaxLength) { ... } Custom Enumerations To define a custom enumeration, use the enum keyword: enum MyColor { Red ...