Powershell Scripting issue for creating shared mailboxes
Hello All, I've been seeing some strangeness when developing a small powershell script, I've stepped through it step by step with powergui and when i call my custom function the variable randomly inherits a bunch of extra information when i call function createGroup it appears to be appending the value of $ouGroup Any help would be greatly appreciated function groupExists($groupName){ $test = $null $test = dsquery group domainroot -r -name "$groupName" if($test.length -eq 0){ return $true }else{ return $false } } function isValidDB($dbName){ $checkError = $null Get-MailboxDatabase -Identity $dbName -ErrorVariable $checkError -ErrorAction SilentlyContinue if($checkError.length -eq 0){ return $true }else{ return $false } } function mbxExists ($mbxName){ $checkError = $null Get-Mailbox -Identity "$mbxName" -ErrorVariable $checkError -ErrorAction SilentlyContinue if($checkError.length -eq 0){ return $true }else{ return $false } } function createGroup ($cGGroup, $ou, $groupDesc){ #Peice togther the groups $group = $cGGroup + "-RW" $groupReturn = $group if(groupExists($group)){ Write-Host '' Write-Host -ForegroundColor Red 'Group already exists. Exiting...' Write-Host '' exit } #Peice togther the LDAP Distinguished name $groupName = "CN=" + $group +","+ $ou Write-Host '' Write-Host ("Group: " + $groupName) try{ #dsadd group "$groupName" -samid "$group" -secgrp 'yes' -scope '1' -desc "$groupDesc" #write-host '' }catch{ #Failed to create group, write message and exit. Write-Host '' Write-Host -foregroundcolor DarkRed 'Unable to create group, Confirm name is valid' Write-Host '' exit } return [string]$groupReturn } function createMailbox ($mbxName, $ou, $db, $mbxAlias, $mbxGroup, $mbxEmail){ #function asumes input has been checked by getInputMBXName (mailbox doesnt exist) $upn = $mbxName + '@cleanair.ict' $mailError = $null New-Mailbox -Name:"$mbxName" -OrganizationalUnit:"$ou" -Database:"$db" -Userprincipalname '$upn' -alias '$mbxAlias' -Shared -ErrorVariable $mailError if($mailError.length -ne 0){ # Something went wrong, likely wrong parameters passed. Write-Host '' Write-Host -ForegroundColor DarkRed 'Error Creating Mailbox. Exiting...' Write-Host $mailError[0] exit } Start-Sleep 1 # Confirm we can find the mailbox and then apply our permisions $count = 0 do{ if($count -eq 14){ Write-Host '' Write-Host -ForegroundColor DarkRed 'Unable to find the new shared mailbox after 15 tries exiting' Write-Host '' exit }elseif(mbxExists($mbxName)){ # Mailbox found, break out of loop and do our work $count = 99 }else{ $count++ Start-Sleep 1 } }while($count -le 14) Add-ADPermission "$mbxAlias" -User 'MAIL-ALL-RW' -AccessRights:'FullAccess' Add-ADPermission "$mbxAlias" -User "$mbxGroup" -AccessRights 'FullAccess' -ExtendedRights 'Send-As' Start-Sleep 1 Get-Mailbox -Identity "$mbxAlias" | Update-List -Property 'EmailAddresses' -Add "$mbxEmail" | Set-Mailbox | Set-Mailbox -EmailAddressPolicyEnabled $false -PrimarySmtpAddress "$mbxEmail" } function getInputDatabase(){ # Function to get and ensure the input makes sense $condition = $true $userIn do{ [string]$userIn = Read-Host "Database:(Orion\Mailbox Database) " if($userIn.length -eq 0){ $userIn = "orion\Mailbox Database" $condition = $false }elseif(($userIn.Contains("\")) -and (isValidDB($userIn))){ $condition = $false }else{ $condition = $true Write-Host -ForegroundColor Red "Invalid database name, Name should be in format of Server\Database and must be a valid database on the server" } }while($condition) return $userIn } function getInputMBXName(){ $condition = $true $userIn do{ [string]$userIn = Read-Host "Mailbox name: " if($userIn.length -eq 0){ Write-Host -ForegroundColor Red "You must enter a name" $condition = $true }elseif(!(mbxExists($userIn))){ $condition = $false }else{ $condition = $true Write-Host -ForegroundColor Red "Mailbox name already inuse" } }while($condition) return $userIn } function getInputSharedEmail() { $condition = $true $userIn do{ [string]$userIn = Read-Host "E-mail address: " if($userIn.length -eq 0){ Write-Host -ForegroundColor Red "You must enter an e-mail address" $condition = $true }elseif((!(mbxExists($userIn))) -and ($userIn.contains("@"))-and ($userIn.contains("."))){ $condition = $false }else{ $condition = $true Write-Host -ForegroundColor Red "E-mail address already inuse or invalid" } }while($condition) return $userIn } function getInputSharedAlias(){ $condition = $true $userIn do{ [string]$userIn = Read-Host "Mailbox alias: " if($userIn.length -eq 0){ Write-Host -ForegroundColor Red "You must enter an alias" $condition = $true }elseif((!(mbxExists($userIn))) -and (!$userIn.contains("@"))-and (!$userIn.contains(" "))){ $condition = $false }else{ $condition = $true Write-Host -ForegroundColor Red "Mailbox alias already inuse or contains invalid characters" } }while($condition) return $userIn } # MAIN Script Execution Area #Required Variables $ouEmail = 'cleanair.ict/cleanair/Administrative/Exchange resources' $ouGroup = 'OU=Exchange resources,OU=Administrative,OU=Cleanair,DC=cleanair,DC=ict' #Get the Input and do some sanitity checks Write-Host -ForegroundColor Yellow "Script to create a shared mailbox" Write-Host '' [string]$sharedDB = getInputDatabase [string]$sharedName = getInputMBXName [string]$sharedEmail = getInputSharedEmail [string]$sharedAlias = getInputSharedAlias #Write-Host("SharedDB " + $sharedDB) Write-Host("SharedName " + $sharedName) Write-Host $sharedName -OutVariable $outGroup #Write-Host("SharedEmail " + $sharedEmail) #Write-Host("SharedAlias " + $sharedAlias) #Get the work done $sharedGroup = createGroup($outGroup, $ouGroup, $null) Write-Host("SharedGroup " + $sharedGroup) #createMailbox($sharedName, $ouEmail, $sharedDB, $sharedAlias, $sharedGroup, $sharedEmail) See bellow for the script
November 12th, 2010 10:53am

On Fri, 12 Nov 2010 15:49:37 +0000, Ryan Eld. _ wrote: >I've been seeing some strangeness when developing a small powershell script, I've stepped through it step by step with powergui and when i call my custom function the variable randomly inherits a bunch of extra information > > > >when i call function createGroup it appears to be appending the value of $ouGroup Why is that surprising? You passed the "$ouGroup" variable to the function as the second parameter here: $sharedGroup = createGroup($outGroup, $ouGroup, $null) That 2nd parameter is called "$ou" in your function and it's appended to the string stored in $groupname: $groupName = "CN=" + $group +","+ $ou --- Rich Matheisen MCSE+I, Exchange MVP --- Rich Matheisen MCSE+I, Exchange MVP
Free Windows Admin Tool Kit Click here and download it now
November 12th, 2010 11:02pm

Hi Ryan, Any update? Best regards, Serena
November 16th, 2010 3:59am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics