简体   繁体   中英

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. But if I run the two lines of code individually in the Powershell console it works. the variable $USB_Drive is not used anywhere else than in these two lines. 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:

# 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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