question

ImranShabir avatar image
1 Vote"
ImranShabir asked ImranShabir edited

O365 Powershell New User

Hi, i created this script and it works but the issue i have is after creating the O365 mailbox i wanted to get a Add-MailboxFolderPermission command to add specific permissions and when it runs this command i get an error saying the account doesnt exist even when i add a start-sleep -miliseconds 1000.

Code:

 #Installs AD modules
 import-module activedirectory
    
 #exchange snapin
 Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
    
    
 # The Questions
 Write-host "Please complete the following questions, Ensure spelling and case are accurate"
    
 $First=Read-Host 'Enter First Name'
 $Last=Read-Host 'Enter Last Name'
 $Title=Read-Host 'Enter Job Title'
 $username=Read-Host 'Enter Username'
 $Department=Read-Host 'Enter the users department. Press Space then Enter if information is not available'
 $manager=Read-Host 'Enter Managers UserName'
 $Location=Read-Host 'Enter Office Location'
    
 #Hidden Attributes
 $Path="OU=Users,DC=domain,DC=com"
 $SAMAccountName=$username
 $DisplayName=$First+' '+$Last
 $UserPrincipalName=$username+'@domain.com'
 $Calendar=$username+':\Calendar'
 $routing=$username+'@domain.com'
    
 #This section prompts you to enter a password 
 $password=Read-Host "Enter Users Password" -AsSecureString
    
 #AD Attributes 
 New-ADUser -SAMAccountName $SAMAccountName -name $DisplayName -GivenName $First -Surname $Last -UserPrincipalName $UserPrincipalName -DisplayName $DisplayName -Title $Title -Description $Title -Department $Department -Path $Path -Company $Company -Office $Location -OfficePhone $OfficePhone -Manager $Manager -HomeDirectory $HomeDrive
    
 #Password
 Set-ADAccountPassword -identity $SAMAccountName -NewPassword $password -Reset
 Start-sleep -milliseconds 5000
 Enable-ADAccount -Identity $SAMAccountName
    
 #This section forces and AD to 365 Delta sync from the domain controller, then pauses the script to make sure the sync has completed.
 Invoke-Command -Computer SyncServer01.domain.com -Scriptblock {Start-ADSyncSyncCycle -PolicyType Delta}
 start-sleep -milliseconds 10000
    
 #powershell to connect to O365
 $O365Credentials = Get-Credential
 $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $O365Credentials -Authentication Basic -AllowRedirection
 Import-Module (Import-PSSession -Session $Session -AllowClobber -DisableNameChecking) -Global
    
 # enable exchange
 Enable-RemoteMailbox $username -RemoteRoutingAddress $routing
    
 #Get-Mailbox information
 Get-Mailbox $username
    
 #add sleep in before calendar permissions
 start-sleep -milliseconds 10000
    
 #exchange calendar permissions
 Add-MailboxFolderPermission $Calendar -User "User01" -AccessRights Reviewer
    
 #now remove the connection to O365
 Remove-PSSession $Session
    
 ###### HomeDrive
 $HomeDrive = "\\Server01\HomeDrive\$username"
    
 if (Test-Path $HomeDrive) {
 Write-Host -ForegroundColor Yellow "
 -------------------------------------------------------
 `n
 Account or parts of the setup have already been done.
 `n
 -------------------------------------------------------
 "
 }
 else {
 # create new folder
 $null = New-Item -Path $HomeDrive -ItemType Directory
 # get permissions
 $acl = Get-Acl -Path $HomeDrive
    
 # add a new permission (FullControl, Modify, Read)
 $permission = $username, 'Full', 'ContainerInherit, ObjectInherit', 'None', 'Allow'
 $rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permission
 $acl.SetAccessRule($rule)
    
 # set new permissions
 $acl | Set-Acl -Path $HomeDrive 
 # verify the folder has been created
 if (Test-Path $HomeDrive) {
 Write-Host -ForegroundColor Green "
 ----------------------------
 `n
 Account Created successfully.
 `n
 ----------------------------
 "
 }
 else {
 Write-Host -ForegroundColor Red "
 --------------------------------
 `n
 The Account failed to create all or parts of its setup.
 `n
 --------------------------------
 "
 }
 Set-ADUser -identity $username -HomeDrive $null -HomeDirectory $null
 }


Any Help would be greatful. Thank you in advance



office-exchange-server-administrationoffice-outlook-itprooffice-exchange-online-itprooffice-exchange-server-dev
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @ImranShabir ,
Since we are focusing on general issues of Exchange, we can only provide limited help for scripts and Powershell, so we cannot tell whether your script is correct.
We can investigate from the following directions first:
1.What is the configuration of your Exchange environment?
2.Can you view the mailbox created by this script in the Exchange admin center?
3.For the user mailbox created by this script, if you run other command lines, is it successful? For example, Get-Mailbox.
4.In your environment, if you manually create a user mailbox and then run the command line that assigns permissions, can it be successful?
5.I noticed that your script contains command lines for creating users and assigning permissions. If you run the command line scripts for creating users and assigning permissions separately, will it be successful?

0 Votes 0 ·

Hi @LucasLiu-MSFT

Thank you for the response,

Answers to your questions..

1.What is the configuration of your Exchange environment?
A: Hybrid (on prem is Exchange 2016 and O365)
2.Can you view the mailbox created by this script in the Exchange admin center?
A: Yes without the mailboxfolderpermissions command.
3.For the user mailbox created by this script, if you run other command lines, is it successful? For example, Get-Mailbox.
A: Yes
4.In your environment, if you manually create a user mailbox and then run the command line that assigns permissions, can it be successful?
A: Yes
5.I noticed that your script contains command lines for creating users and assigning permissions. If you run the command line scripts for creating users and assigning permissions separately, will it be successful?
A: Separately it works.

0 Votes 0 ·

Hi @ImranShabir ,
Based on the information you provided, I think the problem may be caused by your script itself. I will tag your post with "Office-Exchange-Server-Dev", which will introduce engineers related to Exchange development.

0 Votes 0 ·

1 Answer

ImranShabir avatar image
0 Votes"
ImranShabir answered

Hi,

Can i get an update please?

Imran

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.