The Multifunctioning DBA

Jan 27 2009   8:52PM GMT

Disk Monitoring Q and A

Colin Smith Colin Smith Profile: Colin Smith

Mike sent me an email asking about the input file for the disk monitoring script that I posted a while back. He could not get the script to work as he was putting the hostname and the IP of the machine in the computerlistall.txt file. The script was written to have either the hostname or the IP of the machine but not both. This started me thinking that a file with both the hostname and the IP of the machine would be useful in more ways then just this script. We may be able to use this file for multiple scripts and certainly is a good idea just to manage and keep track of names and IP’s. So I did the following to accommodate Mike. I hope this helps Mike and some others out there.

Here is a screenshot of my new computerlistall.txt file. notice that I do have headers in the file. This is very important because of the way the import-csv cmdlet works.

Computerlistall.txt

Computerlistall.txt

Now that I have that as my input I have to modify the script just a bit to accommodate this format.

The arrows point out what I have changed. I put in the $computer and the $ip lines just to show you the output when doing this.

So you can see that $compuuter is now the value of the hostname and $ip is now the value of the IP for that server. I do not know about you but I think this is freaking Cool.
So if you modify your computerlistall.txt file to be a file with header values and data values seperated by commas and modify the script as I have shown you here then you should be all set. This will allow you to use either the $computer or the $ip variable in the WMI connection string. Below is the entire modified script.

##########################################

###         Gather PAC Disk Information      ###

##########################################

Clear-Content “D:\Scripts\Powershell\PAC\lowdisk.txt”

$i = 0

$users = “some.email@address.com”

$computers = import-csv “D:\Scripts\Powershell\PAC\computerlistall.txt”

echo “ServerName        Drive Letter      Drive Size  Free Space  Percent Free” >> “D:\Scripts\Powershell\PAC\lowdisk.txt”

echo “———-        ————      ———-  ———-  ————” >> “D:\Scripts\Powershell\PAC\lowdisk.txt”

foreach ($line in $computers)

{

$computer = $line.hostname

$ip = $line.ip

$computer

$ip

}

$drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}

foreach($drive in $drives)

{

$size1 = $drive.size / 1GB

$size1

$size = “{0:N2}” -f $size1

$size

$free1 = $drive.freespace / 1GB

$free = “{0:N2}” -f $free1

$ID = $drive.DeviceID

$a = $free1 / $size1 * 100

$b = “{0:N2}” -f $a

##############################################

##    Determine if any disks low    ##

##############################################

if (($ID -eq “D:”) -or ($ID -eq “S:”) -or ($ID -eq “T:”) -or ($ID -eq “C:”) -and ($free1 -lt 1))

{

echo “$computer         $ID               $size       $free       $b” >> “D:\Scripts\Powershell\PAC\lowdisk.txt”

$i++

#[char]10 | Out-File -append ./low.txt

}

}

####################################################

##    Send Notification if alert $i is greater then 0         ##

####################################################

if ($i -gt 0)

{

foreach ($user in $users)

{

echo “Sending Email notification ro $user”

$smtpServer = “smtp server”

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)

$emailFrom = “fromuser@domain.com”

$subject = “Email Subject”

foreach ($line in Get-Content “D:\Scripts\Powershell\PAC\lowdisk.txt”)

{

$body += “$line `n”

}

$smtp.Send($EmailFrom,$user,$subject,$body)

$body = “”

}

}

Hope that helps and please drop me a line by heading over to http://sysadminsmith.com/ and clicking the Submit a Question link on the right hand side of the page. Thanks

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: