【问题标题】:Powershell - InputObject error message when piping a get-disk variablePowershell - 管道获取磁盘变量时的 InputObject 错误消息
【发布时间】:2022-01-09 17:40:40
【问题描述】:

我将 get-disk 放入一个变量中,当我想用​​ clear-disk 对其进行管道传输时,我收到 InputObject is null 错误消息。但是,如果我在 Powershell 控制台中单独运行这两行代码,它就可以工作。变量 $USB_Drive 在这两行之外的任何地方都没有使用。知道这可能是什么吗?

这里有两行:

$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

还有错误信息:

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:

【问题讨论】:

    标签: windows powershell


    【解决方案1】:

    您正在尝试将 out-gridview 的输出存储在您的变量中,这将更接近您所追求的:

    # 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
    

    【讨论】:

      【解决方案2】:

      感谢您的回答,但这是因为变量不是全局创建的。如果我创建变量 $global:USB_Drive Global 一切正常。

      【讨论】:

        猜你喜欢
        • 2020-08-10
        • 2019-04-07
        • 2014-07-17
        • 2016-12-10
        • 2019-02-10
        • 1970-01-01
        • 1970-01-01
        • 2012-08-16
        • 1970-01-01
        相关资源
        最近更新 更多