Solved

Loop Question for Powershell Script

Posted on 2013-06-26
4
527 Views
Last Modified: 2013-06-26
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:

#...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
}
}

Open in new window

0
Comment
Question by:Sedgwick_County
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39278718
Just run it in a endless loop. If you need to "jump" to the beginning again, execute continue.
Please use proper indents to make the code more readable.
You allowed "6" as input, though that is not used anywhere. You'll see that I've simplified the menu check, and added "9" for exit.
#...Load Exchange Module into Powershell Session
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://NAMEOFEXCHANGESERVER.COM/PowerShell/ -Authentication Kerberos
Import-PSSession $s

:endless while ($true) {
  #...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
9 = Exit
----------------------___-----------------"
    $choice1 = read-host -prompt "Select number & press enter"
  } until ($choice1 -like "[1-59]")

  #...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}
    "9" {break endless}
  }
}

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39278720
#...Load Exchange Module into Powershell Session
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://NAMEOFEXCHANGESERVER.COM/PowerShell/ -

Authentication Kerberos
Import-PSSession $s
Do {

#...Ask for Username
$username=Read-Host 'Type Username (Enter to exit)'

#...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
}
}
} until ($username -ne '')

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39278761
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.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39278773
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..
#...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

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Powershell + stopping and starting on a remote server 12 23
Exchange 2010 internal URLs 2 40
XML Removal- Powershell 4 25
powershell $webclient.UploadFileWithUniqueName 6 24
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question