internal/functions/Import-EWSDLL.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Function Import-EWSDLL {
    <#
    .SYNOPSIS
    Loads EWS DLL into the session
     
    .DESCRIPTION
    Search and Loads EWS DLL into the session
     
    .EXAMPLE
    PS C:\> Import-EWSDLL
    Search and Loads EWS DLL into the session
     
    #>

    [Cmdletbinding()]
    param(
        # Parameters
    )
    Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] This script requires at least EWS API 2.1" -DefaultColor Yellow
    
    # Locating DLL location either in working path, in EWS API 2.1 path or in EWS API 2.2 path
    $Directory = ".\"
    $EWS = Join-Path $Directory "Microsoft.Exchange.WebServices.dll"
    $test = Test-Path -Path $EWS
    if ($test -eq $False) {
        Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] EWS DLL in local path not found" -DefaultColor Cyan
        $test2 = Test-Path -Path "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.*\Microsoft.Exchange.WebServices.dll"
        if ($test2 -eq $False) {
            Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] EWS 2.1 not found" -DefaultColor Cyan
            $test3 = Test-Path -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.*\Microsoft.Exchange.WebServices.dll"
            if ($test3 -eq $False) {
                Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] EWS 2.2 not found" -DefaultColor Cyan
            }
            else {
                Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] EWS 2.2 found" -DefaultColor Cyan
            }
        }
        else {
            Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] EWS 2.1 found" -DefaultColor Cyan
        }
    }
    else {
        Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] EWS DLL found in local path" -DefaultColor Cyan
    }

    if ($test -eq $False -and $test2 -eq $False -and $test3 -eq $False) {
        $EwsMessage = @"
You don't seem to have EWS API dll file 'Microsoft.Exchange.WebServices.dll' in the same Directory of this script
please get a copy of the file or download the whole API from:
https://www.microsoft.com/en-us/download/details.aspx?id=42951
         
we will open your browser in 10 seconds automatically directly to this URL
"@

        Write-PSFHostColor -String $EwsMessage -DefaultColor Yellow
        Start-sleep -Seconds 10
        Start-Process "https://www.microsoft.com/en-us/download/details.aspx?id=42951"

        return
    }
    Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] EWS API detected. All good!" -DefaultColor Cyan

    if ($test -eq $True) {
        Add-Type -Path $EWS
        Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] Using EWS DLL in local path" -DefaultColor Cyan
    }
    elseif ($test2 -eq $True) {
        Add-Type -Path "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.*\Microsoft.Exchange.WebServices.dll"
        Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] Using EWS 2.1" -DefaultColor Cyan
    }
    elseif ($test3 -eq $True) {
        Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.*\Microsoft.Exchange.WebServices.dll"
        Write-PSFHostColor -String "[$((Get-Date).ToString("HH:mm:ss"))] Using EWS 2.2" -DefaultColor Cyan
    }
}