Powershell Loop all’interno di Loop

Ho uno script PowerShell che esegue un rapporto sull’utilizzo del disco (da http://gallery.technet.microsoft.com/Disk-Space-HTML-Email-f8b6bbfe )

Sto provando a cambiarlo per visualizzare anche le dimensioni della cartella, ma sono rimasto bloccato. Ho scritto questo che funziona bene:

$volName = "D:\" $folders = (Get-ChildItem $volName | ?{ $_.PSIsContainer }) foreach ($folders in $folders) { $size = (get-childitem $folders.fullname -recurse | measure-object -property length -sum) write-host $folders.fullname ($size.sum / 1MB) } 

Ma non viene fuori proprio quando cerco di inserirlo. Questo è quello che ho finora:

 # Script will generate Disk Space Report for Exchange Servers (Daily, Weekly, Monthly) $ErrorActionPreference = "SilentlyContinue"; $scriptpath = $MyInvocation.MyCommand.Definition $dir = Split-Path $scriptpath #Variables to configure $percentWarning = 25; $percentCritcal = 15; $smtpServer = "[email protected]" $ReportSender = "[email protected]" $users = "[email protected]", "[email protected]"; $MailSubject = "DiskSpace Report for $titledate" #No change needed from here!!! $reportPath = "$dir\Logs\" $reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html"; $diskReport = $reportPath + $reportName $redColor = "#FF0000" $orangeColor = "#FBB917" $whiteColor = "#FFFFFF" $greenColor = "#7FFF00" $i = 0; $computers = $env:COMPUTERNAME; $datetime = Get-Date -Format "MM-dd-yyyy_HHmmss"; If (Test-Path $diskReport) { Remove-Item $diskReport } $titleDate = get-date -uformat "%m-%d-%Y - %A" $header = "    DiskSpace Report      
DiskSpace Report for $titledate
" Add-Content $diskReport $header $tableHeader = " " Add-Content $diskReport $tableHeader foreach($computer in $computers) { $disks = Get-WmiObject -ComputerName $computer -Class Win32_Volume -Filter "DriveType = 3" | Where-Object {$_.Label -ne "System Reserved" -and $_.DriveLetter -ne "C:"} $computer = $computer.toupper() foreach($disk in $disks) { $deviceID = $disk.Label; $volName = $disk.Name; $folders = (Get-ChildItem $volName | ?{ $_.PSIsContainer }); foreach ($folders in $folders) { $size = (get-childitem $folders.fullname -recurse | measure-object -property length -sum) [float]$size = $disk.Capacity; [float]$freespace = $disk.FreeSpace; $percentFree = [Math]::Round(($freespace / $size) * 100, 2); $sizeGB = [Math]::Round($size / 1073741824, 2); $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2); $usedSpaceGB = [Math]::Round($sizeGB - $freeSpaceGB, 2); $color = $greenColor; if($percentFree -lt $percentWarning) { $color = $orangeColor if($percentFree -lt $percentCritcal) { $color = $redColor } } $dataRow = " " Add-Content $diskReport $dataRow; Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree"; $i++ } } } $tableDescription = "
Server Drive Folder Folder Size Total Capacity(GB) Used Capacity(GB) Free Space(GB) Freespace %
$computer $volName $folders.fullname $size $sizeGB $usedSpaceGB $freeSpaceGB $percentFree

" Add-Content $diskReport $tableDescription Add-Content $diskReport "" if ($i -gt 0) { foreach ($user in $users) { Write-Host "Sending Email notification to $user" $smtp = New-Object Net.Mail.SmtpClient($smtpServer) $msg = New-Object Net.Mail.MailMessage $msg.To.Add($user) $msg.From = $ReportSender $msg.Subject = $MailSubject $msg.IsBodyHTML = $true $msg.Body = get-content $diskReport $smtp.Send($msg) $body = "" } }

Ed esce così:

http://sofit.miximages.com/html/ru9sfb.jpg

