O365 Powershell New User

Imran Shabir 6 Reputation points
2021-04-23T13:28:47.957+00:00

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

Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
4,909 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,205 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
515 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,363 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Imran Shabir 6 Reputation points
    2021-05-05T17:56:03.157+00:00

    Hi,

    Can i get an update please?

    Imran

    0 comments No comments