Search code examples
powershelloutlookoffice365signatureoutlook-web-app

Upload local users outlook client signature to Office 365 OWA (remotely) - On-prem exchange to Exchange online


We are in the process of migrating user mailboxes from on-prem exchange to office 365. Since we are only doing few users at a time, our requirements were to sync local outlook client signatures into office 365 OWA portal. So user sees the same signature on their OWA portal.

Has anyone done this?


Solution

  • I have figured it out. So I have created a script to help our migration team to upload local outlook client signature to office 365.

    You need to have admin access to user PC and exchange online admin privileges such as Global Administrator.

    Here is the code:


    write-host "`n`n`n`n"
    
    #initializing variables
    $username = $null
    $computer = $null
    $path = $null
    $number = $null
    $i = 0
    $select = $null
    $signs = $null
    $chkerr = $false
    $session = $null
    
    #Run a continous loop
    do {
    
        #if powershell is not connected to office 365 then run the connection code below
        if ($session.State -ne "opened") {
    
            Write-Host "`nChecking connection to Office 365: " -NoNewline
            Write-Host "Inactive!" -ForegroundColor white -BackgroundColor red
    
            Write-Host "`nConnecting to Office 365 account...`n"
    
            #Get user office 365 credentials 
            $UserCredential = Get-Credential
    
            #create session
            $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
    
            #start the session to office 365
            Import-PSSession $Session
            Sleep 4
        }
    
        #if the session to 365 is success then run the code below
        if ($?) {
            Write-Host "`nChecking connection to Office 365: " -NoNewline
            Write-Host "Active!" -ForegroundColor Black -BackgroundColor green
    
            #Start a loop to prompt for username and machinename
            do {
    
                #this variable is used below and set as false if there is an error. Also, shows a message that user need to re-enter the information 
                $chkerr = $false
                if ($chkerr) {write-host "`nPlease try again..." -BackgroundColor yellow -ForegroundColor black}
    
                #store username and machine name
                $username = Read-Host "`n`nPlease enter user name"
                $computer = Read-Host "Please enter computer name"
    
                #if machine is not pingable then show an error
                if (!(Test-Connection $computer -Count 1) ) {Write-Host "`nError: Computer is offline or name is incorrect" -ForegroundColor white -BackgroundColor red; $chkerr = $true}
                else {
                    #if machine is pingable then check if signature direcotry exists
                    if (!(Test-Path "\\$computer\c$\Users\$username\AppData\Roaming\Microsoft\Signatures\")) { Write-Host "`nError: Outlook signature directory does not exists `nOR `nUsername is incorrect!" -ForegroundColor white -BackgroundColor red;$chkerr = $true}
                }
    
                #show error msg if username or machine name is left blank
                if ($username -eq $null -or !$username -or $computer -eq $null -OR !$computer) {Write-Host "`nError: Username or computer name cannot be left empty" -ForegroundColor white -BackgroundColor red;$chkerr = $true }
    
    
            #repeatedly ask for username and machine name until user gets it right or moves on to next username and machine name
            } while ($chkerr)
    
            #set the path to signature directory
            $path = "\\$computer\c$\Users\$username\AppData\Roaming\Microsoft\Signatures"
    
            #get a list of signature files and store them in a variable
            $number = Get-ChildItem "$path\*.htm" 
    
            #check if there are any signature files at all
            if ($number -or $number -ne $null) {
    
                #start a loop
                do { 
    
                    #if there are multiple signature files found then show them on screen
                    Write-Host "`nSelect one for following file:`n"
                    for ([int]$i=0;$i -lt $number.count; $i++) {
    
                        Write-Host ("$i" +' --> ' + $number[$i].name)
                    }
    
                    #if there is more than one signature file then prompt user to select one
                    if ($number.count -gt 1) {
                        $select = Read-Host "`nPlease enter your selection"
    
                        #show error if user enters a number that is of selection
                        if ([int]$select+1 -gt [int]$number.Count) {Write-Host "`nError: Please enter a valid selections!" -ForegroundColor white -BackgroundColor red}
                    } else {
    
                        #if only one signature file is found then do not prompt and select that one file as default
                        $select = "0"
                        Write-Host "`nOnly one file found, selecting default file -->"  $number[$select].name -ForegroundColor black -BackgroundColor yellow
                    }
                #repeat the loop until user selects atleast one signature file
                } while  ([int]$select+1 -gt [int]$number.Count)
    
                #show the selected signature file
                Write-Host "You have selected following signature file:" $number[$select].fullname
                sleep 3
    
                #grab the html file and store it into a variable
                Write-Host "`nImporting signatures into powershell"
                $signs = Get-Content $number[$select].fullname
    
                #show error if the signature import fails
                if (!$?) {Write-Host "`nError: Unable to import signature into powershell!" -ForegroundColor white -BackgroundColor red}
                else { 
    
                    Write-Host "`nSuccess!" -ForegroundColor Black -BackgroundColor green
                    Write-Host "`nImporting signatures to Office 365 account of $username"
    
                    #import the signature into users OWA and set it to be used during replies, forwards and message creations.
                    Get-Mailbox $username | Set-MailboxMessageConfiguration -SignatureHtml $signs -AutoAddSignature:$true -AutoAddSignatureOnReply:$true -AutoAddSignatureOnMobile:$true
                    if (!$?) {Write-Host "`nError: Unable to import signature into Office 365 account!" -ForegroundColor white -BackgroundColor red}
                    else {Write-Host "`nSuccess!" -ForegroundColor Black -BackgroundColor green}
                }
            }
            else {
                #show error if no signature files are found
                Write-Host "`nError: No signature files found!" -ForegroundColor white -BackgroundColor red
            }
    
        } 
        else {
            # show error if we are unable to connect to office 365 account.
            Write-Host "`nError: Connection to office 365 is required for this script to work" -ForegroundColor white -BackgroundColor red
        }
    
    } while ($true)
    


    Successful test of Output:

    Making connection: enter image description here

    After successfull connection: enter image description here

    After entering username and password: enter image description here

    After making the selection: enter image description here

    OWA view: enter image description here


    Error checking against user input:

    Entering wrong office 365 credentials: enter image description here

    Entering correct computer name but wrong username: enter image description here

    Entering correct username but wrong computer name: enter image description here

    Entering wrong selecting that is out of the provided set of options: enter image description here


    You can also modify the script to grab input from an excel sheet.