header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Monitoring/Logging drives on servers
Last Post 26 Dec 2008 05:16 PM by jason_stephens. 2 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
jason_stephensUser is Offline
New Member
New Member
Posts:15
Avatar

--
04 Nov 2008 06:23 PM  

Just to start off, I am a novice. There is another post here about getting drive info:

powershellcommunity.org/Forums/tabid/54/forumid/14/postid/2432/view/topic/Default.aspx

I didn't want to monkey up that post with the discussion of my solution, but thought the two were closely related. The main difference is my problem was not only monitoring the drive space of servers but also to see trends. First lets see the code:

#------------------------------------------------------------------------------
# Title: drive_log.ps1
# Author: Jason Stephens
# Date: 27 Oct 08
# Description: Log drive space and free space of all drives
# Version: 1.0
#------------------------------------------------------------------------------
$computer = "AAMCMLM1"
$logLocation = "I:\Jason Stephens\AAMCMLM1\drive_log.xls"

# Start Microsoft Excel and open Excel document
$objExcel = New-Object -comobject Excel.Application
$objWorkbook = $objExcel.Workbooks.Open($logLocation)

# Select the next available cell in column A
$xlup = -4162
$range = "A" + $objExcel.Rows.Count
[void]$objExcel.Range($range).End($xlup).Activate()
[void]$objExcel.ActiveCell.Offset(1,0).Activate()

# Obtain drive information
$logicalDisks=Get-WMIObject -computername $computer -Class Win32_LogicalDisk -filter "DriveType=3"

# Insert information into excel log
$objExcel.ActiveCell.Value2 = Get-Date -Format g
[void]$objExcel.ActiveCell.Offset(0,1).Activate()
Foreach($disk in $logicaldisks)
{
 $size = "{0:N3}" -f ($disk.size/1GB)
 $free = "{0:N3}" -f ($disk.freespace/1GB)
 $objExcel.ActiveCell.Value2 = "$size / $free" 
 [void]$objExcel.ActiveCell.Offset(0,1).Activate()
}

# Save and shut down excel
$objWorkbook.Save()
$objExcel.Quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel)
Remove-Variable objExcel

Now, as you can tell, I have used Microsoft's Excel instead of a CSV file. I am not sure of the wisdom of that other than it is only done for formatting reasons like a frozen header row.

Also, I didn't use the -Query that was used in the other post. Should I have? I don't have the drive name or title in my query because it is in the frozen frame in the excel document. I would ofcourse have to edit that should the drives change their setup. That doesn't happen that often, so not a major concern of mine.

I also didn't use a list of servers file. Mainly because of what the poster on the other post had a fear of. When one server failed, your script wouldn't finish correctly. It might not just be ping info but some other issue. That is why I have housed each script in the same folder as the excel log document and just scheduled a task to run a main powershell script that fires off each of the individual server scripts. I did this so I wouldn't have a bunch of scheduled jobs. Just the one to fire off the rest.

I was wondering if you guys wouldn't mind critiquing my little script for proper format, best practices, readability, best and most appropriate solutions, and just plain good ol' usability.

 

Just to clarify, this is the way I have it set up. I have a scheduled job fire off a script that has nothing in it but the commands to fire off these individual scripts. Each script gets drive info and records it in its excel log each day.

I thought of housing this all in one script. One idea I had was to have one script and just pull the servername from the arguments when I call the script over and over from the master script I have fire off from the schedule. What do you guys think?

smurawskiUser is Offline
New Member
New Member
Posts:46

--
02 Dec 2008 02:23 PM  
Jason,

To answer your question about using -Filter and supplying the class, either way will work fine. Using -Query will allow you to return specific properties, which is important if you are querying a class with a lot of properties when you only need a few.

I don't think you are getting the isolation you believe you are by housing each script separately. I would make the computer name and log location into parameters and pass in that value. That way, if you make a change to the script, you don't have to change X number of files.

param ( $computer = "AAMCMLM1", $logLocation = "I:\Jason Stephens\AAMCMLM1\drive_log.xls" )


Then your master script could be something as simple as
 <br /> param($ServerFile = 'C:\servers.txt') <br /> $Servers = Get-Content $ServerFile <br />  <br /> foreach ($Server in $Servers) <br /> { <br />   ./drive_log.ps1 -computer $server <br /> } <br /> 


Also, since you are calling them all from a master script, unless you are kicking off separate processes to run them, you still run into a chance of blocking with a failed WMI query. I would move the WMI query before you even create the COM object for Excel; that way, if your WMI query fails, you don't have to deal with any weirdness from Excel.

If you are launching the scripts via separate processes (to prevent blocking due to a failed WMI query), you will have to watch access to the Excel file (if they are all writing to the same file).

I did not see where you were logging the volume name. Are you?

Did you mean to have the $size/$free? Reversing that will give you a percentage of free space.

Finally, I would also log both the Free Space and Size. Since I grabbed that info, it doesn't hurt to log it and might come in useful.

Polymon (open source, free, systems monitoring http://www.codeplex.com/polymon) has disk monitoring built in.
Steven Murawski
Co-Host - Mind of Root (www.mindofroot.com)
Host - PowerShell Basics (powershell-basics.com)
jason_stephensUser is Offline
New Member
New Member
Posts:15
Avatar

--
26 Dec 2008 05:16 PM  

First, thank you for your reply.

I am wanting to kind of use this as a learning tool as well as a useful script to help me document my servers. Should I be placing this into a CSV file, HTML file, XML file, or just use excel like I have been. Or should I dump them into a table in MS SQL? What is your opinion on the best and proper way to log this information?

Things I should of done include using parameters instead of multiple scripts, output as an object, ping check servers first, open excel after I have drive information, and use some sort of continue on error. At least those are some of my observations along with the ones you pointed out. Can you think of any others?

I don't have much experience with XML but that is what I am thinking I should log it in. Am I right on thinking that? Maybe create a new XML file for each year and then write an HTML viewer page to check by year and server?
I am really curious what the best and proper way to log this kind of information would be using best practices and efficient code.

PS...to answer some of the questions you had...
1. They each have their own excel log they write to.
2. My intention is to save both the drive size and drive free space and not the percentage.
3. I launch a PS script via task scheduler on a computer behind my desk that runs each server's script.

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer