Tag: powershell

Time to do work?

When I was working at IBM there was a daily report that I issued on the state of the environment, drivespace, and crap like that.  I always told myself that if I ever had time I would automate much or all of the process as thoroughly as possible.  As time was not on my side… that never happened, and instead I browsed my computer for 4 different servers, and 32 different directories every working day just so I knew how much drivespace I *didn’t* have left.

Now that I don’t work for them anymore it is much easier to breathe and script a bit.  Here’s what 2 hours netted me:

# Returns pre-formatted list of drivespace available to all logical drives on given servers
# outputs list to text file

$output = “c:\scripting\mtptfs.txt”
del $output
$evs = @(”evs01″,”evs02″,”evs03″)

foreach ($name in $evs) {
gwmi -computer $name -class win32_volume -filter “drivetype=3″ |
sort-object Caption |
select-object SystemName, Caption,{[math]::round($_.Capacity /1GB, 2)},{[math]::round($_.freespace / 1GB, 2)} >> $output
}

That is a neat little powershell script.  It grabs info from a WMI object of each of my exchange virtual servers, and dumps some numbers to a text file, in a not so classy way.

After another 2 hours I ditched all of that and came to a decent, but not completely done final product:

###
###  Returns pre-formatted list of drivespace available to all
###  exchange virtual servers, and returns the information in a
###  programmatically generated excel spreadsheet
###



### CREATES NEW EXCEL FILE USING COM OBJECT
$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()

###SETS VARIABLE FOR WORKSHEETS
$p = 1

###CREATES ARRAY FOR WHICH SERVERS TO QUERY
$evs = @(”EVS01″,”EVS02″,”EVS03″)



###LOOP FOR CREATING 1 EXCEL SHEET FOR EACH EVS
foreach ($name in $evs) {

###WRITES COLUMN HEADERS ON EACH EXCEL SHEET
$Sheet = $Excel.WorkSheets.Item($p)
$Sheet.Cells.Item(1,1) = “Computer”
$Sheet.Cells.Item(1,2) = “Drive/MountPoint”
$Sheet.Cells.Item(1,3) = “TOTAL SIZE”
$Sheet.Cells.Item(1,4) = “FREE SPACE”

###CHANGES COLORS AND FONTS ON COLUMN HEADERS
$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True



###SETS CELL STARTING POINT FOR WRITING EVS & DRIVESPACE INFORMATION
$intRow = 2

###POWERSHELL SCRIPT FOR GETTING WMI INFORMATION ABOUT ALL LOGICAL VOLUMES ON AN EVS
$colItems = gwmi -computer $name -class win32_volume -filter “drivetype=3″ | sort-object Caption

###LOOP FOR WRITING WMI INFORMATION BY ROW
foreach ($objItem in $colItems) {
$Sheet.Cells.Item($intRow,1) = $objItem.SystemName
$Sheet.Cells.Item($intRow,2) = $objItem.Caption
$Sheet.Cells.Item($intRow,3) = $objItem.Capacity / 1GB
$Sheet.Cells.Item($intRow,4) = $objItem.freespace / 1GB
$intRow = $intRow + 1

}

###CHANGES WORKBOOK TO NEXT SHEET
$p = $p + 1

###ADJUSTS COLUMN WIDTH FOR SHEET AUTOMATICALLY
$WorkBook.EntireColumn.AutoFit()

###FORMATS THE CAPACITY INFORMATION INTO INTEGERS WITH TWO DECIMAL PLACES
$Sheet.Cells.NumberFormat = “#.00″

}

This script is much nicer.  It creates a new excel file using the COM object, and dumps each EVS to its own sheet.  This comes complete with colors.  I have also included the pictures, circles, and arrows on the back of each one, and a paragraph describing what each one was.

I didn’t finish this completely because I ran out of time trying to get the borders completed.

All that being said my 2nd week with powershell is actually a good experience.