【问题标题】:Save powershell get-disk as variable and the use it in if function将 powershell get-disk 保存为变量并在 if 函数中使用它
【发布时间】:2022-11-18 16:42:37
【问题描述】:

我想让脚本只从特定的 USB 驱动器运行。我想制作具有 get-disk 命令输出的变量 $drive,如果 $drive 包含我的 usb 串行 - 制作一些功能。

$drive=get-disk -SerialNumber "980D06056030" |英尺序列号

if ( "980D06056030" -eq $drive ) {Write-Host "Yes"}

这行不通 =(

【问题讨论】:

    标签: powershell


    【解决方案1】:

    使用 Where-Object 过滤:

    Get-Disk | Where-Object -FilterScript {$_.Serialnumber -Eq "980D06056030"}
    

    注意:首先使用Get-Disk并确认序列号显示在USB属性的屏幕上。某些 USB 设备不显示序列号。 您还可以使用 where-object 来过滤 USB 设备,例如:

    $USBSerialnum = "980D06056030"
    Get-Disk | Where-Object -FilterScript {($_.Bustype -Eq "USB") -and ($_.Serialnumber -Eq $USBSerialnum) }
    

    【讨论】:

    • 它适用于输出“获取磁盘”功能,以及我的。第二部分的问题,我尝试从变量中获取序列号,如果包含我的序列号,则创建一些函数
    • @Kirill - 如果您接受,请将其标记为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 2019-08-17
    • 2013-10-22
    • 2021-11-18
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多