我正在编写一个脚本来检查逻辑驱动器是 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()