VMware Cloud Community
deryambn
Enthusiast
Enthusiast
Jump to solution

How to add another choice in the script which ask me the options again

Hello,

I found a script which asks me choose an option about lock down mode options on a cluster. When the script finishes there is an option to exit script. But i want to ask me choose another cluster name and do the options again for that cluster. Could you hep me?

Especially @LucD İ wanna ask your help.

Thanks

The script:

##################################################
# Script: configure-ssh-lockdown-mode-in-a-cluster.ps1
# Author: Doug DeFrank
# Date: 2017-10-14
#
# Purpose: Configure SSH, Lockdown mode for all hosts in a cluster
##################################################

Write-Host `n "This script will allow a user to enable or disable SSH as well as Lockdown Mode for all hosts in a cluster."

### Prompt user for vCenter Server name, and connect to it
$vCenterServer = Read-Host -Prompt 'Enter the FQDN of the vCenter Server you want to connect to (ex. vcenter.domain.com)'

### This Try/Catch statement will stop the script if a vCenter Server doesn't exist, a bad username/password is entered, etc.
Try {
Connect-VIServer -Server $vCenterServer -ErrorAction Stop | Out-Null
}

Catch {
Write-Host -ForegroundColor Red -BackgroundColor Black "Could not connect to the vCenter Server [$vCenterServer]." `n
Exit
}

### Prompt the user for the cluster name within that vCenter Server
$ClusterName = Read-Host -Prompt 'Enter the full name of the cluster you want to work with (ex. ProdCluster01)'

### Get information about all hosts in the defined cluster
$vmHosts = Get-Cluster -Name $ClusterName | Get-VMHost | Sort-Object

$cont = ""

Do {
### Ask the user what step to perform
Write-Host `n
Write-Host "Please enter a task to perform on each host in the cluster:"
Write-Host "1.) Enable SSH"
Write-Host "2.) Disable Lockdown Mode"
Write-Host "3.) Disable SSH"
Write-Host "4.) Enable Lockdown Mode"
Write-Host "5.) Exit" `n
$choice = Read-Host

### Perform a particular task based on user input
Switch ($choice) {

### If task #1 is chosen, Enable SSH on all hosts in the cluster
1 {
Write-Host -ForegroundColor Yellow `n "Enabling SSH on all hosts in the $ClusterName cluster."
ForEach ($vmHost in $vmHosts) {
Start-VMHostService -HostService ($vmHost | Get-VMHostService | Where-Object {$_.key -eq "TSM-SSH"}) -Confirm:$false | Select-Object VMHost,Key,Label,Running
}
$cont = $true
}

### If task #2 is chosen, Disable Lockdown Mode on all hosts in the cluster
2 {
Write-Host -ForegroundColor Yellow `n "Disabling Lockdown Mode on all hosts in the $ClusterName cluster."
ForEach ($vmHost in $vmHosts) {
($vmHost | Get-View).ExitLockdownMode()
}
$cont = $true
}

### If task #3 is chosen, Disable SSH on all hosts in the cluster
3 {
Write-Host -ForegroundColor Yellow `n "Disabling SSH on all hosts in the $ClusterName cluster."
ForEach ($vmHost in $vmHosts) {
Stop-VMHostService -HostService ($vmHost | Get-VMHostService | Where-Object {$_.key -eq "TSM-SSH"}) -Confirm:$false | Select-Object VMHost,Key,Label,Running
}
$cont = $true
}

### If task #4 is chosen, Enable Lockdown Mode on all hosts in the cluster
4 {
Write-Host -ForegroundColor Yellow `n "Enabling Lockdown Mode on all hosts in the $ClusterName cluster."
ForEach ($vmHost in $vmHosts) {
($vmHost | Get-View).EnterLockdownMode()
}
$cont = $true
}

### If task #5 is chosen, exit the script
5 {
Write-Host "Exiting..."
$cont = $false
}

### If user enters anything other than 1-5, input is invalid and ask question again
default {
Write-Host -ForegroundColor Red ">>> Invalid input. Please select option 1-5."
$cont = $true
}
}
}

### Loop through the script until task #5 (Exit) is chosen
While ($cont)

### Disconnect from the vCenter Server
Disconnect-VIServer -Server $vCenterServer -Confirm:$false | Out-Null

0 Kudos
1 Solution

Accepted Solutions
Macleud
Enthusiast
Enthusiast
Jump to solution

Hi.

You can do this using a loop While. By putting some code in it.

 

 

