找回密码
 新建账号

Powershell Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter'

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

手机版|轻松E站

GMT+8, 2024/4/28 03:48

快速回复 返回顶部 返回列表