Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

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

    I try to run below script by PS1 have error.









    Code:
    # 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 "[email protected]"
    $to= New-Object System.Net.Mail.MailAddress "[email protected]"
    $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 [email protected] [email protected] "Disk Space Report - $Date" hub1 $freeSpaceFileName
    Last edited by momaydopod; 16th January 2014, 08:59.

  • #2
    Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

    Error this script

    Comment


    • #3
      Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

      Code:
      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
       
      
      ________________________________________________________________________________________________________________________________________________________________________

      Comment


      • #4
        Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

        Can you please post where you got the original script from.

        Comment


        • #5
          Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

          Originally posted by wullieb1 View Post
          Can you please post where you got the original script from.
          Never mind this is it

          http://social.technet.microsoft.com/...m=winservergen

          Comment


          • #6
            Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

            From the original script, that didn't actually work, i managed to get this working script going.

            Code:
            #requires -version 2.0 
            #use parameter  drive report to html.ps1 computer1,computer2 or a computer list file 
            #change file path and name on line 7 below to reflect name and  path of computer list file using. 
            #script will open web browser with current report when completed. 
             
            #Param ( 
            #$computers = (Get-Content  "C:\Scripts\Computers.txt") 
            #) 
             
            $computers = "YOURCOMPNAME" 
            
            $Path = "c:\script\DriveReport.html"
            
            $Title="Hard Drive Report to HTML" 
             
            #embed a stylesheet in the html header 
            $head = @" 
            <mce:style><!-- 
            mce:0 
            --></mce:style><style _mce_bogus="1"><!-- 
            mce:0 
            --></style> 
            <Title>$Title</Title> 
            <br> 
            "@  
             
            #define an array for html fragments 
            [email protected]() 
             
            #get the drive data 
            $data=Get-WmiObject -Class Win32_logicaldisk -filter "drivetype=3" -computer $computers 
             
            #group data by computername 
            $groups=$Data | Group-Object -Property SystemName 
             
            #this is the graph character 
            [string]$g=[char]9608  
             
            #create html fragments for each computer 
            #iterate through each group object 
                     
            ForEach ($computer in $groups) { 
                 
                $fragments+="<H2>$($computer.Name)</H2>" 
                 
                #define a collection of drives from the group object 
                $Drives=$computer.group 
                 
                #create an html fragment 
                $html=$drives | Select @{Name="Drive";Expression={$_.DeviceID}}, 
                @{Name="SizeGB";Expression={$_.Size/1GB  -as [int]}}, 
                @{Name="UsedGB";Expression={"{0:N2}" -f (($_.Size - $_.Freespace)/1GB) }}, 
                @{Name="FreeGB";Expression={"{0:N2}" -f ($_.FreeSpace/1GB) }}, 
                @{Name="Usage";Expression={ 
                  $UsedPer= (($_.Size - $_.Freespace)/$_.Size)*100 
                  $UsedGraph=$g * ($UsedPer/2) 
                  $FreeGraph=$g* ((100-$UsedPer)/2) 
                  #I'm using place holders for the < and > characters 
                  "xopenFont color=Redxclose{0}xopen/FontxclosexopenFont Color=Greenxclose{1}xopen/fontxclose" -f $usedGraph,$FreeGraph 
                }} | ConvertTo-Html -Fragment  
                 
                #replace the tag place holders. It is a hack but it works. 
                $html=$html -replace "xopen","<" 
                $html=$html -replace "xclose",">" 
                 
                #add to fragments 
                $Fragments+=$html 
                 
                #insert a return between each computer 
                $fragments+="<br>" 
                 
            } #foreach computer 
             
            #add a footer 
            $footer=("<br><I>Report run {0} by {1}\{2}<I>" -f (Get-Date -displayhint date),$env:userdomain,$env:username) 
            $fragments+=$footer 
             
            #write the result to a file 
            ConvertTo-Html -head $head -body $fragments  | Out-File -FilePath $Path
            That gives me what i require from there.

            Comment


            • #7
              Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

              Try:

              Code:
              $comp = "YOURCOMPNAME"   # single computer!
              
              $data = Get-WmiObject -class Win32_logicaldisk -filter "drivetype=3" -computername $comp
              
              $data
              /Rems
              Last edited by Rems; 17th January 2014, 00:38.

              This posting is provided "AS IS" with no warranties, and confers no rights.

              __________________

              ** Remember to give credit where credit's due **
              and leave Reputation Points for meaningful posts

              Comment


              • #8
                Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

                This script, a copy of yours, works for me after a couple of changes.

                Code:
                # First lets create a text file, where we will later save the freedisk space info
                $freeSpaceFileName = "c:\script\FreeSpace.htm"
                $serverlist = "c:\script\computers.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 "[email protected]"
                $to= New-Object System.Net.Mail.MailAddress "[email protected]"
                $subject="Servers Disk space report - $Date" 
                $smtphost="BRISBANE-EX03.int.heatandcontrol.com"
                $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 [email protected] [email protected] "Disk Space Report - $Date" hub1 $freeSpaceFileName

                Comment


                • #9
                  Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

                  Script is Very Works


                  Thank you very much for you support


                  How to add attachment of HTML to email ? Because when sent to email format is incorrect .
                  Last edited by momaydopod; 17th January 2014, 08:20.

                  Comment


                  • #10
                    Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

                    How to attache HTML file in to email ?

                    Comment


                    • #11
                      Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

                      Originally posted by momaydopod View Post
                      How to attache HTML file in to email ?
                      Change the SendEmail function to this

                      Code:
                      Function sendEmail
                      { param($from,$to,$subject,$smtphost,$htmlFileName)
                      $from=New-Object System.Net.Mail.MailAddress "fromemail"
                      $to= New-Object System.Net.Mail.MailAddress "toemail"
                      $subject="Servers Disk space report - $Date" 
                      $smtphost="emailserver"
                      #$body = Get-Content $htmlFileName
                      $smtp= New-Object System.Net.Mail.SmtpClient $smtphost
                      $att = new-object Net.Mail.Attachment($htmlFileName)
                      $msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
                      $msg.isBodyhtml = $true
                      $msg.Attachments.Add($att)
                      $smtp.send($msg)
                      $att.dispose()
                      }

                      Comment


                      • #12
                        Re: PowerShell Check Monitor and Report the Disk Space through SCRIPT using PowerShel

                        Yes i can do it , Thank you for you answer

                        Originally posted by wullieb1 View Post
                        Change the SendEmail function to this

                        Code:
                        Function sendEmail
                        { param($from,$to,$subject,$smtphost,$htmlFileName)
                        $from=New-Object System.Net.Mail.MailAddress "fromemail"
                        $to= New-Object System.Net.Mail.MailAddress "toemail"
                        $subject="Servers Disk space report - $Date" 
                        $smtphost="emailserver"
                        #$body = Get-Content $htmlFileName
                        $smtp= New-Object System.Net.Mail.SmtpClient $smtphost
                        $att = new-object Net.Mail.Attachment($htmlFileName)
                        $msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
                        $msg.isBodyhtml = $true
                        $msg.Attachments.Add($att)
                        $smtp.send($msg)
                        $att.dispose()
                        }

                        Comment

                        Working...
                        X