• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1073
  • Last Modified:

Help breaking out of a do...while loop in powershell

Hello Powershell Experts,
I’m once again stuck on something simple.  I have created (with significant help from this community) a small script that basically allows me to input a department and collect a list of users in that department along with their titles, and their mailbox size.  This part is working fine.  What I would like to do is put this into a loop so that a help desk person could run the script and collect the information for multiple departments.  My Powershell skills are embarrassingly elementary and I’m having problems setting up the loop correctly.  I think I want to use the Do…While loop as part of my script.  The code below lets me run the report for more than one department:
$RunReport = 'y'
Do {
$ReportLocation = "D:\PreMigrationReports\'$Department'.txt"
$Department = ""
$Department = Read-Host "For which department do you want to run the pre-migration report?"
$Users = Get-user -Filter {Department -like $Department}
$title = foreach ($user in $Users) {select-object title}
$Results = foreach ($user in $Users) {
    Get-MailboxStatistics $User.samaccountname | `
    Select-object `
    @{n="User";e={$_.DisplayName}}, `
    @{n="Job Title";e={$user.title}}, `
    @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}
    } 
Write-host "The pre-migration report for '$Department' has been saved to '$ReportLocation'"    
$RunReport = Read-Host "Do you want to run the pre-migration report for another department?"
}
While ($RunReports = 'Y')

The issue I’m running into is I don’t know how to break out of the loop.  I tried something like the following, but I keep getting asked if I want to run the report for another department.

$RunReport = 'y'
Do {
$ReportLocation = "D:\PreMigrationReports\'$Department'.txt"
$Department = ""
$Department = Read-Host "For which department do you want to run the pre-migration report?"
$Users = Get-user -Filter {Department -like $Department}
$title = foreach ($user in $Users) {select-object title}
$Results = foreach ($user in $Users) {
    Get-MailboxStatistics $User.samaccountname | `
    Select-object `
    @{n="User";e={$_.DisplayName}}, `
    @{n="Job Title";e={$user.title}}, `
    @{n="Mailbox Size (MB)";e={$_.totalitemsize.value.ToMB()}}
    } 
Write-host "The pre-migration report for '$Department' has been saved to '$ReportLocation'"    
$RunReport = Read-Host "Do you want to run the pre-migration report for another department? Y/N"
If ($RunReport -eq 'N'){Write-Host "Your departmental pre-migration reports are ready for retrieval."}
}
While ($RunReports = 'Y')

Open in new window

I know this is very elementary stuff and I apologize in advance for having to ask, but I’ve tried everything I can think of and I’m just not getting it.  Thanks in advance.

Nick
0
ndalmolin_13
Asked:
ndalmolin_13
1 Solution
 
Dan ArseneauCommented:
Change your variable $RunReports to $true or $false.

You're performing a condition check at the end of the while statement so you should be using -eq, not '='.
0
 
netballiCommented:
just put
{break}

from where you wish to exit the loop
0
 
ndalmolin_13Author Commented:
Thanks.
0
 
QlemoC++ DeveloperCommented:
What DanArseneau points to is to replace your last line with
   While ($RunReport -eq 'Y')

Open in new window

(note the singular - you are setting $RunReport with read-host, not $RunReports).
I would also move
If ($RunReport -eq 'N'){Write-Host "Your departmental pre-migration reports are ready for retrieval."}

Open in new window

to outside of the loop:
   ...
   While ($RunReport -eq 'Y')
Write-Host "Your departmental pre-migration reports are ready for retrieval."

Open in new window

and, btw, your line
$title = foreach ($user in $Users) {select-object title}

Open in new window

is superfluous (you do not use the var anywhere) and cumbersome, as you usually would perform
$title = $Users | select-object title

Open in new window

if anything at all.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now