I wanted a way to choose what my PowerShell session would connect to when starting up. At various times I need to connect to on-premises production systems, to lab systems, or to an Office 365 tenant.
Editing my profile each time I wanted to connect to something different became time consuming. So I figured out how to add menu choices to my profile with some .NET objects. This gave me the ability to choose the resources I wanted to connect to, and also quickly start up multiple PowerShell sessions.
For those of you who don’t know where to find your profile, or haven’t created one yet, type $profile at the PowerShell prompt. It will show you where it expects to find the file.
The following is a template to create the choices, and then use one or more IF statements to act upon the choice.
Create the Title and Description
$resourceTitle = "Your Eye Catching Title Goes Here" $resourceDesc = "Your obvious description about the resource goes here."
Define and Present the Choices
Consider using descriptive variable names for your choices. For example, Yes, No, All, Individual, Exit.
The “&” symbol before a letter defines it as the keystroke to use for that choice. For example, “E&xit” presents [X] as an option to choose Exit, and “&Yes” presents [Y] as an option to choose Yes.
The second line of each defined choice is shown when “?” is pressed for help.
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", ` "Connect to the resource." $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", ` "Do not connect to the resource." $exit = New-Object System.Management.Automation.Host.ChoiceDescription "E&xit", ` "Exit to PS Prompt."
This line puts all of the choices together into a variable:
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no, $exit)
The following line prompts for the choice, and stores the result into a variable. The number at the end defines which choice is considered the default, and the first choice starts with 0 (zero). In this example “Yes” is the default.
$resourceResult = $host.ui.PromptForChoice($resourceTitle, $resourceDesc, $options, 0)
I’ve Made My Choice. Now What Happens?
Here are a couple of examples of what could happen.
## Exit to the PowerShell Prompt If ($resourceResult -eq 2) { write-host; write-host "BYE!"; write-host; Exit }
## Connect to the Resource If ($resourceResult -eq 0) { Write-Host; Write-Host "Prompting for Credentials..." $resourceCredential = Get-Credential "username@contoso.com" Write-Host "Connecting..."; Write-Host ## Connect to O365 Import-Module MsOnline Connect-MsolService -Credential $resourceCredential ## Connect to Exchange Online $Session = New-PSSession -ConfigurationName Microsoft.Exchange ` -ConnectionUri https://outlook.office365.com/powershell-liveid/ ` -Credential resourceCredential -Authentication Basic -AllowRedirection Import-PSSession $Session }
An Example from Start to Finish
This example asks if you want to connect to all resources, to individual resources, or to exit. If you choose individual resources, you are then asked if you want to connect to each individual resource.
$myResTitle = "MY RESOURCES" $myResDesc = "Connect to All or Individual Resources." $all = New-Object System.Management.Automation.Host.ChoiceDescription "&All", ` "Connect to all resources." $ind = New-Object System.Management.Automation.Host.ChoiceDescription "&Individual", ` "Connect to individual resources." $exit = New-Object System.Management.Automation.Host.ChoiceDescription "E&xit", ` "Exit to PS Prompt." $options = [System.Management.Automation.Host.ChoiceDescription[]]($all, $ind, $exit) $myResResult = $host.ui.PromptForChoice($myResTitle, $myResDesc, $options, 0) If ($myResResult -eq 2) { write-host; write-host "BYE!"; write-host; Exit } #### Connect to all resources #### If ($myResResult -eq 0) { Write-Host; Write-Host "Prompting for Credentials..." ## My Domain Admin Credentials $DAcredential = Get-Credential "dauser@contoso.com" ## My Regular Login Credentials $credential = Get-Credential "user@contoso.com" Write-Host "Connecting..."; Write-Host ## Connect to Exchange 2010 On-Prem $ExSession = New-PSSession -ConfigurationName Microsoft.Exchange ` -ConnectionUri "http://exsrv1.contoso.com/PowerShell" -Credential $credential Import-PSSession $ExSession Set-AdServerSettings -ViewEntireForest $True ## Connect to Active Directory $ADSession = New-PSSession -computerName dc01.contoso.com -Credential $DAcredential Invoke-Command { Import-module activedirectory } -Session $ADSession Import-PSSession $ADSession -module activedirectory ## Connect to O365 Tenant Import-Module MsOnline Connect-MsolService -Credential $credential ## Connect to Lync Online Import-Module LyncOnlineConnector $lyncSession = New-CsOnlineSession -Credential $credential Import-PSSession $lyncSession } #### Connect to individual resources #### If ($myResResult -eq 1) { Write-Host; Write-Host "Prompting for Credentials..."; Write-Host ## My Domain Admin Credentials $DAcredential = Get-Credential "dauser@contoso.com" ## My Regular Login Credentials $credential = Get-Credential "user@contoso.com" ## Connect to Exchange $myResTitle = "MY EXCHANGE 2010 ON-PREM" $myResDesc = "Connect to my Exchange on-prem environment." $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", ` "Connect to Exchange." $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", ` "Do not connect to Exchange." $exit = New-Object System.Management.Automation.Host.ChoiceDescription "E&xit", ` "Exit to PS Prompt." $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no, $exit) $myExResult = $host.ui.PromptForChoice($myResTitle, $myResDesc, $options, 0) If ($myExResult -eq 2) { write-host; write-host "BYE!"; write-host; Exit } If ($myExResult -eq 0) { $ExSession = New-PSSession -ConfigurationName Microsoft.Exchange ` -ConnectionUri "http://exsrv1.contoso.com/PowerShell" -Credential $credential Import-PSSession $ExSession Set-AdServerSettings -ViewEntireForest $True } ## Connect to Active Directory $myResTitle = "MY ACTIVE DIRECTORY" $myResDesc = "Connect to my Active Directory Environment." $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", ` "Connect to my Active Directory." $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", ` "Do not connect to my Active Directory." $exit = New-Object System.Management.Automation.Host.ChoiceDescription "E&xit", ` "Exit to PS Prompt." $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no, $exit) $myAdResult = $host.ui.PromptForChoice($myResTitle, $myResDesc, $options, 0) If ($myAdResult -eq 2) { write-host; write-host "BYE!"; write-host; Exit } If ($myAdResult -eq 0) { $ADSession = New-PSSession -computerName dc01.contoso.com -Credential $DAcredential Invoke-Command { Import-module activedirectory } -Session $ADSession Import-PSSession $ADSession -module activedirectory } ## Connect to Office 365 $myResTitle = "MY OFFICE 365" $myResDesc = "Connect to my Office 365 tenant." $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", ` "Connect to my Office 365 tenant." $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", ` "Do not connect to my Office 365 tenant." $exit = New-Object System.Management.Automation.Host.ChoiceDescription "E&xit", ` "Exit to PS Prompt." $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no, $exit) $myO365result = $host.ui.PromptForChoice($myResTitle, $myResDesc, $options, 0) If ($myO365result -eq 2) { write-host; write-host "BYE!"; write-host; Exit } If ($myO365result -eq 0) { ## Connect to O365 Import-Module MsOnline Connect-MsolService -Credential $credential ## Connect to Lync Online Import-Module LyncOnlineConnector $lyncSession = New-CsOnlineSession -Credential $credential Import-PSSession $lyncSession } } Write-Host ; Write-Host "All Done." ; Write-Host