meowjayjay
Goto Top

Exchange Shell - PS Script

Liebe Coder!

Wir migrieren unsere on Premise Exchange Postfächer via PST auf 0365.

Der Plan ist anhand einer .csv Massexport / Forwarding auf die neue Mailadresse zu betreiben - mithilfe dieses Scripts:


# Parameter Definition
param (
    [string]$Mailbox ="user@domain.de",  
    [string]$ExportPath = "C:\Exports",  
    [string]$ForwardingAddress = "user@domain.com"  
)

# Funktion zum Exportieren der PST-Datei
function Export-MailboxToPST {
    param (
        [string]$Mailbox,
        [string]$ExportPath
    )

    # Check if ExportPath exists, if not create it
    if (!(Test-Path -Path $ExportPath)) {
        New-Item -ItemType Directory -Path $ExportPath
    }

    # Define the PST file path
    $PSTFilePath = Join-Path -Path $ExportPath -ChildPath "$Mailbox.pst"  

    # Create a new mailbox export request
    New-MailboxExportRequest -Mailbox $Mailbox -FilePath $PSTFilePath

    # Wait for the export request to complete
    $exportRequest = Get-MailboxExportRequest -Mailbox $Mailbox
    while ($exportRequest.Status -ne 'Completed') {  
        Start-Sleep -Seconds 10
        $exportRequest = Get-MailboxExportRequest -Mailbox $Mailbox
    }

    Write-Host "Export of mailbox $Mailbox to $PSTFilePath completed."  
}

# Funktion zum Einrichten der Weiterleitung
function Set-MailboxForwarding {
    param (
        [string]$Mailbox,
        [string]$ForwardingAddress
    )

    # Set forwarding address
    Set-Mailbox -Identity $Mailbox -ForwardingSMTPAddress $ForwardingAddress -DeliverToMailboxAndForward $true

    Write-Host "Forwarding for mailbox $Mailbox to $ForwardingAddress set."  
}

# Hauptskript
try {
    # Export the mailbox to PST
    Export-MailboxToPST -Mailbox $Mailbox -ExportPath $ExportPath

    # Set the forwarding address
    Set-MailboxForwarding -Mailbox $Mailbox -ForwardingAddress $ForwardingAddress

    Write-Host "Mailbox $Mailbox has been exported to PST and forwarding set to $ForwardingAddress."  
}
catch {
    Write-Error "An error occurred: $_"  
}

mit folgenden Parametern:

.\PstImportMappingFile.ps1 -Mailbox "user@domain.de" -ExportPath "C:\Exports" -ForwardingAdress "User@domain.com"  


Kann sich das jemand mal durchlesen und mir feedback / Verbesserungsvorschläge geben?

Wäre sehr dankbar! face-smile.


Danke Danke und liebe Grüße
Meow

Content-Key: 9886332795

Url: https://administrator.de/contentid/9886332795

Printed on: July 27, 2024 at 11:07 o'clock

Member: pebcak7123
pebcak7123 Jul 09, 2024 at 08:29:23 (UTC)
Goto Top
Nur kurz rübergeschaut, das try ... catch im "Hauptscript" wird so nicht funktionieren da deine functions nie einen error zurückgeben. Da müsste auch in die functions selber nen try .. catch block der bei catch über throw den fehler zurückgibt. Muss auch geschaut werden welche in den functions verwendeten comandlets überhaupt nen stoppenden fehler zurückgeben ohne "-ErrorAction Stop"