##################################################
# Script: configure-ssh-lockdown-mode-in-a-cluster.ps1
# Author: Doug DeFrank
# Date: 2017-10-14
#
# Purpose: Configure SSH, Lockdown mode for all hosts in a cluster
##################################################

Write-Host `n "This script will allow a user to enable or disable SSH as well as Lockdown Mode for all hosts in a cluster."

### Prompt user for vCenter Server name, and connect to it
$vCenterServer = Read-Host -Prompt 'Enter the FQDN of the vCenter Server you want to connect to (ex. vcenter.domain.com)'

### This Try/Catch statement will stop the script if a vCenter Server doesn't exist, a bad username/password is entered, etc.
Try {
Connect-VIServer -Server $vCenterServer -ErrorAction Stop | Out-Null
}

Catch {
Write-Host -ForegroundColor Red -BackgroundColor Black "Could not connect to the vCenter Server [$vCenterServer]." `n
Exit
}

while ($True){
    ### Prompt the user for the cluster name within that vCenter Server
    $ClusterName = Read-Host -Prompt 'Enter the full name of the cluster you want to work with (ex. ProdCluster01)'

    ### Get information about all hosts in the defined cluster
    $vmHosts = Get-Cluster -Name $ClusterName | Get-VMHost | Sort-Object

    $cont = ""
    $contR = ""

    Do {
    ### Ask the user what step to perform
    Write-Host `n
    Write-Host "Please enter a task to perform on each host in the cluster:"
    Write-Host "1.) Enable SSH"
    Write-Host "2.) Disable Lockdown Mode"
    Write-Host "3.) Disable SSH"
    Write-Host "4.) Enable Lockdown Mode"
    Write-Host "5.) Next Cluster"
    Write-Host "6.) Exit" `n
    $choice = Read-Host

    ### Perform a particular task based on user input
    Switch ($choice) {

    ### If task #1 is chosen, Enable SSH on all hosts in the cluster
    1 {
    Write-Host -ForegroundColor Yellow `n "Enabling SSH on all hosts in the $ClusterName cluster."
    ForEach ($vmHost in $vmHosts) {
    Start-VMHostService -HostService ($vmHost | Get-VMHostService | Where-Object {$_.key -eq "TSM-SSH"}) -Confirm:$false | Select-Object VMHost,Key,Label,Running
    }
    $cont = $true
    }

    ### If task #2 is chosen, Disable Lockdown Mode on all hosts in the cluster
    2 {
    Write-Host -ForegroundColor Yellow `n "Disabling Lockdown Mode on all hosts in the $ClusterName cluster."
    ForEach ($vmHost in $vmHosts) {
    ($vmHost | Get-View).ExitLockdownMode()
    }
    $cont = $true
    }

    ### If task #3 is chosen, Disable SSH on all hosts in the cluster
    3 {
    Write-Host -ForegroundColor Yellow `n "Disabling SSH on all hosts in the $ClusterName cluster."
    ForEach ($vmHost in $vmHosts) {
    Stop-VMHostService -HostService ($vmHost | Get-VMHostService | Where-Object {$_.key -eq "TSM-SSH"}) -Confirm:$false | Select-Object VMHost,Key,Label,Running
    }
    $cont = $true
    }

    ### If task #4 is chosen, Enable Lockdown Mode on all hosts in the cluster
    4 {
    Write-Host -ForegroundColor Yellow `n "Enabling Lockdown Mode on all hosts in the $ClusterName cluster."
    ForEach ($vmHost in $vmHosts) {
    ($vmHost | Get-View).EnterLockdownMode()
    }
    $cont = $true
    }

    ### If task #5 is chosen, next cluster
    5 {
        Write-Host "Next Cluster..."
        $cont = $false
        $contR = $false
        }

    ### If task #6 is chosen, exit the script
    6 {
    Write-Host "Exiting..."
    $cont = $false
    $contR = $True
    }

    ### If user enters anything other than 1-5, input is invalid and ask question again
    default {
    Write-Host -ForegroundColor Red ">>> Invalid input. Please select option 1-6."
    $cont = $true
    $contR = $false
    }
    }
    }

    ### Loop through the script until task #6 (Exit) is chosen
    While ($cont)

    if ($contR) {
      break 
    }
}

### Disconnect from the vCenter Server
Disconnect-VIServer -Server $vCenterServer -Confirm:$false | Out-Null

 

 

 

View solution in original post

0 Kudos
2 Replies
Macleud
Enthusiast
Enthusiast
Jump to solution

Hi.

You can do this using a loop While. By putting some code in it.

 

 

