With a command line I noticed that my free disk space is below some value – II


Just to finish my script, if I want to create a notification to all my computers and disks :

Now I am creating a Posh object to send in –messagedata and showing  which computer and volume it is notifying

   1:  

   2: function Notify-SpaceDisk     {

   3:  

   4:     Param (

   5:             [Parameter(position=0,Mandatory = $true,ValueFromPipeline = $true )] $ComputerName,

   6:             [Parameter(position=1,Mandatory = $true)] $action

   7:         )

   8:         

   9:         Get-WmiObject -computername $($ComputerName)  -query "select  *  from Win32_Volume where (DriveType = 2) or (DriveType = 3)"  |  ForEach-Object {

  10:                         

  11:  

  12:                         $pso = New-Object Psobject -property @{Server=$ComputerName;Driveletter=$_.driveletter} 

  13:                         try {

  14:                              Register-WMIEvent -ComputerName $($ComputerName) -Query "Select * from __instanceModificationEvent WITHIN 5 WHERE TargetInstance ISA 'Win32_PerfFormattedData_PerfDisk_LogicalDisk' AND TargetInstance.NAME=`'$($_.Driveletter)`' AND TargetInstance.freemegabytes <450000" -Action  $action -MessageData $pso | Out-null

  15:                             Write-Host "Event registered in Volume $($_.driveletter) computer $($computername)"

  16:                             

  17:                         } catch {

  18:                             $msg = "Failed to Register WMI Event on {0} volume [2}. {1}" -f $($ComputerName),$($_.driveletter),$($_.Exception.Message)

  19:                              Write-Warning -Message $msg

  20:                         }

  21:          

  22:                         

  23:         }

  24: }

What I done , just create a function to receive the Computer name and the action that I want to do.

So if I want to just show in a console ;

   1: $ComputerName = 'Vader'

   2: $action = {write-host "Hey..hey..wakeup Man. Take a look in Your $($event.MessageData.Server), volume $($event.MessageData.DriveLetter) , it is almost full"}  

   3: Notify-SpaceDisk $Computername $action

or send email  receiving computername by pipe

   1:  

   2: $action = {param ($pso) ; ipmo functions;Send-SMTPmail -smtpserver 'smtp.mail.yahoo.com.br' -from 'juniorlaerte@yahoo.com.br' -to 'laertejuniordba@hotmail.com' -subject 'Event Notification' -body "Hey..hey..wakeup Man. Take a look in Your $($event.MessageData.Server), volume $($event.MessageData.DriveLetter) , it is almost full"}  

   3: "Vader" | Notify-SpaceDisk -action $action

 

You can  also do a lot of cool stuff. You can pass the WHITIN Seconds  and WQL by parameter.., create a xml with the computers and volumes that you want to be notified ..etc Smile

 

POWERSHELL ROCKS !!!

 

Ella despidió a su amor
Él partió en un barco en el muelle de San Blas
Él juró que volvería
Y empapada en llanto ella juró que esperaría
Miles de lunas pasaron
Y siempre ella estaba en el muelle esperando
Muchas tardes se anidaron
Se anidaron en su pelo y en sus labios

Llevaba el mismo vestido
Y por si él volviera no se fuera a equivocar
Los cangrejos le mordían
Su ropaje, su tristeza y su ilusión
Y el tiempo se escurrió
Y sus ojos se le llenaron de amaneceres
Y del mar se enamoró
Y su cuerpo se enraizó
En el muelle

Sola, sola en el olvido
Sola, sola con su espíritu
Sola, sola con su amor en mar
Sola en el muelle de San Blas

En El Muelle De San Blas

Maná

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Powershell, SQL SERVER EM GERAL, Virtual Pass BR. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s