Home > Software engineering >  Powershell - InputObject error message when piping a get-disk variable
Powershell - InputObject error message when piping a get-disk variable

Time:12-04

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:

CodePudding user response:

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

CodePudding user response:

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.

  • Related