3

我正在编写一个脚本来检查逻辑驱动器是 USB 还是 iSCSI 目标。如果是忽略相关的驱动器号。

Get-WmiObject win32_logicaldisk -Filter "DriveType='3'" |
where-object{$_.DeviceID -ne $usbletters -and $_.DeviceID -ne $iSCSIletters} | %
{$_.Name} | out-file $kreports\avail.txt

我的问题是,当检测到相同类型的多个驱动器时,它们会被不等于选项简单地忽略。我假设我需要做某种形式的 foreach 循环?

如果有人能指出我正确的方向,那就太好了!

这是完整的脚本。

#Variables and Arguments
$kreports = "C:\Kworking\reports"


# Create kworking Reports folder
if (!(Test-Path -path $kreports))
{New-Item $kreports -type directory}


# USB Drive check
$diskdrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"}
$usbletters = $diskdrive | %{gwmi -Query "ASSOCIATORS OF 
{Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE

AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS

OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass =

Win32_LogicalDiskToPartition"} | %{$_.DeviceID}

# iSCSI Drive check
$iSCSIdrive = gwmi win32_diskdrive | ?{$_.model -match "iSCSI"}
$iSCSIletters = $iSCSIdrive | %{gwmi -Query "ASSOCIATORS OF 
{Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE

AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS

OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass =

Win32_LogicalDiskToPartition"} | %{$_.DeviceID}


# Disk Information
Get-WmiObject win32_logicaldisk -Filter "DriveType='3'" | where-object{$_.DeviceID -ne
$usbletters -and $_.DeviceID -ne $iSCSIletters} | %{$_.Name} | out-file
$kreports\avail.txt


# Fix Output Line Spacing
$InputFile = "$kreports\avail.txt"
$OutputFile = "$kreports\availdisks.txt"

$Writer = New-Object IO.StreamWriter "$OutputFile"
$Writer.Write( [String]::Join("`r`n", $(Get-Content $InputFile)) )
$Writer.Close()
4

1 回答 1

3

问题是Win32_LogicalDisk' 的成员DeviceID包含驱动器号和冒号。例如,由于值是,C:并且您针对 测试是否相等Cwhere-object所以 没有找到任何东西。

要么在驱动器号上包含冒号,要么使用 Powershell 方式的正则表达式。

$disks = gwmi win32_logicaldisk -Filter "DriveType='3'"
# Select all devices that do not have deviceids a,b,c,k or l, followed by colon
$avail = $disks | ? { $_.DeviceID -notmatch "[abckl]:"}
# Do something with filtered results
于 2012-11-21T06:51:15.620 回答