当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 命令行参数

作者:精品下载站 日期:2024-12-14 22:06:55 浏览:12 分类:玩电脑

PowerShell 命令行参数


PowerShell 是一种多功能脚本语言,为处理命令行参数提供广泛支持,包括命名参数和位置参数,以及开关、数组和 $Args 等特殊参数类型。

在本文中,我们将讨论如何使用 PowerShell 命令行参数。

PowerShell 命令语法基础知识

PowerShell 支持命名参数和位置参数。命名参数通过其特定名称进行标识,位置参数通过其顺序进行标识。

以下示例说明了在 PowerShell 脚本中定义这两种类型:

# Create scrip1.ps1 file having parameters of different data types
param(
    [string]$Subject,
    [int]$Marks
)
Write-Host "Tom scored $Marks in subject $Subject."

在上面的示例中,$Subject 和 $Marks 是命名参数。让我们在运行脚本时使用它们:

D:\PS\script1.ps1 -Subject "Physics" -Marks 74

将参数传递给 PowerShell 脚本

让我们借助示例来了解在运行脚本文件时使用命名参数和位置参数。

使用命名参数

命名参数使得在脚本中使用它们时更容易阅读和理解。要在 PowerShell 脚本中使用命名参数,请显式提供它们:

D:\PS\script1.ps1 -Subject "Physics" -Marks 74

在上面的PowerShell脚本中,运行脚本文件时,需要Subject和Marks两个参数。我们在脚本中明确提供了这些参数及其值。

在脚本中使用命名参数时,参数顺序并不重要,提供正确的参数名称很重要。

上述 PowerShell 脚本的输出是:

[玩转系统] PowerShell 命令行参数

使用位置参数

PowerShell 脚本中的位置参数允许您在不指定参数名称的情况下传递值,您必须按照脚本中定义参数的顺序提供值。

D:\PS\script1.ps1 "Physics" 74

在上面的 PowerShell 脚本中,我们按照脚本文件中定义的顺序提供了参数值。

切换参数命令行参数

当您的脚本中需要on/off开关时,使用Switch参数。它们不需要值,但要么是 true 要么是 false。

# Create script1.ps1 file having switch parameter
param(
    [string]$Subject,
    [int]$Marks,
    [switch]$Verbose
)
Write-Host "Tom scored $Marks in subject $Subject"
Write-Host "The True/False switch argument is $Verbose"

要使用 switch 参数,请在调用脚本时使用它:

# Without switch parameter, it print the value as False
D:\PS\script1.ps1 -Subject "Physics" -Marks 74 

# Include switch parameter when calling a script

D:\PS\script1.ps1 -Subject "Physics" -Marks 74 -Verbose

在上面的 PowerShell 脚本中,如果命令行参数中包含 switch 参数,它将打印 True,否则显示输出 False。

上述 PowerShell 脚本的输出是:

[玩转系统] PowerShell 命令行参数

命令行参数中的数组参数

数组参数允许您将数组(多个值)传递给命令行参数中的单个参数。

# Create script1.ps1 file having Subjects parameter of type string[]
param(
    [string[]]$Subjects
)
Write-Host "Subjects passed as an array are: $Subjects"

要在命令行参数中使用数组参数,请使用逗号分隔的列表将多个值传递给 $Subjects 参数。

D:\PS\script1.ps1 -Subjects "Maths","Physics","English"

上述 PowerShell 脚本的输出是:

PS C:\> D:\PS\script1.ps1 -Subjects "Maths","Physics","English"                                                         Subjects passed as array: Maths Physics English

PS C:\>                                                                                                                               

命令行参数中的验证属性

PowerShell 脚本中的验证属性允许您对参数实施特定限制。

# Create a script1.ps1 file having validation range defined for parameter.
param(
    [ValidateRange(1, 100)][int]$Marks
)
Write-Host "$Marks is between 1 to 100" -ErrorAction SilentlyContinue

在上面的 PowerShell 脚本中,$Marks 参数必须介于 1 到 100 之间。

传递给定范围内和范围外的参数值。

# Pass the Marks value in between 1 to 100
 D:\PS\script1.ps1 -Marks 25

# Pass the Marks value greater than 100
 D:\PS\script1.ps1 -Marks 101

在上面的PowerShell脚本中,-Marks参数值在1到100之间将显示结果如下:

PS C:\> D:\PS\script1.ps1 -Marks 25                                                                                     25 is between 1 to 100

-Marks 参数值大于 100 将显示异常,因为提供的值超出了验证范围。

PS C:\> D:\PS\script1.ps1 -Marks 101                                                                                    D:\PS\script1.ps1 : Cannot validate argument on parameter 'Marks'. The 101 argument is greater than the maximum
allowed range of 100. Supply an argument that is less than or equal to 100 and then try the command again.
At line:1 char:26
+ D:\PS\script1.ps1 -Marks 101
+                          ~~~
    + CategoryInfo          : InvalidData: (:) [script1.ps1], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,script1.ps1

在命令行参数中使用 $Args 自动变量

$Args 是一个自动变量,其中包含传递给脚本的所有参数的数组。

要使用 $Args 变量访问和操作脚本的参数,请使用以下脚本:

foreach ($arg in $Args) {
    Write-Host $arg
}

使用 CMD 中的参数运行 PowerShell 脚本

要使用 cmd 中的参数运行 PowerShell 脚本,请使用以下命令:

powershell -ExecutionPolicy Bypass -File "D:\PS\script1.ps1" -Marks 74 -Subject "Physics" -Verbose

上述命令的输出显示结果如下:

C:\>powershell -ExecutionPolicy Bypass -File "D:\PS\script1.ps1" -Marks 74 -Subject "Physics" -Verbose

Tom scored 74 in subject Physics
The True/False switch argument is True

您可以了解有关如何使用不同方法从 cmd 运行 PowerShell 脚本的更多信息。

结论

我希望上述有关如何使用 PowerShell 命令行参数的文章对您有所帮助。我们深入讨论了命令行参数主题,包括命名参数、位置参数、开关、数组参数和用于访问传递给脚本的参数的 $Args。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