Sedgwick_County
asked on
Loop Question for Powershell Script
Hello, I am new to powershell and have been working on a code but am running into a road block. What I would like to do is have the code go back to the top and ask for the username again to run the process over if needed. After it runs set-mailbox -identity $username -CustomAttribute I need it to go back to the top and ask for the username again without reloading the Exchange Module again.
Here is the code:
Here is the code:
#...Load Exchange Module into Powershell Session
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://NAMEOFEXCHANGESERVER.COM/PowerShell/ -
Authentication Kerberos
Import-PSSession $s
#...Ask for Username
$username=Read-Host 'Username'
#...Display Custom Attributes
Get-Mailbox -Identity "$username" | Format-List
CustomAttribute2,CustomAttribute3,CustomAttribute4,CustomAttribute5,CustomAttribute6
#...Display Menu
Do {
Write-Host "
----------Custom Attribute Change---------
1 = Change Attribute 2
2 = Change Attribute 3
3 = Change Attribute 4
4 = Change Attribute 5
5 = Change Attribute 6
----------------------___-----------------"
$choice1 = read-host -prompt "Select number & press enter"
} until ($choice1 -eq "1" -or $choice1 -eq "2" -or $choice1 -eq "3" -or $choice1 -eq "4" -or $choice1 -eq "5" -or $choice1 -eq "6")
#...Menu Selection Actions
Switch ($choice1) {
"1" {$att2= Read-Host 'New Attribute2:'
set-mailbox -identity $username -CustomAttribute2 $att2}
"2" {$att3= Read-Host 'New Attribute3:'
set-mailbox -identity $username -CustomAttribute3 $att3}
"3" {$att4= Read-Host 'New Attribute4:'
set-mailbox -identity $username -CustomAttribute4 $att4}
"4" {$att5= Read-Host 'New Attribute5:'
set-mailbox -identity $username -CustomAttribute5 $att5}
"5" {$att6= Read-Host 'New Attribute6:'
set-mailbox -identity $username -CustomAttribute6 $att6
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sedgwick,
The outer DO loop needs to include the read-host of $username, else it does not work and is not as intended ;-). In addition, the end criterion has not been defined yet, but I guess an empty username is a valid one.
The outer DO loop needs to include the read-host of $username, else it does not work and is not as intended ;-). In addition, the end criterion has not been defined yet, but I guess an empty username is a valid one.
All above codes will work.. IMO, You can ask a question at the end of each mailbox edit process to exit or continue before you go back to the 'Type Username' question..
For example..
For example..
#...Load Exchange Module into Powershell Session
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://NAMEOFEXCHANGESERVER.COM/PowerShell/ -
Authentication Kerberos
Import-PSSession $s
Do {
#Add rest of the code here for processing
$Exitcode = Read-Host "Type 0 & press Enter to exit or Enter to Continue"
} Until ($Exitcode -eq 0)
Open in new window