Additional info:
THIS script, provided by Adaxes, uses a different method of authenticating (to the LyncOnlineConnector):
http://www.adaxes.com/script-repository ... e-s288.htm
This DOES work in our environment (as a Production Script instead of a Custom Command). I tried updating it to connect to MSOnline instead, and then run the two lines of code, like this:
# Get the user ID in Office 365
try
{
$objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
$Context.LogMessage("The user doesn't have an Office 365 Account", "Warning")
return
}
# Script block to remove FWD from the user
$scriptBlock = {
Import-Module MSOnline #<<---------------------------------#
# Connection to Office365
$password = ConvertTo-SecureString -AsPlainText -Force -String $password
$credential = New-Object System.Management.Automation.PsCredential($adminName,$password)
try
{
# Connect to O365
$session = New-CsOnlineSession -Credential $credential
Import-PSSession $session -AllowClobber | Out-Null
# Break the external FWD
Set-Mailbox "%userPrincipalName%" -ForwardingSmtpAddress $Null
Set-Mailbox "%userPrincipalName%" -DeliverToMailboxAndForward $False
}
finally
{
# Close the remote session and release resources
Remove-PSSession $session
}
}
# Get credential to connect to Office 365 with
$office365Cred = $Context.GetOffice365Credential()
$adminName = $office365Cred.Username
$password = $office365Cred.GetNetworkCredential().Password
# Start Windows PowerShell as a separate process and run the script block in that process
$powershellPath = "$env:windir\system32\windowspowershell\v1.0\powershell.exe"
$arguments = @("-noninteractive", "-noprofile", "-executionpolicy bypass", "-Command `$adminName = '$adminName'; `$password = '$password'; `$objectId = '$objectId'; `$policyName = '$policyName'; $scriptBlock")
$starProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
$starProcessInfo.FileName = $powershellPath
$starProcessInfo.Arguments = $arguments
$starProcessInfo.WindowStyle = "Hidden"
$starProcessInfo.CreateNoWindow = $True
$starProcessInfo.UseShellExecute = $False
$starProcessInfo.RedirectStandardOutput = $True
$starProcessInfo.RedirectStandardError = $True
$process = [System.Diagnostics.Process]::Start($starProcessInfo)
$resultErrors = $process.StandardError.ReadToEnd()
$resultOutput = $process.StandardOutput.ReadToEnd()
# Add operation result to the Execution Log
# Add errors
if (-not([System.String]::IsNullOrEmpty($resultErrors)))
{
$Context.LogMessage($resultErrors, "Error")
}
# Add information messages and warnings
if (-not([System.String]::IsNullOrEmpty($resultOutput)))
{
$Context.LogMessage($resultOutput, "Warning")
}
This returns the error:
3JakeTEST (Company.A.org\\Disabled\\Users)
-Remove External Email Forwarding: 1 operation executed
--Run PowerShell script 'sets attributes for external forwarding to $Null' for the user
Set-Mailbox : The term 'Set-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again...
I thought that Set-Mailbox was part of the MSOnline module; is there a different module I should try to import instead?