我正在检查我的服务器磁盘空间使用情况并生成一个文本文件作为报告。 我想按照FreePercentage Free%的升序对输出文件进行排序。
我被困在这个阶段。它只是没有排序。 这是一个定界问题吗?我的错误在哪里?
# files to be used
$FileName0 = "C:\MONITORING\WINDOWS-SPACE\SCRIPT\SPACE0.txt"
$FileName1 = "C:\MONITORING\WINDOWS-SPACE\SCRIPT\SPACE1.txt"
$FileName2 = "C:\MONITORING\WINDOWS-SPACE\SCRIPT\SPACE2.txt"
# file containing servers list
#$ServerName = Get-Content "C:\MONITORING\WINDOWS-SPACE\SCRIPT\COMPUTERS.txt"
# use 2 servers only for coding test, will be replaced by a file containing all
# servers
$ServerName = "SERVER1","SERVER2"
# loop and process drive-id and calculations and populate filename-0
foreach ($_ in $ServerName) {
Get-WmiObject -Query "Select * from win32_logicaldisk" -ComputerName $_ |
Select PSComputername, DeviceID,
@{Name="TotalGB"; Expression={"{0:N0}" -F ($_.Size/1GB)}},
@{Name="UsedGB" ; Expression={"{0:N0}" -F (($_.Size/1GB)-($_.Freespace/1GB))}},
@{Name="FreeGB" ; Expression={"{0:N0}" -F ($_.Freespace/1GB)}},
@{Name="Free%" ; Expression={"{0:N0}" -F (($_.Freespace/$_.Size)*100)}} |
Format-Table -AutoSize >> $FileName0
}
# remove unwanted drives ('A'&'Z') and duplicate lines and empty lines and
# populate filename-1
Get-Content $FileName0 | Select-Object -Unique |
Where {$_ -notmatch "A:"} |
Where {$_ -notmatch "Z:"} |
Where { $_ -notmatch "^-" } |
Where {$_ -ne ""} |
Format-Table -AutoSize > $FileName1
# input final result into a CSV file filename-2
Import-Csv $FileName1 -Header PSComputername, TotalGB, UsedGB, FreeGB, Free% |
sort [int]Free% |
Format-Table -AutoSize >> $FileName2
实际输出文件:
PSComputerName DeviceID TotalGB UsedGB FreeGB Free% SERVER1 C: 60 33 27 45 SERVER1 D: 20 2 18 88 SERVER2 C: 40 27 13 33 SERVER2 D: 50 6 44 87
所需的输出文件:
PSComputerName DeviceID TotalGB UsedGB FreeGB Free% SERVER2 C: 40 27 13 33 SERVER1 C: 60 33 27 45 SERVER2 D: 50 6 44 87 SERVER1 D: 20 2 18 88
答案 0 :(得分:2)
Get-WmiObject
接受计算机名称列表,因此您不需要循环。但是,您的主要问题是,您将WMI结果通过Format-Table
传输,将其写入文件,然后尝试使用Import-Csv
再次读取它,此时您的中间文件的格式不是{\ n}。 t CSV。 1}} 之后立即对数据进行排序将其写入文件。
Get-WmiObject
如果您想要实际的CSV输出,请使用Get-WmiObject -Query 'SELECT * FROM Win32_LogicalDisk' -ComputerName $ServerName |
Select PSComputername, DeviceID,
@{Name="TotalGB"; Expression={"{0:N0}" -f ($_.Size/1GB)}},
@{Name="UsedGB" ; Expression={"{0:N0}" -f (($_.Size/1GB)-($_.Freespace/1GB))}},
@{Name="FreeGB" ; Expression={"{0:N0}" -f ($_.Freespace/1GB)}},
@{Name="Free%" ; Expression={"{0:N0}" -f (($_.Freespace/$_.Size)*100)}} |
Sort-Object {[int]$_.'Free%'} |
Format-Table -AutoSize |
Set-Content $FileName2
代替Export-Csv
:
Format-Table | Set-Content
答案 1 :(得分:1)
这是一个较短的方式来做你要求的事情:
$computerList = "server1","server2"
$computerList | ForEach-Object {
$computerName = $_
$params = @{
"Class" = "Win32_LogicalDisk"
"ComputerName" = $computerName
"Filter" = "DriveType=3"
}
Get-WmiObject @params | Select-Object `
@{Name = "ComputerName"; Expression = {$_.__SERVER}},
@{Name = "Drive"; Expression = {$_.DeviceID}},
@{Name = "TotalGB"; Expression = {[Int] ("{0:N0}" -f ($_.Size / 1gb) )}},
@{Name = "UsedGB"; Expression = {[Int] ("{0:N0}" -f (($_.Size / 1gb) - ($_.FreeSpace / 1gb)))}},
@{Name = "FreeGB"; Expression = {[Int] ("{0:N0}" -f ($_.FreeSpace / 1gb) )}},
@{Name = "FreePercent"; Expression = {[Int] ("{0:N0}" -f (($_.FreeSpace / $_.Size) * 100) )}}
} | Sort-Object FreePercent
您遇到的问题是在创建输出之前尝试使用格式和排序。在尝试格式化或排序之前,请确保输出所有内容。