PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShell or VB

As noted above - the code you are running and the code you posted do not match.

Look closely at the error message.  It shows you exactly where the problem is.  You have edited the script and added an illegal syntax.

$data=Get-WmiObject  -Class Win32_logicaldisk "drivetype=3" -computer $computers

The original code you posted has this:

$dp = Get-WmiObject win32_logicaldisk -ComputerName $server |  Where-Object {$_.drivetype -eq 3}

The big question is, why did you change it?

January 16th, 2014 9:00am

You are also getting different errors each time.  You may have incaccessible systems and you have no error management.  I recommend posting issues that you are having with the creator of the script.  They may have already fixed this,

Free Windows Admin Tool Kit Click here and download it now
January 16th, 2014 9:02am

I try to run below script by PS1 have error.

# First lets create a text file, where we will later save the freedisk space info
$freeSpaceFileName = "FreeSpace.htm"
$serverlist = "Serverlist.txt"
$warning = 30
$critical = 10
New-Item -ItemType file $freeSpaceFileName -Force
# Getting the freespace info using WMI
#Get-WmiObject win32_logicaldisk  | Where-Object {$_.drivetype -eq 3} | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt
# Function to write the HTML Header to the file
Function writeHtmlHeader
{
param($fileName)
$date = ( get-date ).ToString('dd/mm/yyyy')
Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName '<title>All Servers DiskSpace Report</title>'
add-content $fileName '<STYLE TYPE="text/css">'
add-content $fileName  "<!--"
add-content $fileName  "td {"
add-content $fileName  "font-family: Tahoma;"
add-content $fileName  "font-size: 11px;"
add-content $fileName  "border-top: 1px solid #999999;"
add-content $fileName  "border-right: 1px solid #999999;"
add-content $fileName  "border-bottom: 1px solid #999999;"
add-content $fileName  "border-left: 1px solid #999999;"
add-content $fileName  "padding-top: 0px;"
add-content $fileName  "padding-right: 0px;"
add-content $fileName  "padding-bottom: 0px;"
add-content $fileName  "padding-left: 0px;"
add-content $fileName  "}"
add-content $fileName  "body {"
add-content $fileName  "margin-left: 5px;"
add-content $fileName  "margin-top: 5px;"
add-content $fileName  "margin-right: 0px;"
add-content $fileName  "margin-bottom: 10px;"
add-content $fileName  ""
add-content $fileName  "table {"
add-content $fileName  "border: thin solid #000000;"
add-content $fileName  "}"
add-content $fileName  "-->"
add-content $fileName  "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"

add-content $fileName  "<table width='100%'>"
add-content $fileName  "<tr bgcolor='#CCCCCC'>"
add-content $fileName  "<td colspan='7' height='25' align='center'>"
add-content $fileName  "<font face='tahoma' color='#003399' size='4'><strong>All Servers DiskSpace Report - $date</strong></font>"
add-content $fileName  "</td>"
add-content $fileName  "</tr>"
add-content $fileName  "</table>"

}

# Function to write the HTML Header to the file
Function writeTableHeader
{
param($fileName)

Add-Content $fileName "<tr bgcolor=#CCCCCC>"
Add-Content $fileName "<td width='10%' align='center'>Drive</td>"
Add-Content $fileName "<td width='50%' align='center'>Drive Label</td>"
Add-Content $fileName "<td width='10%' align='center'>Total Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Used Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Free Space(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Freespace %</td>"
Add-Content $fileName "</tr>"
}

Function writeHtmlFooter
{
param($fileName)

Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}

