Answered by:
Overwrite Parameter in Do loop

Question
-
When I attempt to run the following function I am unable to overwrite the $nametocheck variable using read-host. This is passed as a parameter and I want to return it for use in another function. The $mailboxes and $groups are arrays with groups and mailboxes from Office 365.
Function UserGroupCheck { param( [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] [string] $nametocheck ) $errorcheck = $error.count $mailboxes = get-mailbox $groups = Get-group Do{ $endloop = $false If($mailboxes -contains $nametocheck){ $endloop = $true} ElseIf($groups -contains $nametocheck){ $endloop = $true} If($endloop -ne $true){ Write-Output "Did you mean one of these?" $mailboxes -like "*$nametocheck*" $groups -like "*$nametocheck*" $nametocheck = Read-Host -Prompt 'Enter the correct name' } }while ($endloop -ne $true) If ($errorcheck -lt $error.count) { Write-Output "There seems to be a problem, Please check the error." Write-Output $error KeyPause } return $nametocheck }
Thursday, June 12, 2014 7:59 PM
Answers
-
Something that looks off to me here is that you're mixing in some Write-Output, but then returning $nametocheck at the end. If you're assigning the result of this function to a variable, you'll wind up having an array with things like "Did you mean one of these?" , etc, before the final array element which contains the actual result.
Since this function's purpose seems to involve user interface, it's okay to use Write-Host here for the prompts, and only send $nametocheck down the Output stream.
I'm assuming that you have functions named Get-Group and Get-Mailbox which return arrays of strings. If $mailboxes and $groups contain other types of objects, your use of the -contains operator is probably not going to work.
Here's the revision which only sends $nametocheck out to the pipeline, and everything else is routed through Write-Host:
Function UserGroupCheck { param( [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] [string] $nametocheck ) $errorcheck = $error.count $mailboxes = get-mailbox $groups = Get-group Do{ $endloop = $false If($mailboxes -contains $nametocheck){ $endloop = $true} ElseIf($groups -contains $nametocheck){ $endloop = $true} If($endloop -ne $true){ Write-Host "Did you mean one of these?" $mailboxes -like "*$nametocheck*" | Out-String | Write-Host $groups -like "*$nametocheck*" | Out-String | Write-Host $nametocheck = Read-Host -Prompt 'Enter the correct name' } }while ($endloop -ne $true) If ($errorcheck -lt $error.count) { Write-Host "There seems to be a problem, Please check the error." $error | Out-String | Write-Host KeyPause } return $nametocheck } $string = 'Bob' $test = UserGroupCheck -nametocheck $string
- Marked as answer by wmsdist Thursday, June 12, 2014 8:31 PM
Thursday, June 12, 2014 8:11 PM
All replies
-
Something that looks off to me here is that you're mixing in some Write-Output, but then returning $nametocheck at the end. If you're assigning the result of this function to a variable, you'll wind up having an array with things like "Did you mean one of these?" , etc, before the final array element which contains the actual result.
Since this function's purpose seems to involve user interface, it's okay to use Write-Host here for the prompts, and only send $nametocheck down the Output stream.
I'm assuming that you have functions named Get-Group and Get-Mailbox which return arrays of strings. If $mailboxes and $groups contain other types of objects, your use of the -contains operator is probably not going to work.
Here's the revision which only sends $nametocheck out to the pipeline, and everything else is routed through Write-Host:
Function UserGroupCheck { param( [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] [string] $nametocheck ) $errorcheck = $error.count $mailboxes = get-mailbox $groups = Get-group Do{ $endloop = $false If($mailboxes -contains $nametocheck){ $endloop = $true} ElseIf($groups -contains $nametocheck){ $endloop = $true} If($endloop -ne $true){ Write-Host "Did you mean one of these?" $mailboxes -like "*$nametocheck*" | Out-String | Write-Host $groups -like "*$nametocheck*" | Out-String | Write-Host $nametocheck = Read-Host -Prompt 'Enter the correct name' } }while ($endloop -ne $true) If ($errorcheck -lt $error.count) { Write-Host "There seems to be a problem, Please check the error." $error | Out-String | Write-Host KeyPause } return $nametocheck } $string = 'Bob' $test = UserGroupCheck -nametocheck $string
- Marked as answer by wmsdist Thursday, June 12, 2014 8:31 PM
Thursday, June 12, 2014 8:11 PM -
THANK YOU!! The write-host was most helpful along with the nudge about the contents of the arrays. They did in fact contain other objects and I had to edit the If statements. The working code is below.
Function UserGroupCheck { param( [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] [string] $nametocheck ) $errorcheck = $error.count $mailboxes = get-mailbox $groups = Get-group Do{ $endloop = $false If($mailboxes.Name -contains $nametocheck){ $endloop = $true} ElseIf($groups.Name -contains $nametocheck){ $endloop = $true} If($endloop -ne $true){ Write-Output "Did you mean one of these?" $mailboxes -like "*$nametocheck*" | Out-String | Write-Host $groups -like "*$nametocheck*" | Out-String | Write-Host $nametocheck = Read-Host -Prompt 'Enter the correct name' } }while ($endloop -ne $true) If ($errorcheck -lt $error.count) { Write-Output "There seems to be a problem, Please check the error." Write-Output $error KeyPause } return $nametocheck }
Thursday, June 12, 2014 8:31 PM -
Get-mailbox and Get-Group are Office online and Exchange CmdLets. They returns objects.
The tests here won't work with Office365 or Exchange. The design is faulty and unnecessary.
To check a mailbox use this
if(Get-Mailbox $nametoCheck -ea 0){
# mailbox found
}else{
# mailbox not found
}Same is true for GEt-Group
¯\_(ツ)_/¯
- Edited by jrv Thursday, June 12, 2014 8:42 PM
Thursday, June 12, 2014 8:41 PM -
Perhaps you need to explain what the purpose of this is. It seems unnecessary.
¯\_(ツ)_/¯
Thursday, June 12, 2014 8:43 PM -
If you get an error you will get bad data. All Write-Output statements aggregate to the return even if you use a return statement.
Return statements are no considered a good idea. Your sue is one perfect example of why and how this leads to failure.
¯\_(ツ)_/¯
Thursday, June 12, 2014 9:08 PM