| Powershell 7.3复制代码$DontRecurse = $True
Get-ChildItem -Path . -Recurse:!$DontRecurse
Powershell 5.1复制代码Get-ChildItem: Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by parameter 'Recurse'.
Powershell Switch 类型的参数,值只能是 $True $False 0 1 之一,如果参数的值是一个变量,写法应该是 -<ParameterName>:$<VariableName>,比如,参数名是 Recurse,将 $Recurse 传递给 Recurse 参数,应该写成 -Recurse:$Recurse,如果写成 -Recurse $Recurse 无论 $Recurse 的值是什么参数 Recurse 得到的值都是 $True,如果参数的值需要经过运算,运算要写在()内,不能直接写表达式,也可以先进行运算,再将值赋给一个变量,再用这个变量传值。复制代码Get-ChildItem : 无法将“System.String”转换为参数“Recurse”所需的类型“System.Management.Automation.SwitchParameter”。
所在位置 行:1 字符: 32
+ Get-ChildItem -Path . -Recurse:!$DontRecurse
+                                ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem],ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.GetChildItemCommand
复制代码$DontRecurse = $True
Get-ChildItem -Path . -Recurse:(!$DontRecurse)
复制代码Get-ChildItem -Path . -Recurse:(3 -gt 5)
PowerShell Documentation: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters#switch-parameters复制代码$DontRecurse = $True
$Recurse = !$DontRecurse
Get-ChildItem -Path . -Recurse:$Recurse
 |