简体   繁体   English

Powershell - 管道获取磁盘变量时出现 InputObject 错误消息

[英]Powershell - InputObject error message when piping a get-disk variable

I put get-disk into a variable and when I want to pipe it with clear-disk I get an InputObject is null error message.我将 get-disk 放入一个变量中,当我想 pipe 使用 clear-disk 时,我得到一个 InputObject is null 错误消息。 But if I run the two lines of code individually in the Powershell console it works.但是,如果我在 Powershell 控制台中单独运行这两行代码,它就可以工作。 the variable $USB_Drive is not used anywhere else than in these two lines.变量 $USB_Drive 在这两行之外的任何地方都没有使用。 Any idea what this could be?知道这可能是什么吗?

Here the two lines:这里有两行:

$USB_Drive = Get-Disk | Where-Object BusType -eq USB | Out-GridView -Title 'Select USB Drive' -OutputMode Single

$Results = $USB_Drive | Clear-Disk -RemoveData -RemoveOEM -Confirm:$false -Passthru | New-partition -UseMaximumSize -IsActive -AssignDriveLetter | Format-Volume -FileSystem NTFS

And the Error Message:和错误信息:

ERROR: Clear-Disk : Das Argument für den Parameter "InputObject" kann nicht überprüft werden. Das Argument ist NULL. Geben Sie einen gültigen Wert für das Argument
ERROR: an, und führen Sie den Befehl erneut aus.
ERROR: In C:\Users\jorisbieg\Documents\SAPIEN\PowerShell Studio\Projects\ISODRIVE\ISODRIVE.Run.ps1:196 Zeichen:17
ERROR: + ... USB_Drive | Clear-Disk -RemoveData -RemoveOEM -Confirm:$false -Passth ...
ERROR: +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidData: (:) [Clear-Disk], ParameterBindingValidationException
ERROR:     + FullyQualifiedErrorId : ParameterArgumentValidationError,Clear-Disk
ERROR:

You are trying to store the output of out-gridview in your variable, this will be closer to what you are after:您正在尝试将 out-gridview 的 output 存储在您的变量中,这将更接近您所追求的:

# Store the usb drive
$USB_Drive = Get-Disk | Where-Object BusType -eq USB 
# display the usb drive
$USB_Drive | Out-GridView -Title 'Select USB Drive' -OutputMode Single

$Results = $USB_Drive | Clear-Disk -RemoveData -RemoveOEM -Confirm:$false -Passthru | New-partition -UseMaximumSize -IsActive -AssignDriveLetter | Format-Volume -FileSystem NTFS

Thanks for the answer, however it was because the variable was not created globally.感谢您的回答,但这是因为该变量不是全局创建的。 If I create the variable $global:USB_Drive Global everything works fine.如果我创建变量 $global:USB_Drive Global 一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM