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
}