Hello Shuja,
Find the updated scripts below.
Script for Business Rule
In the script:
- $urlAttributeName - Specifies the LDAP name of the property that will store the URL to user's OneDrive (e.g. adm-CustomAttributeText1);
- $messageTemplate - Specifies the notification template. In the template, {0} will be replaced with the URL to user's OneDrive.
$attributeName = "seeAlso" # TODO: modify me
$urlAttributeName = "adm-CustomAttributeText1" # TODO: modify me
$messageTemplate = "URL: {0}" # TODO: modify me
$subject = "My Subject" # TODO: modify me
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")
$adminWebApplicationURL = "https://Company-admin.sharepoint.com" # TODO: modify me
$userDNs = @()
try
{
$managerDN = $Context.TargetObject.Get("manager")
$userDNs += $managerDN
}
catch
{
$Context.LogMessage("The user %fullname% has no manager.", "Warning")
}
try
{
$values = $Context.TargetObject.GetEx($attributeName)
$values | %%{$userDNs += $_}
}
catch
{
$Context.LogMessage("Additional delegates are not specified.", "Warning")
}
if ($userDNs.Length -eq 0)
{
return
}
$adminClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($adminWebApplicationURL)
$office365Cred = $Context.GetOffice365Credential()
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($office365Cred.Username, (ConvertTo-SecureString $office365Cred.GetNetworkCredential().Password -AsPlainText -Force))
$adminClientContext.Credentials = $credentials
# Get user
$adminWeb = $adminClientContext.Web
$user = $adminWeb.EnsureUser("%userPrincipalName%")
$adminClientContext.Load($user)
try
{
$adminClientContext.ExecuteQuery()
}
catch
{
Write-Error "An error occurred when searching for the user in SharePoint. Error: $($_.Exception.Message)"
return
}
# Get user profile
$peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($adminClientContext)
$userProfile = $peopleManager.GetPropertiesFor($user.LoginName)
$adminClientContext.Load($userProfile)
$adminClientContext.ExecuteQuery()
# Add personal URL to mail message
$message = [System.String]::Format($messageTemplate, $userProfile.PersonalUrl)
# Save URL
$Context.TargetObject.Put($urlAttributeName, $userProfile.PersonalUrl)
$Context.TargetObject.SetInfoEx(@($urlAttributeName))
Connect-SPOService -Url $adminWebApplicationURL -Credential $office365Cred
# Add permissions
$mailboxParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxParameters"
foreach ($dn in $userDNs)
{
$user = $Context.BindToObjectByDN($dn)
$userName = $user.Get("userPrincipalName")
try
{
$address = $user.Get("mail")
}
catch
{
$address = $NULL
}
# Mailbox permissions
$objReference = New-Object "Softerra.Adaxes.Adsi.AdmObjectReference"
$objReference.ObjectDN = $dn
$permission = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxPermission"
$permission.AllowedRights = "ADM_EXCHANGE_MAILBOX_RIGHTS_FULL_ACCESS"
$permission.Trustee = $objReference
$permissionModification =
New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxRightsModification"
$permissionModification.Operation = "ADS_PROPERTY_APPEND"
$permissionModification.Permission = $permission
$mailboxRights = $mailboxParams.MailboxRights
$mailboxRights.AddModification($permissionModification)
$mailboxParams.MailboxRights = $mailboxRights
# OneDrive permissions
try
{
Set-SPOUser -Site $userProfile.PersonalUrl -LoginName $userName -IsSiteCollectionAdmin $True -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occured when adding permission for $userName. Error: " + $_.Exception.Message, "Warning")
}
if ($address -ne $NULL)
{
$Context.SendMail($address, $subject, $message, $NULL)
}
}
# Update user mailbox
$Context.TargetObject.SetMailParameters($mailboxParams, "ADM_SET_EXCHANGE_PARAMS_FLAGS_NONE")
Script for Scheduled Task
To include the URL to user's OneDrive, use a value reference for the property that stores the URL (e.g. %adm-CustomAttributeText1%)
$attributeName = "seeAlso" # TODO: modify me
$message = "My Message URL: %adm-CustomAttributeText1%" # TODO: modify me
$subject = "My Subject" # TODO: modify me
$userDNs = @()
try
{
$managerDN = $Context.TargetObject.Get("manager")
$userDNs += $managerDN
}
catch
{
$Context.LogMessage("Manager not specified.", "Warning")
}
try
{
$values = $Context.TargetObject.GetEx($attributeName)
$values | %%{$userDNs += $_}
}
catch
{
$Context.LogMessage("Attribute $attributeName is empty.", "Warning")
}
if ($userDNs.Length -eq 0)
{
return
}
foreach ($dn in $userDNs)
{
$user = $Context.BindToObjectByDN($dn)
$address = $user.Get("mail")
$Context.SendMail($address, $subject, $message, $NULL)
}