PowerShell,调用带有参数但不带值的脚本,就像带有选项的脚本一样

PowerShell,调用带有参数但不带值的脚本,就像带有选项的脚本一样,powershell,Powershell,是否可以使用选项调用PowerShell脚本?就像一个没有值的参数 例如,我目前使用: param ( $lastmonth=0 ); 在剧本里。现在我可以用这个电话了 script.ps1 -lastmonth 1 或 并使用$lastmonth来控制流量 但是我想打电话给你 script.ps1 -lastmonth 及 并根据是否给出-lastmount来控制流量。将参数类型设置为[switch],例如 param ( [switch]$lastmonth ); 编

是否可以使用选项调用PowerShell脚本?就像一个没有值的参数

例如,我目前使用:

param (
    $lastmonth=0
);
在剧本里。现在我可以用这个电话了

script.ps1 -lastmonth 1

并使用$lastmonth来控制流量

但是我想打电话给你

script.ps1 -lastmonth


并根据是否给出-lastmount来控制流量。

将参数类型设置为
[switch]
,例如

param (
    [switch]$lastmonth
);
编辑: 请注意,该变量将是一个布尔值。您可以通过以下方式进行测试:

if ($lastMonth) {
    Write-Host "lastMonth is set."
}
else {
    Write-Host "lastMonth is not set."
}

(谢谢Chris)

这称为开关参数。签出。

。。。然后它是一个布尔变量。@Chris Lol,是的,应该提到这一点!我将更新我的答案:)
param (
    [switch]$lastmonth
);
if ($lastMonth) {
    Write-Host "lastMonth is set."
}
else {
    Write-Host "lastMonth is not set."
}