##################################################
# Script: configure-ssh-lockdown-mode-in-a-cluster.ps1
# Author: Doug DeFrank
# Date: 2017-10-14
#
# Purpose: Configure SSH, Lockdown mode for all hosts in a cluster
##################################################

Write-Host `n "This script will allow a user to enable or disable SSH as well as Lockdown Mode for all hosts in a cluster."

### Prompt user for vCenter Server name, and connect to it
$vCenterServer = Read-Host -Prompt 'Enter the FQDN of the vCenter Server you want to connect to (ex. vcenter.domain.com)'

### This Try/Catch statement will stop the script if a vCenter Server doesn't exist, a bad username/password is entered, etc.
Try {
Connect-VIServer -Server $vCenterServer -ErrorAction Stop | Out-Null
}

Catch {
Write-Host -ForegroundColor Red -BackgroundColor Black "Could not connect to the vCenter Server [$vCenterServer]." `n
Exit
}

while ($True){
    ### Prompt the user for the cluster name within that vCenter Server
    $ClusterName = Read-Host -Prompt 'Enter the full name of the cluster you want to work with (ex. ProdCluster01)'

    ### Get information about all hosts in the defined cluster
    $vmHosts = Get-Cluster -Name $ClusterName | Get-VMHost | Sort-Object

    $cont = ""
    $contR = ""

    Do {
    ### Ask the user what step to perform
    Write-Host `n
    Write-Host "Please enter a task to perform on each host in the cluster:"
    Write-Host "1.) Enable SSH"
    Write-Host "2.) Disable Lockdown Mode"
    Write-Host "3.) Disable SSH"
    Write-Host "4.) Enable Lockdown Mode"
    Write-Host "5.) Next Cluster"
    Write-Host "6.) Exit" `n
    $choice = Read-Host

    ### Perform a particular task based on user input
    Switch ($choice) {

    ### If task #1 is chosen, Enable SSH on all hosts in the cluster
    1 {
    Write-Host -ForegroundColor Yellow `n "Enabling SSH on all hosts in the $ClusterName cluster."
    ForEach ($vmHost in $vmHosts) {
    Start-VMHostService -HostService ($vmHost | Get-VMHostService | Where-Object {$_.key -eq "TSM-SSH"}) -Confirm:$false | Select-Object VMHost,Key,Label,Running
    }
    $cont = $true
    }

    ### If task #2 is chosen, Disable Lockdown Mode on all hosts in the cluster
    2 {
    Write-Host -ForegroundColor Yellow `n "Disabling Lockdown Mode on all hosts in the $ClusterName cluster."
    ForEach ($vmHost in $vmHosts) {
    ($vmHost | Get-View).ExitLockdownMode()
    }
    $cont = $true
    }

    ### If task #3 is chosen, Disable SSH on all hosts in the cluster
    3 {
    Write-Host -ForegroundColor Yellow `n "Disabling SSH on all hosts in the $ClusterName cluster."
    ForEach ($vmHost in $vmHosts) {
    Stop-VMHostService -HostService ($vmHost | Get-VMHostService | Where-Object {$_.key -eq "TSM-SSH"}) -Confirm:$false | Select-Object VMHost,Key,Label,Running
    }
    $cont = $true
    }

    ### If task #4 is chosen, Enable Lockdown Mode on all hosts in the cluster
    4 {
    Write-Host -ForegroundColor Yellow `n "Enabling Lockdown Mode on all hosts in the $ClusterName cluster."
    ForEach ($vmHost in $vmHosts) {
    ($vmHost | Get-View).EnterLockdownMode()
    }
    $cont = $true
    }

    ### If task #5 is chosen, next cluster
    5 {
        Write-Host "Next Cluster..."
        $cont = $false
        $contR = $false
        }

    ### If task #6 is chosen, exit the script
    6 {
    Write-Host "Exiting..."
    $cont = $false
    $contR = $True
    }

    ### If user enters anything other than 1-5, input is invalid and ask question again
    default {
    Write-Host -ForegroundColor Red ">>> Invalid input. Please select option 1-6."
    $cont = $true
    $contR = $false
    }
    }
    }

    ### Loop through the script until task #6 (Exit) is chosen
    While ($cont)

    if ($contR) {
      break 
    }
}

### Disconnect from the vCenter Server
Disconnect-VIServer -Server $vCenterServer -Confirm:$false | Out-Null

 

 

 

0 Kudos
deryambn
Enthusiast
Enthusiast
Jump to solution

Thank you so much @Macleud . The script worked.

Tags (1)
0 Kudos