Function writeDiskInfo
{
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace - $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
 if ($freePercent -gt $warning)
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$devid</td>"
 Add-Content $fileName "<td>$volName</td>"

 Add-Content $fileName "<td>$totSpace</td>"
 Add-Content $fileName "<td>$usedSpace</td>"
 Add-Content $fileName "<td>$frSpace</td>"
 Add-Content $fileName "<td>$freePercent</td>"
 Add-Content $fileName "</tr>"
 }
 elseif ($freePercent -le $critical)
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$devid</td>"
 Add-Content $fileName "<td>$volName</td>"
 Add-Content $fileName "<td>$totSpace</td>"
 Add-Content $fileName "<td>$usedSpace</td>"
 Add-Content $fileName "<td>$frSpace</td>"
 Add-Content $fileName "<td bgcolor='#FF0000' align=center>$freePercent</td>"
 #<td bgcolor='#FF0000' align=center>
 Add-Content $fileName "</tr>"
 }
 else
 {
 Add-Content $fileName "<tr>"
 Add-Content $fileName "<td>$devid</td>"
 Add-Content $fileName "<td>$volName</td>"
 Add-Content $fileName "<td>$totSpace</td>"
 Add-Content $fileName "<td>$usedSpace</td>"
 Add-Content $fileName "<td>$frSpace</td>"
 Add-Content $fileName "<td bgcolor='#FBB917' align=center>$freePercent</td>"
 # #FBB917
 Add-Content $fileName "</tr>"
 }
}
Function sendEmail
{ param($from,$to,$subject,$smtphost,$htmlFileName)
$from=New-Object System.Net.Mail.MailAddress "xxx@test.com"
$to= New-Object System.Net.Mail.MailAddress "xxx@test.com"
$subject="Servers Disk space report - $Date" 
$smtphost="X.X.X.X"
$body = Get-Content $htmlFileName
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)

}

writeHtmlHeader $freeSpaceFileName
foreach ($server in Get-Content $serverlist)
{
 Add-Content $freeSpaceFileName "<table width='100%'><tbody>"
 Add-Content $freeSpaceFileName "<tr bgcolor='#CCCCCC'>"
 Add-Content $freeSpaceFileName "<td width='100%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong> $server </strong></font></td>"
 Add-Content $freeSpaceFileName "</tr>"

 writeTableHeader $freeSpaceFileName

 $dp = Get-WmiObject win32_logicaldisk -ComputerName $server |  Where-Object {$_.drivetype -eq 3}
 foreach ($item in $dp)
 {
 Write-Host  $item.DeviceID  $item.VolumeName $item.FreeSpace $item.Size
 writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size

 }
}
writeHtmlFooter $freeSpaceFileName
$date = ( get-date ).ToString('yyyy/MM/dd')
sendEmail xxx@test.com xxx@test.com "Disk Space Report - $Date" hub1 $freeSpaceFileName

January 16th, 2014 10:52am

Free Windows Admin Tool Kit Click here and download it now
January 16th, 2014 10:56am

Hi momaydopod,

when posting errors, please try to copy&paste the error-text, not a Screenshot. Most people don't have microscopes at their desk ;) (It also makes working with the error-text easier, like looking something up ...)

The Script in your screenshot and the one you posted above appear to not match.

When having trouble with a large script that contains multiple functions, you may want to minimize clutter, by leaving out the cosmetic functions that work without error. Posting a big Wall-of-Script reduces the chance someone else will try looking it through and actually find your error.

Finally, when asking a question, you may want to include a bit more text than just one short line, saying "I try to run this, have error". Adding a proper greeting, describing what you are trying to achieve, what troubleshooting you did and professing eternal gratitude if someone will help (yeah, that last one is a bit of an exaggeration) certainly will increase your chance someone will try to help you.

Now you got advice on how to post in this forum instead on your script-problem, because frankly, that appears a more critical problem of yours ;)

Cheers and good luck with the script,
Fred

January 16th, 2014 11:16am

PS C:\disk> C:\disk\disk2.ps1
Get-WmiObject : Invalid namespace 
At C:\disk\disk2.ps1:27 char:20
+ $data=Get-WmiObject <<<<  -Class Win32_logicaldisk "drivetype=3" -computer $computers 
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
 
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\disk\disk2.ps1:27 char:20
+ $data=Get-WmiObject <<<<  -Class Win32_logicaldisk "drivetype=3" -computer $computers 
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
 
Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
At C:\disk\disk2.ps1:75 char:56
+ ConvertTo-Html -head $head -body $fragments  | Out-File <<<<  $Path 
    + CategoryInfo          : InvalidData: (:) [Out-File], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand
 
The term '.\drivereport.htm' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was includ
ed, verify that the path is correct and try again.
At C:\disk\disk2.ps1:77 char:18
+ .\drivereport.htm <<<< 
    + CategoryInfo          : ObjectNotFound: (.\drivereport.htm:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 

________________________________________________________________________________________________________________________________________________________________________

Free Windows Admin Tool Kit Click here and download it now
January 16th, 2014 1:47pm

How to add attachment HTML to this script
January 19th, 2014 12:17am

How to attache HTML file in to email ?
Free Windows Admin Tool Kit Click here and download it now
January 21st, 2014 11:22pm

How to attache HTML file in to email ?

Is this a new question?

January 21st, 2014 11:27pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics