Hi guys,
Trying to build my PowerShell skills by working on a new starter script and looking to get a loop working that will prompt to ask if the new starter has a mobile number or not. Options are ‘y’ and ‘n’. If they select ‘y’ it asks for the number then stores it in a variable. If they select ‘n’ it will inform them that no mobile number has been added and move to the next part of the script. If they don’t select anything and hit enter, it will ask them to enter either ‘y’ or ‘n’.
Currently, the script will continue if I just press enter when it prompts for the mobile number. I’d like it to continue to prompt them until they select ‘y’ or ‘n’. How could I accomplish that? Thanks!
#Allows the script to run as admin even if PS isn't opened as admin
([switch]$Elevated)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated) {
# tried to elevate, did not work, aborting
}
else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
#Set the title of the window.
$host.ui.RawUI.WindowTitle = "New User Creation Script"
#Give the user a quick description
Write-Host
Write-Host
Write-Host "This script will create a new AD user and move them to the correct OU."
Write-Host
Write-Host
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."
Write-Host "Press '5' for Company5."
Write-Host "Press 'Q' to quit."
}
Do {
Show-Menu
$input = Read-Host "Please make a selection"
switch ($input) {
'1' {
$domain = 'company1.com'
$company = 'Company1'
$website = 'www.company1.com'
Write-Host
Write-Host -ForegroundColor Green "You have selected Company1"
Write-Host
} '2' {
$domain = 'company2.com'
$company = 'Company2'
$website = 'www.company2.com'
Write-Host
Write-Host -ForegroundColor Green "You have selected Company2"
Write-Host
} '3' {
$domain = 'company3.com'
$company = 'Company3'
$website = 'www.company3.com'
Write-Host
Write-Host -ForegroundColor Green "You have selected Company3"
Write-Host
} '4' {
$domain = 'company4.co.uk'
$company = 'Company4'
$website = 'www.company4.co.uk'
Write-Host
Write-Host -ForegroundColor Green "You have selected Company4"
Write-Host
} '5' {
$domain = 'company5.com'
$company = 'Company5'
$website = 'www.company5.com'
Write-Host
Write-Host -ForegroundColor Green "You have selected Company5"
Write-Host
} 'Q' {
Write-Host 'Exiting Script' -ForegroundColor Yellow
Start-Sleep -Seconds 1
Exit
}
default {
Write-Host ('{0}Please enter a valid option{0}' -f [environment]::NewLine) -ForegroundColor Red
}
}
} While ($domain -eq $null)
Write-Host
Write-Host "Connecting to local Exchange"
Write-Host
#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 "What's the user's first name?"
$lastname = Read-Host "What's the user's last name?"
$upn = Read-Host "What will the username be?"
$password = Read-Host "Enter the user's temporary password" -AsSecureString
$username="$($firstname) $($Lastname)"
$alias="$($firstname.ToLower()).$($lastname.ToLower())"
$newMbxParams = @{
Name = $username
Password = $password
UserPrincipalName = "$upn@$domain"
Alias = $alias
DisplayName = $username
FirstName = $firstname
LastName = $lastname
OnPremisesOrganizationalUnit = "domain/Azure/Organization/$Company"
PrimarySmtpAddress = "$alias@$domain"
RemoteRoutingAddress = "$alias@$domain"
}
New-RemoteMailbox @newMbxParams | Out-Null
Write-Host
Write-Host -ForegroundColor Green "Remote mailbox for $username has been created"
Write-Host
#Write-Host -ForegroundColor Yellow "Complete the rest on 365 and AD"
$office = Read-Host "Where does the user work?"
Write-Host
$telephone = Read-Host "What's the office telephone number?"
Write-Host
$jobtitle = Read-Host "What's the user's job title?"
Do {
Write-host "Does the user have a mobile phone?" -ForegroundColor Yellow
$Readhost = Read-Host " ( y / n ) "
Switch ($ReadHost)
{
Y {$mobile = Read-Host "Please enter the mobile number"
Write-Host
Write-Host -ForegroundColor Green "Mobile number added"}
N {
Write-Host
Write-Host "No mobile number added"
Write-Host
Start-Sleep -Seconds 1
}
Default {
Write-Host ('{0}Please select Yes or No{0}' -f [environment]::NewLine) -ForegroundColor Red
}
}
} Until ($Readhost -eq 'n' -or 'N')
Set-ADUser -Identity $upn -Office $office -OfficePhone $telephone -HomePage $website -Title $jobtitle -MobilePhone $mobile
Write-Host
Write-Host -ForegroundColor Green "$username has been created in AD"
Write-Host
Write-Host 'Press any key to exit.';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');