Jump to content

Recommended Posts

Posted

I'm trying to work our a script that will read every account in an OU and issue the following command against them if they don't already have a mail enabled account:-

 

Enable-RemoteMailbox -identity $username -RemoteRoutingAddress $mailalias@{tenant}.mail.onmicrosoft.com

 

I have a script to run manually that will prompt for usernamd and mailalias, however I'd like to automate this to run on a full OU

 

I've come across this, but it errors:-

 

[color=#859900 !important][font=Menlo]Get-ADUser [/font][/color][color=#93A1A1][font=Menlo]username | Enable-RemoteMailbox [/font][/color][color=#268BD2 !important][font=Menlo]$_[/font][/color][color=#93A1A1][font=Menlo].sAMAccountName -RemoteRoutingAddress [/font][/color][color=#2AA198 !important][font=Menlo]"[/font][/color][color=#CB4B16 !important][font=Menlo]$([/font][/color][color=#268BD2 !important][font=Menlo]$_[/font][/color][color=#93A1A1][font=Menlo].sAMAccountName[/font][/color][color=#CB4B16 !important][font=Menlo])[/font][/color][color=#2AA198 !important][font=Menlo]@tenant.mail.onmicrosoft.com"

 

As my powershell skills are a bit sketchy, I'm struggling to know what to change to get it to work[/font][/color]

Posted

So this is the actual script:-

 

$Users = Get-User -OrganizationalUnit "ad.DOMAIN.org.uk/SCHOOL/Users/Students/2021"ForEach ($User in $Users) {    Get-ADUser $User | Enable-RemoteMailbox $_.sAMAccountName -RemoteRoutingAddress "$($_.sAMAccountName)@tenant.mail.onmicrosoft.com"  }

 

and the error is:-

 

Get-ADUser : Cannot bind parameter 'Identity'. Cannot convert the"ad.DOMAIN.org.uk/SCHOOL/Users/Students/2021/USERNAME" value of type"Microsoft.Exchange.Data.Directory.Management.User" to type "Microsoft.ActiveDirectory.Management.ADUser".At C:\scripts\test.ps1:4 char:14+   Get-ADUser $User | Enable-RemoteMailbox $_.sAMAccountName -RemoteRo ...+              ~~~~~    + CategoryInfo          : InvalidArgument: ( [Get-ADUser], ParameterBindingException    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADUser

 

(Obviously I've sanitised the code and the error to remove our actual structure)

Posted

I’m not sure if you code would be able to work but I would use SearchBase instead of an array

 

Get-ADUser -SearchBase "OU=2011,OU=Students,DC=domain,DC=local" | Enable-RemoteMailbox $_.SamAccountName -RemoteRoutingAddress "$($_.SamAccountName)@tenant.mail.onmicrosoft.com"

 

Obviously change the SearchBase to match what you need and ensure it is the correct format.

  • Thanks 1
Posted
The only thing I would say is | into a for each and put the enable-remote mailbox in a try catch statement. Gives you some error trapping ability and the likes.
Posted

Thankyou @snagrat

 

Next question would be whilst SamAccountName is great for privisioning students mailboxes (where username and email adress match).

 

For staff mailboxes, the staff logon to the domain using the SamAccountName, however their O365 login and email address are based on the userPrincipalName. Is there a way to extract just the UPN without the domain name on the end, to use that for the remote routing address?

Posted

I've tried running using searchbase, and it's still giving me errors:-

 

Get-ADUser -SearchBase "OU=2021,OU=Students,OU=Users,OU=SCHOOL,DC=ad,DC=DOMAIN,DC=org,DC=UK" -filter "ObjectClass -eq 'user'" | Enable-RemoteMailbox $_.SamAccountName -RemoteRoutingAddress "$($_.SamAccountName)@TENANT.mail.onmicrosoft. com"

 

Cannot bind argument to parameter 'Identity' because it is null.    + CategoryInfo          : InvalidData: ( [Enable-RemoteMailbox], ParameterBindingValidationException    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Enable-RemoteMailbox    + PSComputerName        : ex02.ad.DOMAIN.org.uk

Posted (edited)

Oh that’s harder and not sure how to fit that in to what I have already provided.

 

You could do it easily by importing a csv with the accounts in. But that would mean manually creating the csv to begin with and ensuring the UPN was correct without the domain bit

 

EDIT:

This may work:

 

Get-ADUser -SearchBase "OU=2021,OU=Students,OU=Users,OU=SCHOOL,DC=ad,DC=DOMAIN,DC=org,DC=UK" -filter "ObjectClass -eq 'user'" | Enable-RemoteMailbox $_.UserPrincipalName.TrimEnd(" @DomAin.com") -RemoteRoutingAddress "$($_.UserPrincipalName.TrimEnd(" @DomAin.com"))@TENANT.mail.onmicrosoft. com"

Edited by snagrat

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...