header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Disk space alert through e-mail
Last Post 03 Aug 2009 04:51 AM by Chad Miller. 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
anandaUser is Offline
New Member
New Member
Posts:28
Avatar

--
03 Aug 2009 04:41 AM
    This is script below,

    #array with the server name to check
     $serversToCheck="servername"
    #array tostore the results
     $results=@()
    #loop through the list of servers.....
    foreach ($server in $serversToCheck)
    {
    #get information about the server logical disk drives(include only the drive,size and free space
    $diskList=Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -computerName $server | select-Object -Property Name, Size, FreeSpace
    #loop through the server drives and add a few custom properties
     foreach ($disk in $diskList)
     {
     add-member -inputobject $disk -type noteproperty -name Machine -value $server;
    add-member -inputobject $disk -type noteproperty -name PrecentFree -value ([long]$disk.freespace/[long]$disk.Size);
    add-member -inputobject $disk -type scriptproperty -name Status -value
    {
    if($this.PrecentFree -lt 0.20){"CRITTICAL"} elseif ($this.PrecentFree -lt 0.35) {"WARNING"}else{"OK"}}; #add the drive info to the result array $results += $disk; } }
    #return from the results only data for drives with status different than Ok
    #format the output in table, grouped by server name, with size in GB
    $badDisks = $results | where-object -filterScript {$-.Status -ne "OK"}
    #check if there are any disks which are running on low disk space
    if ($badDisks)
    {
    #format the output (store it in a file)
    $badDisks | format-table -groupBy Machine -Property
     @{lable="Drive";expression={$_.Name}},
    @{lable="Disk Size ";expression={"{0,7:N2} GB" -f ($_.Size / 1gb)}},
     @{lable="Free Space"; expression={"{0,72:N2} GB" -f($_.FreeSpace / 1gb)}},
    @{lable="Precent Free";expression={"{0,12:P}" -f $_.PrecentFree}},
    @{lable="Free Status";expression={$_.Status}} | out-file "D:\ananda\powershell\baddisk.txt" -encoding Unicode
     [string]$msg=get-content -path "D:\ananda\powershell\baddisks.txt" -encoding Unicode

    #send out an alert e-mail
    $SmtpClient = new-object system.net.mail.smtpclient ("10.4.54.22") #$SmtpClient.host $From="Diskspacealert@ril.com" $to="ananda.murugesan@ril.com" $Title="Low disk space alert" $Body="The following drive are runnning low on diskspace 'n" + $msg $SmtpClient.send($from,$to,$title,$body) }

    I could not able to correct this error, Pl can anyone help me .


    Error details:

    The term '$-.Status' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again. At D:\DC\DiskspaceAlert.ps1:28 char:61 + $badDisks = $results | where-object -filterScript {$-.Status <<<< -ne "OK"} The term '$-.Status' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again. At D:\DC\DiskspaceAlert.ps1:28 char:61 + $badDisks = $results | where-object -filterScript {$-.Status <<<< -ne "OK"}

    thanks
    Chad MillerUser is Offline
    Basic Member
    Basic Member
    Posts:160
    Avatar

    --
    03 Aug 2009 04:51 AM
    One problem I noticed is that you have a format before a select or foreach statement. The second problem you are missing a select or foreach

    This line

    $badDisks | format-table -groupBy Machine -Property

    should be

    $badDisk | Select

    the format-table statement should be after the select and before the out-file

    For the error, I don't see where status is being set to a value in your script. The where condition should be (you have a dash instead of an underline plus the filter script terms are not needed):

    $results | where-object {$_.Status -ne "OK"}
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer