Powershell – Get inventory/slots of Dell chassis/blades

Standard

Here’s a script which will get the contents of your Dell blade chassis and export the list to a CSV file. This can be very useful to get an overview of or update your list of servers. The script uses the Dell racadm command “getslotname” and “plink.exe” to connect to the chassis and retrieve the information. You must download plink.exe (part of the putty suite) for this script to work.
I have divided the script into three sections variables, functions and script main to simplify the structure of the script. Below I will walk through the script and explain how I wrote each section.

Variables

#VARIABLES
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath #path to the directory in which the script is run.
$plink = "$dir\plink.exe" #path to plink.exe in the script directory
$CSV = "$dir\SlotNames.csv" #results will be exported to a csv file with this path
#Parameters
$username = "some_user" #username used to login to the chassis
$password = "some_password" #password used to login to the chassis
$ChassisNames = @("cmc01","cmc02","cmc03","cmc04","cmc05","cmc06") #add your chassis names or IPs in this array like shown.

The comments in the code pretty much explain all the different variables. Just remember to fill out the #parameters section according to your needs before you run the script.

Functions

1. Function Get-SlotName

function Get-SlotName {
	param (
		$ChassisName,
		$slotNumber
	)
	$EXE = $plink    
		$arg1 = "-pw"
		$arg2 = "$password"
		$arg3 = "$username@$ChassisName"
		$arg4 = "getslotname -i $slotnumber"		
		$command = "$EXE $arg1 $arg2 $arg3 $arg4"
		$output = invoke-expression -Command $command
		return $output
}

This function takes two parameters $ChassisName and $SlotNumber. It then uses plink.exe to run the command “getslotname -i slotnumber” on the chassis and return the output.

Script Main

#SCRIPT MAIN
clear
$Slots = @()
$ChassisNames | % {
	$Chassis = $_
	Write-Host "Getting information from $Chassis.."
	for ($i =1; $i -le 16; $i++) {
		$Slot = "" | select "Chassis","Slot #","Slot Name"
		$Slot.Chassis = $Chassis
		$Slot."Slot #" = $i
		$Slot."Slot Name" = Get-SlotName -ChassisName $Chassis -slotNumber $i
		$Slots += $Slot
	}
}
$Slots | Export-Csv $CSV -NoTypeInformation -Force

In the script main I start by looping through the array of chassis names $ChassisNames. As each chassis has 16 slots I then add a nested for loop to iterate over the integers 1-16. For each slot number 1-16 I create a custom object $Slot with the properties “Chassis”, “Slot #” and “Slot Name”. I use the Get-SlotName function to get the slot name value and assign it to the custom object along with the chassis and slot number values which I already have. I then add the $Slot object to the $Slots array. Finally the $Slots array is exported to CSV.
That’s it! I have copied in the full script below. I hope you find it useful. Enjoy!

################################################################################################
##Script:			Get-DellChassisSlots.ps1
##
##Description:		Gets an inventory of the slots in the given Dell blade chassis' and exports
#+					the information to a csv file. Needs plink.exe in order to run (download and
#+					place in the script directory). 
##Created by:		Noam Wajnman
##Creation Date:	April 09, 2014
################################################################################################
#FUNCTIONS
function Get-SlotName {
	param (
		$ChassisName,
		$slotNumber
	)
	$EXE = $plink    
		$arg1 = "-pw"
		$arg2 = "$password"
		$arg3 = "$username@$ChassisName"
		$arg4 = "getslotname -i $slotnumber"		
		$command = "$EXE $arg1 $arg2 $arg3 $arg4"
		$output = invoke-expression -Command $command
		return $output
}
#VARIABLES
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath #path to the directory in which the script is run.
$plink = "$dir\plink.exe" #path to plink.exe in the script directory
$CSV = "$dir\SlotNames.csv" #results will be exported to a csv file with this path
#Parameters
$username = "some_user" #username used to login to the chassis
$password = "some_password" #password used to login to the chassis
$ChassisNames = @("cmc01","cmc02","cmc03","cmc04","cmc05","cmc06") #add your chassis names or IPs in this array like shown.
#SCRIPT MAIN
clear
$Slots = @()
$ChassisNames | % {
	$Chassis = $_
	Write-Host "Getting information from $Chassis.."
	for ($i =1; $i -le 16; $i++) {
		$Slot = "" | select "Chassis","Slot #","Slot Name"
		$Slot.Chassis = $Chassis
		$Slot."Slot #" = $i
		$Slot."Slot Name" = Get-SlotName -ChassisName $Chassis -slotNumber $i
		$Slots += $Slot
	}
}
$Slots | Export-Csv $CSV -NoTypeInformation -Force
Advertisements

4 thoughts on “Powershell – Get inventory/slots of Dell chassis/blades

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s