Qualcuno sa perché il nome della cartella e le variabili delle dimensioni della cartella non funzionano?

Penso che il problema (o uno dei problemi) sia questa linea: foreach ($folders in $folders) . Dovrebbe essere foreach ($folder in $folders) e poi ovunque nel ciclo for che si fa riferimento alla cartella corrente si dovrebbe usare $folder (non $folders ).

Fai una prova e guarda come appare.

Grazie per l’aiuto, risulta che la modifica della cartella $ (e ha creato poche altre variabili) ha funzionato. Segnala con cartelle e dimensioni delle cartelle:

 # Script will generate Disk Space Report for Exchange Servers (Daily, Weekly, Monthly) $ErrorActionPreference = "SilentlyContinue"; $scriptpath = $MyInvocation.MyCommand.Definition $dir = Split-Path $scriptpath #Variables to configure $percentWarning = 25; $percentCritcal = 15; $smtpServer = "[email protected]" $ReportSender = "[email protected]" $users = "[email protected]", "[email protected]"; $MailSubject = "DiskSpace Report for $titledate" #No change needed from here!!! $reportPath = "$dir\Logs\" $reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html"; $diskReport = $reportPath + $reportName $redColor = "#FF0000" $orangeColor = "#FBB917" $whiteColor = "#FFFFFF" $greenColor = "#7FFF00" $i = 0; $computers = $env:COMPUTERNAME; $datetime = Get-Date -Format "MM-dd-yyyy_HHmmss"; If (Test-Path $diskReport) { Remove-Item $diskReport } $titleDate = get-date -uformat "%m-%d-%Y - %A" $header = "    DiskSpace Report    
Warning less than $percentWarning% free space Critical less than $percentCritcal% free space
DiskSpace Report for $titledate
" Add-Content $diskReport $header $tableHeader = " " Add-Content $diskReport $tableHeader foreach($computer in $computers) { $disks = Get-WmiObject -ComputerName $computer -Class Win32_Volume -Filter "DriveType = 3" | Where-Object {$_.Label -ne "System Reserved" -and $_.DriveLetter -ne "C:"} $computer = $computer.toupper() foreach($disk in $disks) { $deviceID = $disk.Label; $volName = $disk.Name; $folders = (Get-ChildItem $volName | ?{ $_.PSIsContainer }); foreach ($folder in $folders) { $sizefolder = (get-childitem $folder.fullname -recurse | measure-object -property length -sum) $FolderPaths = $folder.fullname [float]$FolderSize = $sizefolder.sum $foldersizeMB = [Math]::Round($foldersize / 1048576, 2); [float]$size = $disk.Capacity; [float]$freespace = $disk.FreeSpace; $percentFree = [Math]::Round(($freespace / $size) * 100, 2); $sizeGB = [Math]::Round($size / 1073741824, 2); $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2); $usedSpaceGB = [Math]::Round($sizeGB - $freeSpaceGB, 2); $color = $greenColor; if($percentFree -lt $percentWarning) { $color = $orangeColor if($percentFree -lt $percentCritcal) { $color = $redColor } } $dataRow = " " Add-Content $diskReport $dataRow; Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree"; $i++ } } } $tableDescription = "
Server Drive Folder Folder Size (MB) Total Capacity(GB) Used Capacity(GB) Free Space(GB) Freespace %
$computer $volName $folderpaths $foldersizeMB $sizeGB $usedSpaceGB $freeSpaceGB $percentFree

" Add-Content $diskReport $tableDescription Add-Content $diskReport "
Warning less than $percentWarning% free space Critical less than $percentCritcal% free space
" if ($i -gt 0) { foreach ($user in $users) { Write-Host "Sending Email notification to $user" $smtp = New-Object Net.Mail.SmtpClient($smtpServer) $msg = New-Object Net.Mail.MailMessage $msg.To.Add($user) $msg.From = $ReportSender $msg.Subject = $MailSubject $msg.IsBodyHTML = $true $msg.Body = get-content $diskReport $smtp.Send($msg) $body = "" } }