Link to home
Start Free TrialLog in
Avatar of ndalmolin_13
ndalmolin_13Flag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Dan Arseneau
Dan Arseneau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
just put
{break}

from where you wish to exit the loop
Avatar of ndalmolin_13

ASKER

Thanks.
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.