From my previous post, I have a nice simple menu figured out and working and that was making my co-worker happy (Happy co-workers more likely to do favors so is a good thing). Our internal patch window for one of our VMware environments was coming up and my co-worker checked on the connected CD-Rom drives from the command line script I found for him so all the guest systems would vmotion properly. Watching over the shoulder I thought I could do a better job of that now.
Two goals.
- A menu to determine which of our environments to authenticate against (we have 5).
- Offer options to list or disconnect CD-Rom drives without exiting the script.
- Happy co-worker
... OK, that's three.
The menu I covered in my previous post with an improvement suggested by Jaykul in the comments.
To go with goal 2, I had a vague memory of 'do .. while'. A quick Get-Help about_ and I see about_while so Get-Help about_while gets me the nice little example I can use to wrap my code in.
$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 will report on and disconnect connected CD drives in VMware guest systems."
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 **";
Connect-VIServer -server SERVER001 -protocol https -user speck;
$environment = "Production";
break;
}
1 {
"**Test environment **";
Connect-VIServer -server 10.10.10.125 -protocol https -user domain\speck;
$environment = "test";
break;
}
2 {
"** Development environment **";
Connect-VIServer -server 192.168.5.5 -protocol https -user domain\speck;
$environment = "dev";
break;
}
}
# set while loop trigger value to 0
$val = 0;
# Start while loop
while ( $val -ne 3 ) {
# Options list
Write-Host "Report / Disconnect CD Drives."
Write-Host "1. List systems with connected CD Drives"
Write-Host "2. Disconnect systems with CD Drives"
Write-Host "3. End"
$title = "Select VMware environment"
$message = "Which environment to run the script against?"
$1 = New-Object System.Management.Automation.Host.ChoiceDescription "&1", `
"List connected CD Drives."
$2 = New-Object System.Management.Automation.Host.ChoiceDescription "&2", `
"Disconnect CD Drives."
$3 = New-Object System.Management.Automation.Host.ChoiceDescription "&3", `
"Quit"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($1, $2, $3)
$result = $Host.ui.PromptForChoice($title, $message, $options, 0)
Write-Host $1
switch ($result)
{
0 {
"** List Connected CD Drives **";
Write-Host " ";
get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" } } | select Name;
Write-Host " ";
}
1 {
"** Disconnect CD Drives **";
Write-Host " ";
Get-VM | Get-CDDrive | where { $_.ConnectionState.Connected -eq "true" } | Set-CDDrive -Connected:$FALSE -Confirm:$FALSE
Write-Host " ";
}
2 {
"** Quit **"
Write-Host " "
$val = 3;
}
}
}