我想将字母从D更改为Z:
Set-WmiInstance -InputObject ( Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" ) -Arguments @{DriveLetter='Z:'}
错误:
Set-WmiInstance : Cannot bind argument to parameter 'InputObject' because it is null. At line:1 char:30 + Set-WmiInstance -InputObject ( Get-WmiObject -Class Win32_volume -Filter "DriveL ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Set-WmiInstance], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetWmiInstance
答案 0 :(得分:4)
发生该错误是因为WMI查询没有返回任何内容(可能是因为D:
没有安装任何设备)。
为避免这种情况,请改用管道:
Get-WmiObject -Class Win32_volume -Filter "DriveLetter = 'd:'" |Set-WmiInstance -Arguments @{DriveLetter='Z:'}
如果Get-WmiObject
没有返回任何内容,那么Set-WmiInstance
将无法运行并且您不会遇到任何错误
答案 1 :(得分:0)
显然嵌套的Get-WmiObject
调用不会返回结果。可能有很多原因:
SilentlyContinue
)一般来说,更好的方法是不依赖于驱动器号,但是drive type和-matias在his answer中指出的 - 使用管道而不是嵌套命令(以便空结果)被跳过而不是抛出错误。)
Get-WmiObject -Class Win32_volume -Filter 'DriveType=5' |
Select-Object -First 1 |
Set-WmiInstance -Arguments @{DriveLetter='Z:'}