CSExtension_Mailbox.vb

Imports Microsoft.MetadirectoryServices
Imports System.Management.Automation
Imports System.Management.Automation.Host
Imports System.Management.Automation.Runspaces
Public Class MACallExport
    Implements IMAExtensibleFileImport
    Implements IMAExtensibleCallExport
    Public Sub GenerateImportFile(ByVal filename As String, ByVal connectTo As String, ByVal user As String, ByVal password As String, ByVal configParameters As ConfigParameterCollection, ByVal fullImport As Boolean, ByVal types As TypeDescriptionCollection, ByRef customData As String) Implements IMAExtensibleFileImport.GenerateImportFile
        Dim config As RunspaceConfiguration = RunspaceConfiguration.Create()
        Dim pscmd As String
        Dim pipeLine As Pipeline
        Dim warning As PSSnapInException = Nothing
        'Add Exchange snapin to config.
        config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", warning)
        If warning IsNot Nothing Then
            Throw New Exception(warning.Message)
            Return
        End If
        Dim myRunSpace As Runspace = RunspaceFactory.CreateRunspace(config)
        myRunSpace.Open()
        'Generate User List
        pscmd = "get-user -OrganizationalUnit 'ou=Users,ou=MyOrg,dc=mydomain2,dc=ch' " _
                & "-DomainController dc.mydomain2.ch " _
                & "| export-csv '" & filename & "' " _
                & "-notypeinformation"
        Pipeline = myRunSpace.CreatePipeline(pscmd)
        Pipeline.Invoke()
        myRunSpace.Close()
    End Sub
    Public Sub BeginExport(ByVal connectTo As String, ByVal user As String, ByVal password As String, ByVal configParameters As ConfigParameterCollection, ByVal types As TypeDescriptionCollection) Implements IMAExtensibleCallExport.BeginExport
    End Sub
    Public Sub ExportEntry(ByVal modificationType As ModificationType, ByVal changedAttributes As String(), ByVal csentry As CSEntry) Implements IMAExtensibleCallExport.ExportEntry
        Dim config As RunspaceConfiguration = RunspaceConfiguration.Create()
        Dim pscmd As String
        Dim pipeLine As Pipeline
        Dim warning As PSSnapInException = Nothing
        'Add Exchange snapin to config.
        config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", warning)
        If warning IsNot Nothing Then
            Throw New Exception(warning.Message)
            Return
        End If
        Dim myRunSpace As Runspace = RunspaceFactory.CreateRunspace(config)
        myRunSpace.Open()
        If modificationType = Microsoft.MetadirectoryServices.ModificationType.Replace AndAlso csentry("RecipientType").Value = "UserMailbox" Then
            pscmd = "enable-mailbox -identity '" & csentry("DistinguishedName").Value _
                    & "' -database 'exchserver.mydomain2.ch\Mailbox Database' " _
                    & "-DomainController dc.mydomain2.ch "
            pipeLine = myRunSpace.CreatePipeline(pscmd)
            pipeLine.Invoke()
        End If
        myRunSpace.Close()
    End Sub
    Public Sub EndExport() Implements IMAExtensibleCallExport.EndExport
    End Sub
End Class