Playing with PowerShell and VMware is a lot of fun. We have multiple VMware environments at work, so I longed for a simple menu to stick in front of some scripts I used for various reports against those environments. After some searches, I came up with the Windows PowerShell Tip of the Week archives under 'User Input' from the Script Center. It had some interesting suggestions, some fancy, some to simple for my use. I longed for the old text base menus that were simple to toss together and easy for neophytes to understand.
I had this vague recollection of select .. case in VBScript so on the VBScript to Windows PowerShell Conversion Guide I followed the link to VBScript commands and then to Select .. case.
Here I found a nice little, short article on switch. It looked simple enough and a quick check in my PowerShell console showed that Get-Help About_Switch had some more stuff. I still have to play with it some more but now I have my nice, simple menu to authenticate to the different environments with one script.
Having multiple environments but wanting to generate a nice space utilization report to check here's what I came up with. Something simple and basic that is understandable by myself and my co-workers and outputs a report. Here is an edited version of the script.
# NAME: get-VMspace.ps1
# AUTHOR: Steven Peck
# DATE: 7/7/2008
# COMMENT: Get LUN space from VMHosts attached to Virtual Centers
# Sort by LUN Name and -unique
# ==============================================================================================
# Set report output directory
$outputDir = "C:\support\Output"
# Set date format for use in file name
$date = Get-Date -uformat "%y%m%d"
# Explain what the script does
Write-Host " "
Write-Host "This script will scan all attached storage on VMHost systems connected to a given Virtual Center server."
Write-Host " "
# Select environment MENU
Write-Host "Choose your environment."
Write-Host "1. Production"
Write-Host "2. Test"
Write-Host "3. Development"
Write-Host " "
$a = Read-Host "Select 1-3: "
Write-Host " "
switch ($a)
{
1 {
"** Production Environment **";
Get-VIServer -server SERVER001 -protocol https -user speck;
$environment = "Production";
break;
}
2 {
"**Test environment **";
Get-VIServer -server 10.10.10.125 -protocol https -user domain\speck;
$environment = "test";
break;
}
3 {
"** Development environment **";
Get-VIServer -server 192.168.5.5 -protocol https -user domain\speck;
$environment = "dev";
break;
}
default {
"** The selection could not be determined **";
break;
}
}
$space = Get-VMHost | Get-Datastore
$space | sort Name -unique | Select-Object Name, CapacityMB, FreeSpaceMB | Export-Csv $outputDir/$date.$environment.space.csv -NoType
Write-Host "Reports are located in $outputDir."
A couple of notes.
- We have several clustered servers and some stand alone for Lab Manager so querying the cluster itself didn't give all the environments.
- We authenticate to different environments with different accounts. I could have made a variable to set the various user id's near the top, I still may do that.
- Need to figure out how to export to tabs in an Excel spreadsheet. Something involving New-Object and Add-Object I suspect. Not having found PowerShell examples I think I may have to figure it out through VBScript examples.
- I should be able to do some math and insert a column for percent free space.
- I need to figure out how to make the default end the script and not continue and error on the next lines.
All in all, I rather like this script. I shall probably try the fancier options on the tips site later but this is much more understandable in the beginning.
Comments
You should check out
You should check out $Host.UI.PromptForChoice
I had seen an example using
I had seen an example using that code before but couldn't figure out how it worked for more then two choices much less that the promptforchoice was the part that made it work. The Microsoft Tip article just didn't have enough details. Using specifically your mentioned $Host.UI.PromptForChoice on the search, I found a more detailed article.
So far I like the limiting input part but not the hot key display behavior. I realize I could set the hot key to the first letter of the environment but the example code doesn't match my actual one. Still, even with the number oddity in the display it still is nice. I will continue experimenting with it.
I will post an update as soon as I get it figured out how to make the output look like what I want.
Thanks...
$outputDir = "C:\support\Output"
# Set date format for use in file name
$date = Get-Date -uformat "%y%m%d"
# Explain what the script does
Write-Host " "
Write-Host "This script will scan all attached storage on VMHost systems connected to a given Virtual Center server."
Write-Host " "
# Select environment MENU
Write-Host "Choose your environment."
Write-Host "1. Production"
Write-Host "2. Test"
Write-Host "3. Development"
Write-Host " "
$title = "Select VMware environment"
$message = "Which environment to run the script against?"
$1 = New-Object System.Management.Automation.Host.ChoiceDescription "&1 Production", `
"Production."
$2 = New-Object System.Management.Automation.Host.ChoiceDescription "&2 Test", `
"Test"
$3 = New-Object System.Management.Automation.Host.ChoiceDescription "&3 Development", `
"Development"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($1, $2, $3)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {
"** Production Environment **";
Get-VIServer -server SERVER001 -protocol https -user speck;
$environment = "Production";
break;
}
1 {
"**Test environment **";
Get-VIServer -server 10.10.10.125 -protocol https -user domain\speck;
$environment = "test";
break;
}
2 {
"** Development environment **";
Get-VIServer -server 192.168.5.5 -protocol https -user domain\speck;
$environment = "dev";
break;
}
}
$space = Get-VMHost | Get-Datastore
$space | sort Name -unique | Select-Object Name, CapacityMB, FreeSpaceMB | Export-Csv $outputDir/$date.$environment.space.csv -NoType
Write-Host "Reports are located in $outputDir."