使用Powershell汇总列

时间:2015-03-30 20:29:30

标签: powershell

我编写了以下PowerShell脚本,用于获取环境中服务器的磁盘空间信息。

$servers = Get-Content E:\POC.txt
$array = @()
foreach($server in $servers){
    $sysinfo =  Get-WmiObject Win32_Volume -ComputerName $server
    for($i = 0;$i -lt $sysinfo.Count; $i++){
        $sname =  $sysinfo[$i].SystemName
        $servername = $server
        $label = $sysinfo[$i].Label
        if(($label) -and (!($label.Contains("FILLER")))){
            write-host "Processing $label from $server"
            $name = $sysinfo[$i].Name
            $capacity = [math]::round(($sysinfo[$i].Capacity/1GB),2)
            $fspace = [math]::round(($sysinfo[$i].FreeSpace/1GB),2)
            $sused = [math]::round((($sysinfo[$i].Capacity - $sysinfo[$i].FreeSpace)/1GB),2)
            $fspacepercent =  [math]::Round((($sysinfo[$i].FreeSpace*100)/$sysinfo[$i].Capacity),2)
            $obj = New-Object PSObject
            $obj | Add-Member -MemberType NoteProperty -Name "SystemName" -Value $sname
            $obj | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $server
            $obj | Add-Member -MemberType NoteProperty -Name "Label" -Value $label
            $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $name
            $obj | Add-Member -MemberType NoteProperty -Name "Capacity(GB)" -Value $capacity
            $obj | Add-Member -MemberType NoteProperty -Name "FreeSpace(GB)" -Value $fspace
            $obj | Add-Member -MemberType NoteProperty -Name "Used(GB)" -Value $sused
            $obj | Add-Member -MemberType NoteProperty -Name "FreeSpace%" -Value $fspacepercent
            $array += $obj
        }
    }
    $array += write-output " "
    $totalSize = ($array | Measure-Object 'Capacity(GB)' -Sum).Sum
    $array += $totalsize
    $array += write-output " "
}
$filename = "E:\VolumeReport.csv"
$array |  Export-CSV $filename -NoTypeInformation

此处的另一个要求是获取每个服务器的容量,大小和自由空间列的总和。我尝试使用Measure-Object但没有成功。 这里没有输出任何值。只是空白。请仔细研究并提供帮助。

1 个答案:

答案 0 :(得分:1)

我们试试这个尺寸。

$servers = Get-Content E:\POC.txt
$propertyOrdered = "SystemName","ServerName","Label","Name","Capacity(GB)","FreeSpace(GB)","Used(GB)","FreeSpace%"
$filename = "C:\temp\VolumeReport.csv"
('"{0}"' -f ($propertyOrdered -join '","')) | Set-Content $filename

foreach($server in $servers){
    $sysinfo =  Get-WmiObject Win32_Volume -ComputerName $server
    $serverDetails = @()

    for($i = 0;$i -lt $sysinfo.Count; $i++){
        $sname =  $sysinfo[$i].SystemName
        $servername = $server
        $label = $sysinfo[$i].Label
        if(($label) -and (!($label.Contains("FILLER")))){
            write-host "Processing $label from $server"
            $name = $sysinfo[$i].Name
            $capacity = [math]::round(($sysinfo[$i].Capacity/1GB),2)
            $fspace = [math]::round(($sysinfo[$i].FreeSpace/1GB),2)
            $sused = [math]::round((($sysinfo[$i].Capacity - $sysinfo[$i].FreeSpace)/1GB),2)
            $fspacepercent =  [math]::Round((($sysinfo[$i].FreeSpace*100)/$sysinfo[$i].Capacity),2)
            $props = @{
                "SystemName" = $sname
                "ServerName" = $server
                "Label" = $label
                "Name" = $name
                "Capacity(GB)" = $capacity
                "FreeSpace(GB)" = $fspace
                "Used(GB)" = $sused
                "FreeSpace%" = $fspacepercent
            }
            # Build this server object.
            $serverDetails += New-Object PSObject -Property $props
        }
    } 

    # Output current details to file. 
    $serverDetails | Select $propertyOrdered | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Add-Content $filename
    #Calculate Totals and append to file.
    $totals = '"","","","Totals",{0},{1},{2},""' -f ($serverDetails | Measure-Object -Property "Capacity(GB)" -Sum).Sum,
        ($serverDetails | Measure-Object -Property "FreeSpace(GB)" -Sum).Sum,
        ($serverDetails | Measure-Object -Property "Used(GB)" -Sum).Sum
    $totals | Add-Content $filename 
}

这里的部分问题是你正在混合对象输出和静态字符串输出,这很可能会阻止你。我以符合2.0标准的方式整理对象生成。并不是说你到底发生了什么是错的,但是这比Add-Member s

更令人愉悦。

我删除了$array因为它不再有位置,因为这里的逻辑不断地将数据输出到输出文件,因为它应该暂时存储它。

对于每个$server,我们在变量$serverDetails中构建一个磁盘信息数组。一旦计算了所有磁盘(仍然使用您的公式),我们就会创建一个总计行。你猜不出你对输出的想法并不是很清楚。上面的代码应该如下所示净输出。 (它在Excel或csv识别读卡器中看起来好多了。)

"SystemName","ServerName","Label","Name","Capacity(GB)","FreeSpace(GB)","Used(GB)","FreeSpace%"
"server01","server01","System Reserved","\\?\Volume{24dbe945-3ea6-11e0-afbd-806e6f6e6963}\","0.1","0.07","0.03","71.85"
"","","","Totals",0.1,0.07,0.03,""
"server02","server02","System Reserved","\\?\Volume{24dbe945-3ea6-11e0-afbd-806e6f6e6963}\","0.1","0.07","0.03","69.27"
"server02","server02","images","I:\","1953.12","152.1","1801.02","7.79"
"server02","server02","Data","E:\","79.76","34.59","45.18","43.36"
"","","","Totals",2032.98,186.76,1846.23,""