【问题标题】:Powershell - Exclude P: Drive from foreach loopPowershell - 排除 P:从 foreach 循环中驱动
【发布时间】:2016-08-29 15:27:18
【问题描述】:

我有一个脚本,它通过服务器的文本文件并报告低于 25% 和 10% 的可用磁盘空间。但是有些服务器有一个 P: 驱动器,它用于分页文件,因此我希望循环忽略这个驱动器。

这是循环部分:

# Start processing disk space reports against a list of servers 
  foreach($computer in $computers) 
 {  
 $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3" -EA SilentlyContinue
 $computer = $computer.toupper() 
  foreach($disk in $disks) 
 {         
  $deviceID = $disk.DeviceID; 
        $volName = $disk.VolumeName; 
  [float]$size = $disk.Size; 
  [float]$freespace = $disk.FreeSpace;  
  $percentFree = [Math]::Round(($freespace / $size) * 100, 2); 
  $sizeGB = [Math]::Round($size / 1073741824, 2); 
  $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2); 
        $usedSpaceGB = $sizeGB - $freeSpaceGB; 
        $color = $whiteColor;

我在 powershell 方面处于非常基础的水平,因此对于语法以及如何排除 P: 驱动器的任何帮助将不胜感激

【问题讨论】:

    标签: powershell foreach diskspace


    【解决方案1】:

    向 WMI 过滤器添加条件以排除带有 DeviceID = P: 的卷:

    $disks = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType = 3 AND DeviceID != 'P:'" -ComputerName $computer -EA SilentlyContinue
    

    或者,使用Where-Object 过滤$disks 集合:

    foreach($disk in $disks |Where-Object {$_.DeviceID -ne 'P:'}) 
    {
        # ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 2017-03-28
      • 2022-01-08
      • 1970-01-01
      • 2019-09-19
      • 2020-06-06
      • 2016-03-13
      相关资源
      最近更新 更多