我正在运行一个脚本来查询服务器的驱动器以带回磁盘空间结果。该脚本使用此问题的服务器文本文件列表运行,我在 txt 文件中只有一个服务器名称,并最终构建一个 html 文档。
出现问题是因为当我使用此脚本的第三个示例时 - 它不会为服务器带回正确数量的驱动器。所以为了测试这一点,我做了以下事情:
测试 1。
Get-WmiObject
-ComputerName DB-server01
-Class Win32_LogicalDisk
-Filter "DriveType = 3"
结果:
DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 28575797248
Size : 146056146944
VolumeName : OS
DeviceID : V:
DriveType : 3
ProviderName :
FreeSpace : 814725959680
Size : 898316103680
VolumeName : SQLData
DeviceID : W:
DriveType : 3
ProviderName :
FreeSpace : 293852868608
Size : 299436601344
VolumeName : SQLLogs
测试 2。
$computers = Get-Content "C:\Powershell\servers.txt";
write-host $computers
foreach($computer in $computers)
{
$disks = Get-WmiObject
-ComputerName $computer
-Class Win32_LogicalDisk
-Filter "DriveType = 3"
write-host $disks
}
结果
DB-server01
\DB-server01\root\cimv2:Win32_LogicalDisk.DeviceID="C:" \db-server01\root\cimv2:Win32_LogicalDisk.DeviceID="V:" \db-server01\root\cimv2:Win32_LogicalDisk.DeviceID="W: "
测试 3。
$i = 0;
$percentWarning = 90;
$percentCritcal = 25;
$computers = Get-Content "C:\Powershell\servers.txt";
write-host $computers
foreach($computer in $computers)
{
$disks = Get-WmiObject
-ComputerName $computer
-Class Win32_LogicalDisk
-Filter "DriveType = 3"
$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;
if($percentFree -lt $percentWarning)
{
$color = $greenColor
if($percentFree -lt $percentCritcal)
{
$color = $redColor
}
if ($percentFree -eq "NaN")
{
$PercentFree = "N/A"
$Color = $whiteColor
}
Add-Content $diskReport $dataRow;
Write-Host -ForegroundColor Green
"$computer $deviceID percentage free space = $percentFree";
$i++
}
}
}
结果:
DB-Server01 C: percentage free space = 19.56
当通过外观测试 3 只带回驱动器 c 并错过了 V&W。
??????