Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Created January 25, 2016 01:46
Show Gist options
  • Save darrenjrobinson/96910c83cdf5878ddfa0 to your computer and use it in GitHub Desktop.
Save darrenjrobinson/96910c83cdf5878ddfa0 to your computer and use it in GitHub Desktop.
MIMExchPSMA-Export
param
(
$username,
$password,
$ExportType
)
begin
{
$DebugFilePath = "C:\PROGRA~1\MICROS~4\2010\SYNCHR~1\EXTENS~2\Exchange\Exchange\DebugExchMA.txt"
if(!(Test-Path $DebugFilePath))
{$DebugFile = New-Item -Path $DebugFilePath -ItemType File}
else
{$DebugFile = Get-Item -Path $DebugFilePath}
"Starting Export : " + (Get-Date) | Out-File $DebugFile -Append
# Setup Remote Powershell Session
$server = "http://exchange.customer.com.au/powershell"
$securestring = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$securestring.AppendChar($_)}
$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username, $securestring
"Looking to see if we have an RPS Session : " | Out-File $DebugFile -Append
if ($global:session){Remove-PSSession $global:session}
"Opening a new RPS Session." | out-file $DebugFile -Append
$skipCertificate = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $server -Authentication Kerberos -Credential $credential -SessionOption $skipCertificate
$global:session = $session
Import-PSSession $global:session
"Opened a new RPS Session" | out-file $DebugFile -Append
}
process
{
$error.clear()
$errorstatus = "success"
$errordetails = $null
$Identifier = $_.Identifier
$objectGuid = $_.DN
#Loop through changes and update parameters
foreach ($can in $_.ChangedAttributeNames)
{
$can | out-file $DebugFile -append
foreach ($ValueChange in $_.AttributeChanges[$can].ValueChanges)
{
if ( $can -eq 'mailNickname'){$mailNickname = $ValueChange.value}
}
}
# Export uses Remote Powershell so we don't have to install the Exchange commandlets on this MIM Server and keep them current.
if ($_.ObjectModificationType -eq 'Add')
{
# adds are caught by importing new objects from Active Directory (see import script)
# and joining these to existing user objects on the metaverse
throw "Add modification are not supported"
}
if ($_.ObjectModificationType -eq 'Delete')
{
# deletes are caught by importing deleted objects (isDeleted) from Active
# Directory (see import script). This way we clear up the CS
throw "Delete modification are not supported"
}
#Supported ChangeType is Replace
if ($_.ObjectModificationType -match 'Replace')
{
$errorstatus = "success"
# Lookup the object so we know it exists before we enable the user
"Get User via DirEntry" | Out-File $DebugFile -Append
$curUser = New-Object System.DirectoryServices.DirectoryEntry "LDAP://<GUID=$objectGuid>", $username, $password
if ( $curUser )
{
# using msExchMailboxGuid to determine if the user has a mailbox
if (!$curUser.Properties["msExchMailboxGuid"])
{
"No MS Exch GUID" | Out-File $DebugFile -Append
if ($mailNickname)
{
if ($curuser.properties["mailNickname"][0])
{
"Replace nickName $curuser.properties['mailNickname'][0] " | Out-File $DebugFile -Append
set-aduser -Identity $objectGuid -replace @{'mailNickname' = $mailNickname} -ErrorAction SilentlyContinue -ErrorVariable $errordetails
}
else
{
"Add nickName $mailNickname" | Out-File $DebugFile -Append
set-aduser -Identity $objectGuid -add @{'mailNickname' = $mailNickname} -ErrorAction SilentlyContinue -ErrorVariable $errordetails
}
}
# User isn't Mailbox enabled. Enable them
if ($mailNickname)
{
"Call Update-Recip" | Out-File $DebugFile -Append
Update-Recipient -Identity $mailnickname -ErrorAction SilentlyContinue -ErrorVariable $errordetails
}
}
}
}
#Return the result to the MA
$obj = @{}
$obj.Add("[Identifier]",$Identifier)
$obj.Add("[ErrorName]","success")
if($errordetails){$obj.Add("[ErrorDetail]",$errordetails) | out-file $DebugFile -append }
$obj
}
end
{
Write-output "Closing RPS Session" | out-file $DebugFile -append
Remove-PSSession $global:session
Write-out "Export Finished: " + (Get-Date) | out-file $DebugFile -append
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment