2014-02-17 40 views
2

我仍在學習PowerShell腳本編寫方法,並且正在研究一個腳本來計算文件服務器的可用空間百分比,並在驅動器達到10%時發送電子郵件通知剩餘空間或更少(這大概每月發生一次,而且在我從客戶端收到電子郵件之前我需要知道沒有更多空間)。截至目前,該腳本運行良好,並且設置爲每天早上通過Windows任務運行。但是我現有的輸出格式是手動完成的。我想知道是否有一種方法可以通過Get-WmiObject函數收集和計算的信息傳遞變量。我已經嘗試過格式表並試圖搞亂哈希表,但無濟於事。任何想法都會有所幫助。謝謝。在PowerShell中創建包含變量的表格

# Set Parameters 
$file = "c:\location\Lowdisk.txt" 
Clear-Content $file 

$emailTO = "[email protected]" 
$emailFrom = "[email protected]" 
$smtpServer = "smtpServer" 

$diskspace = "3" 
$computers = ("FSCN01","FSCN02","FSCN03","FSCN04") 
echo "Server Name  Drive  Drive Size  Free Space  % Free" >> $file 
$i = 0 

# Get Drive Data 
foreach($computer in $computers) 
{ 
$drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} 
foreach($drive in $drives) 
{ 
    $ID = $drive.DeviceID 
    $size1 = $drive.size/1GB 
    $size = "{0:N1}" -f $size1 
    $free1 = $drive.freespace/1GB 
    $free = "{0:N1}" -f $free1 
    $a = $free1/$size1 * 100 
    $b = "{0:N1}" -f $a 
     # Monitor for drive free space % under 10% 
     if ($b -lt 10) 
     { 
      echo "$computer   $ID   $size   $free   $b" >> $file 
      $i++ 
     } 
} 
} 
# Send notification if script finds more than 0 drives with less than 35% free space 
    if ($i -gt 0) 
    { 
     foreach ($user in $emailTo) 
      { 
     echo "Sending Email Notification to $user" 
     $smtp = New-Object Net.Mail.SmtpClient($smtpServer) 
     $subject = "Server with Low Disk Space" 
     foreach ($line in Get-Content $file) 
       { 
       $body += "$line `n" 
       } 
     Send-MailMessage -to $user -From $emailFrom -Attachments $file -SmtpServer $smtpServer -Subject $Subject -Body $body 
     $body = "" 
       } 
    } 

回答

6

Format-Table當您在單一類型的對象中擁有所有感興趣的數據時效果最佳。我建議這個如創建自定義對象:

foreach($computer in $computers) 
{ 
    $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} 
    foreach($drive in $drives) 
    { 
     $obj = new-object psobject -Property @{ 
        ComputerName = $computer 
        Drive = $drive.DeviceID 
        Size = $drive.size/1GB 
        Free = $drive.freespace/1GB 
        PercentFree = $drive.freespace/$drive.size * 100 
       } 
     if ($obj.PercentFree -lt 10) { 
      $obj | Format-Table ComputerName,Drive,Size,Free,PercentFree 
      $i++ 
     } 
    } 
} 

如果你想改變顯示格式,然後在格式-table命令,你可以這樣做:

+0

這正是我期望完成的。謝謝你的幫助!! – user3140412

1

基思有它覆蓋,但因爲無論如何我的回答是一半寫的,在這裏。我的版本不需要創建新對象,並將過濾作爲管道的一部分。正如Keith所說,把所有東西都當作一個物體,直到最後一刻。如果您需要強制PowerShell進行格式化,請使用'out-string'。

另外,請注意支持列表。許多命令會將列表作爲參數(例如計算機名稱列表,電子郵件收件人),而不需要for-each循環。這可以使代碼更加簡潔,但可以說可讀性較差,尤其是對於初學者。無論如何,這是我製作的:

[email protected]("server1","server2","server3","server4") 
$smtpServer = "mail.mydomain.com" 
$toList = @("[email protected]","[email protected]") 
$minPercent = 10 

$tableSpec = @(
     @{label="Computer";expression={$_.SystemName}}, 
     @{label="Disk";expression={$_.DeviceID}}, 
     @{label="Size Gb";expression={[int]($_.size/1Gb)}}, 
     @{label="PercentFree";expression={[int]($_.FreeSpace/$_.Size * 100)}} 
    ) 

$report = (

    Get-WmiObject -ComputerName $comps -Class Win32_LogicalDisk | 
    Where-Object { ($_.DriveType -eq 3) -and ($_.FreeSpace -lt ($_.Size * $minPercent/100)) } | 
    Sort-Object SystemName, DeviceID | 
    ft -property $tableSpec -AutoSize | 
    Out-String 

    ) 

Send-MailMessage -Body $report -To $toList -Subject "Disk space report" -SmtpServer $smtpServer 
相關問題