Still very much a PowerCLI beginner but im trying to get a little smarter with my scripts. Currently i have hard coded a list of clusters e.g.
function TRF-Select-Cluster
{
param (
[string]$SCLUTitle = 'Select Cluster'
)
Clear-Host
Write-Host "================ $SCLUTitle ================"
Write-Host "1: Cluster 1."
Write-Host "2: Cluster 2"
Write-Host "3: Cluster 3"
Write-Host "4: Cluster 4."
Write-Host "5: Cluster 5."
Write-Host "Q: Press 'Q' to return to the previous menu."
}
Followed by
function TRF-Choice-Select-Cluster
{
TRF-Select-Cluster –Title 'Select Cluster'
$selection = Read-Host "Please make a selection"
switch ($selection)
{
'1' {
$CLUChoice = "Cluster 1"
'You chose Cluster 1'
TRF-Choice-Select-Cluster-Commands
} '2' {
$CLUChoice = "Cluster 2"
'You chose Cluster 2'
TRF-Choice-Select-Cluster-Commands
} '3' {
$CLUChoice = "Cluster 3"
'You chose Cluster 3'
TRF-Choice-Select-Cluster-Commands
} '4' {
$CLUChoice = "Cluster 4"
'You chose Cluster 4'
TRF-Choice-Select-Cluster-Commands
} '5' {
$CLUChoice = "Cluster 5"
'You chose Cluster 5'
TRF-Choice-Select-Cluster-Commands
} 'q' {
TRF-Choice-Select-Datacenter
}
}
}
I feel like this is super inefficient and im sure there is a way of creating a list of clusters then creating a selectable menu from the results in significantly fewer lines and more importantly a way of me not having to do this for every single datacenter.... is anyone able to give me some tips?
I just need to be able to make a selection, that selection get written to variable $CLUChoice and then redirected to function TRF-Choice-Select-Cluster-Commands
Thanks!
That should of course be $selection instead of $i
Write-Host "================ Select Cluster ================"
$i = 1
ForEach-Object -InputObject $clusters -Process {
Write-Host "$i $($_.Name)"
$i++
}
$selection = Read-Host "Please make a selection"
if($selection -eq 'q'){
TRF-Choice-Select-Datacenter
}
else{
$CLUChoice = $clusters[$selection -1].Name
Write-Host "You chose cluster $CLUChoice"
TRF-Choice-Select-Cluster-Commands
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Would something like this work?
Write-Host "================ Select Cluster ================"
$i = 1
ForEach-Object -InputObject $clusters -Process {
Write-Host "$i $($_.Name)"
$i++
}
$selection = Read-Host "Please make a selection"
if($selection -eq 'q'){
TRF-Choice-Select-Datacenter
}
else{
$CLUChoice = $clusters[$i -1].Name
Write-Host "You chose cluster $CLUChoice"
TRF-Choice-Select-Cluster-Commands
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the prompt response. Looks pretty closed although every object is on the same line, see below
PS H:\> TRF-testmenu
================ Select Cluster ================
1 Cluster 1 Cluster 2 Cluster 3 Cluster 4 Cluster 5
Please make a selection:
im guessing the counter isnt incrementing?
Indeed, I forgot to copy in the $i++.
The code above is corrected
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ok so that was giving me the same output, i made a small change:
$clusters = Get-Cluster
Write-Host "================ Select Cluster ================"
$i = 1
$clusters | ForEach-Object -Process {
Write-Host "$i $($_.Name)"
$i++
}
$selection = Read-Host "Please make a selection"
if($selection -eq 'q'){
TRF-Choice-Select-Datacenter
}
else{
$CLUChoice = $clusters[$i -1].Name
Write-Host "You chose cluster $CLUChoice"
}
This gives me the menu below:
================ Select Cluster ================
1 Cluster 1
2 Cluster 2
3 Cluster 3
4 Cluster 4
5 Cluster 5
Please make a selection: 1
You chose cluster
however after making a selection it doesnt seem to have populated the $CLUChoice variable, it just states
You chose cluster
That should of course be $selection instead of $i
Write-Host "================ Select Cluster ================"
$i = 1
ForEach-Object -InputObject $clusters -Process {
Write-Host "$i $($_.Name)"
$i++
}
$selection = Read-Host "Please make a selection"
if($selection -eq 'q'){
TRF-Choice-Select-Datacenter
}
else{
$CLUChoice = $clusters[$selection -1].Name
Write-Host "You chose cluster $CLUChoice"
TRF-Choice-Select-Cluster-Commands
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So when the $CLUChoice variable is initially called during write-host it gives the correct response however if you try to view that variable afterwards it gives a completely different result... odd!
PS H:\> TRF-testmenu4
================ Select Cluster ================
1 Cluster 1
2 Cluster 2
3 Cluster 3
4 Cluster 4
5 Cluster 5
Please make a selection: 1
You chose cluster Cluster 1
PS H:\> $CLUChoice
Cluster 5
PS H:\> TRF-testmenu4
================ Select Cluster ================
1 Cluster 1
2 Cluster 2
3 Cluster 3
4 Cluster 4
5 Cluster 5
Please make a selection: 2
You chose cluster Cluster 2
PS H:\> $CLUChoice
Cluster 5
PS H:\> TRF-testmenu4
================ Select Cluster ================
1 Cluster 1
2 Cluster 2
3 Cluster 3
4 Cluster 4
5 Cluster 5
Please make a selection: 3
You chose cluster Cluster 3
PS H:\> $CLUChoice
Cluster 5
PS H:\>
Fixed it! Needed to use a global variable as i was trying to access it outside of the function
function TRF-testmenu4
{
$clusters = Get-datacenter $DCChoice | Get-Cluster
Write-Host "================ Select Cluster ================"
$i = 1
$clusters | ForEach-Object -Process {
Write-Host "$i $($_.Name)"
$i++
}
$selection = Read-Host "Please make a selection"
if($selection -eq 'q'){
TRF-Choice-Select-Datacenter
}
else{
$global:CLUChoice = $clusters[$selection -1].Name
Write-Host "You chose cluster $CLUChoice"
TRF-Choice-Select-Cluster-Commands
}
}
Thanks for all your help!