I need some help getting the below to work. I must use WinrM witrh a server list.. can anyone help ?
$sessions = get-content D:\Powershell\Space\Allservers.txt | New-PSSession
$block = {
function Get-DriveSpace
{
[cmdletbinding()]
param( [Parameter(Mandatory=$true)]$computer , [int]$thresholdMultiplier=.20 )
[string]$filter = "DriveType = '3'"
try{
Get-WmiObject -class win32_logicaldisk -filter $filter -comp $computer |
Select-Object Name,@{label="SizeMB";Expression={[Math]::Truncate($_.Size / 1MB)}},
@{label="FreeMB";Expression={[Math]::Truncate($_.FreeSpace / 1MB)}},
@{label="WarningMB";Expression={[Math]::Truncate(($_.Size * $thresholdMultiplier) /1MB)}}|
Where-Object{$_.FreeMB -le $_.WarningMB}
} catch {
Write-Verbose "Error $Error in getting disk space for $computer"
}
}
}
[string]$body = Invoke-Command -ScriptBlock $block -Session $sessions | Select sizeMB,freeMB,WarningMB | Sort WarningMB | ConvertTo-Html
Send-MailMessage -SmtpServer " " -From " " -To " " -Subject "Servers Less Than 20% Free" -Body $body -BodyAsHtml
$sessions = get-content D:\Powershell\Space\Allservers.txt | New-PSSession
$block = {
function Get-DriveSpace
{
[cmdletbinding()]
param( [Parameter(Mandatory=$true)]$computer , [int]$thresholdMultiplier=.20 )
[string]$filter = "DriveType = '3'"
try{
Get-WmiObject -class win32_logicaldisk -filter $filter -comp $computer |
Select-Object Name,@{label="SizeMB";Expression={[Math]::Truncate($_.Size / 1MB)}},
@{label="FreeMB";Expression={[Math]::Truncate($_.FreeSpace / 1MB)}},
@{label="WarningMB";Expression={[Math]::Truncate(($_.Size * $thresholdMultiplier) /1MB)}}|
Where-Object{$_.FreeMB -le $_.WarningMB}
} catch {
Write-Verbose "Error $Error in getting disk space for $computer"
}
}
}
[string]$body = Invoke-Command -ScriptBlock $block -Session $sessions | Select sizeMB,freeMB,WarningMB | Sort WarningMB | ConvertTo-Html
Send-MailMessage -SmtpServer " " -From " " -To " " -Subject "Servers Less Than 20% Free" -Body $body -BodyAsHtml