New User Script Choice Menu

I’m looking to create a new user script that displays a choice menu asking which company the new starter works for, then, depending on the selection, I’d like 2 outcomes. So, if they select Company1 it will set their domain to @company1.com and then move them to the Company1 OU. If they select Company2, it will set their domain to @company2.com and then move them to the Company2 OU etc. I have the menu sorted, I’m just not sure how to use the input after the menu to complete the above.

function Show-Menu
{
param (
[string]$Title = 'What company does the user work for?'
)
cls
Write-Host "$Title"

Write-Host "Press '1' for Company1."
Write-Host "Press '2' for Company2."
Write-Host "Press '3' for Company3."
Write-Host "Press '4' for Company4."
}

Show-Menu
$input = Read-Host "Please make a selection"
switch ($input)
{
'1' {
cls
'You chose Company1'
} '2' {
cls
'You chose Company2'
} '3' {
cls
'You chose Company3'
} '4' {
cls
'You chose Company4'
}
}

<p class="_1qeIAgB0cPwnLhDF9XSiJM">Eventually, I will use the user inputs to create a new remote mailbox as per the below:</p>

New-RemoteMailbox -Name "$username" -Password $password -UserPrincipalName "$alias@$domain" -alias $alias -DisplayName "$username" -FirstName $firstname -LastName $lastname -OnPremisesOrganizationalUnit company/Azure/Organization/Company1 -PrimarySmtpAddress "$alias@$domain" -RemoteRoutingAddress "$alias@$domain"

<p class="_1qeIAgB0cPwnLhDF9XSiJM">For context, the starter script is below (I want to change it completely, but this is the starting point).</p>

#Connect to local Exchange
$session = new-pssession -configurationname microsoft.exchange -connectionuri http://server/Powershell/ -authentication kerberos
import-pssession $session

#Create new remote mailbox
$firstname = Read-Host "Enter User First Name"
$lastname = Read-Host "Enter User Last Name"
$domain = Read-Host "Enter Domain To Be Used"
$username="$($firstname) $($Lastname)"
$alias="$($firstname.ToLower()).$($lastname.ToLower())"
$password = Read-Host "Enter password" -AsSecureString
New-RemoteMailbox -Name "$username" -Password $password -UserPrincipalName "$alias@$domain" -alias $alias -DisplayNa

Assuming the domain may not match the company, it would be something like this:

function Show-Menu
{
param (
[string]$Title = 'What company does the user work for?'
)

    Write-Host "$Title"

    Write-Host "Press '1' for Company1."
    Write-Host "Press '2' for Company2."
    Write-Host "Press '3' for Company3."
    Write-Host "Press '4' for Company4."
}

Show-Menu
$input = Read-Host "Please make a selection"
switch ($input) {
    '1' {
        $domain = 'company1.com'
        $company = 'Company1'
    } '2' {
        $domain = 'company2.com'
        $company = 'Company2'
    } '3' {
        $domain = 'company3.com'
        $company = 'Company3'
    } '4' {
        $domain = 'company4.com'
        $company = 'Company4'
    }
}

#Connect to local Exchange
$session = new-pssession -configurationname microsoft.exchange -connectionuri http://server/Powershell/ -authentication kerberos
import-pssession $session

#Create new remote mailbox
$firstname = Read-Host "Enter User First Name"
$lastname = Read-Host "Enter User Last Name"
$password = Read-Host "Enter password" -AsSecureString

$username="$($firstname) $($Lastname)"
$alias="$($firstname.ToLower()).$($lastname.ToLower())"


$newMbxParams = @{
    Name = $username
    Password = $password 
    UserPrincipalName = "$alias@$domain"
    Alias = $alias 
    DisplayName = $username
    FirstName = $firstname 
    LastName = $lastname 
    OnPremisesOrganizationalUnit = "company/Azure/Organization/$Company"
    PrimarySmtpAddress = "$alias@$domain"
    RemoteRoutingAddress = "$alias@$domain"
}

$newMbxParams

New-RemoteMailbox @newMbxParams

Great script Rob. To make a recommendation I would do/while the show-menu / switch portion so you can ensure they select a valid option since it is required to continue.

 

Something like:

 

Do {

Show-Menu
$input = Read-Host "Please make a selection"
switch ($input) {
'1' {
$domain = 'company1.com'
$company = 'Company1'
} '2' {
$domain = 'company2.com'
$company = 'Company2'
} '3' {
$domain = 'company3.com'
$company = 'Company3'
} '4' {
$domain = 'company4.com'
$company = 'Company4'
}
}
} While ($domain -eq $null)

Thank you both for your help and suggestions, that’s exactly what I’m after.

Out of interest, Gorstag, how could I display an error message should they select a value that isn’t an option?

@Gorstag,

I figured the op was getting the menu from https://adamtheautomator.com/build-powershell-menu/ and it was not implemented on purpose

@sicfc125

When you use the loop you also need to add a way to get out of it (as Adam discusses in the above blog):

function Show-Menu
{
param (
[string]$Title = 'What company does the user work for?'
)

    Write-Host "$Title"

    Write-Host "Press '1' for Company1."
    Write-Host "Press '2' for Company2."
    Write-Host "Press '3' for Company3."
    Write-Host "Press '4' for Company4."
}

Do {

    Show-Menu
    $input = Read-Host "Please make a selection ('Q' to Quit)"

    switch ($input) {
        '1' {
            $domain = 'company1.com'
            $company = 'Company1'
        } 
        '2' {
            $domain = 'company2.com'
            $company = 'Company2'
        } 
        '3' {
            $domain = 'company3.com'
            $company = 'Company3'
        } 
        '4' {
            $domain = 'company4.com'
            $company = 'Company4'
        }
        'Q' {
            Write-Host 'Exiting loop' -ForegroundColor Yellow
        }
        default {
            Write-Host ('{0}That is not a valid option. Restarting options (add insult here because user cannot select 1-4){0}' -f [environment]::NewLine) -ForegroundColor Red
        }
    }
} While ($domain -eq $null -and $input.ToLower() -ne 'q')

Thank you, Rob. You’re correct, I found the menu on that site.

Appreciate your help.