Install-Dynamics365Update.psm1

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function Install-Dynamics365Update {
    param (
        [parameter(Position=0,
        Mandatory=$true)]
        [string]
        $MediaDir,
        [string]
        $LogFilePath = $null,
        [ValidateRange(1,3600)]
        [int]
        $LogFilePullIntervalInSeconds = 30,
        [switch]
        $LogFilePullToOutput = $False
    )
    $setupFilePath = "$mediaDir\CrmUpdateWrapper.exe";
    $fileVersion = [version]( Get-Command $setupFilePath ).FileVersionInfo.FileVersion;
    Write-Output "Version of software to be installed: $($fileVersion.ToString())";
    $msCRMRegistryValues = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\MSCRM -ErrorAction Ignore;
    if ( !$msCRMRegistryValues ) {
        $errorMessage = "Dynamics 365 Server for Customer Engagement is not installed on this machine";
        Write-Output $errorMessage;
        Throw $errorMessage;
    }
    $installedVersion = Get-Dynamics365ServerVersion;
    Write-Output "Currently installed: $($installedVersion.ToString())";
    if ( $fileVersion.Major -ne $installedVersion.Major ) {
        $errorMessage = "Major version of this update does not correspond to major version of installed software";
        Write-Output $errorMessage;
        Throw $errorMessage;
    }
    if ( $fileVersion -lt $installedVersion ) {
        $errorMessage = "Version of this update is lower than version of installed software";
        Write-Output $errorMessage;
        Throw $errorMessage;
    }
    if ( $fileVersion -eq $installedVersion ) {
        Write-Output "This version is already installed";
        return;
    }
    if([String]::IsNullOrEmpty($logFilePath) -eq $True) {
        $timeStamp = ( Get-Date -Format u ).Replace(" ","-").Replace(":","-");
        $logFilePath = "$env:APPDATA\Microsoft\MSCRM\Logs\DynamicsUpdateInstallationLog_$timeStamp.txt";
    }

    Write-Output "$(Get-Date) Starting $setupFilePath";
    $installCrmScript = {
        param( $setupFilePath, $logFilePath );
        Write-Output "Start-Process '$setupFilePath' -ArgumentList '/q /log $logFilePath /norestart' -Wait;";
        Start-Process "$setupFilePath" -ArgumentList "/q /log $logFilePath /norestart" -Wait;
    }
    $job = Start-Job -ScriptBlock $installCrmScript -ArgumentList $setupFilePath, $logFilePath;
    Write-Output "$(Get-Date) Started installation job, log will be saved in $logFilePath";
    $lastLinesCount = 0;
    $startTime = Get-Date;
    $logReadIterationsLeft = 2;
    Start-Sleep $logFilePullIntervalInSeconds;
    do {
        $elapsedTime = $( Get-Date ) - $startTime;
        $elapsedString = "{0:HH:mm:ss}" -f ( [datetime]$elapsedTime.Ticks );
        Write-Output "$(Get-Date) Elapsed $elapsedString. Waiting until CRM update installation job is done, sleeping $logFilePullIntervalInSeconds sec";
        Start-Sleep $logFilePullIntervalInSeconds;
        if ( $job.State -eq "Completed" ) {
            Write-Output "$(Get-Date) Log read iterations left: $logReadIterationsLeft"
            $logReadIterationsLeft--;
            Start-Sleep 30;
        }
        if(($logFilePullToOutput -eq $True) -and ((Test-Path $logFilePath) -eq $True)) {

            $linesCount    = (Get-Content $logFilePath | Measure-Object -Line).Lines;
            $newLinesCount = $linesCount - $lastLinesCount;

            if($newLinesCount -gt 0) {
                Write-Output "$(Get-Date) - new logs: $newLinesCount lines";
                $lines = Get-Content $logFilePath | Select-Object -First $newLinesCount -Skip $lastLinesCount;

                foreach($line in $lines) {
                    Write-Output $line;
                }
            } else {
                Write-Output "$(Get-Date) - no new logs";
            }

            $lastLinesCount = $linesCount;
        }
    } until ( ( $job.State -eq "Completed" ) -and ( $logReadIterationsLeft -le 0 ) )

    $elapsedTime = $( Get-Date ) - $startTime;
    $elapsedString = "{0:HH:mm:ss}" -f ( [datetime]$elapsedTime.Ticks );
    Write-Output "$(Get-Date) Elapsed $elapsedString. Job is complete, output:";
    Write-Output ( Receive-Job $job );
    Remove-Job $job;

    $installedVersion = Get-Dynamics365ServerVersion;
    Write-Output "Currently installed: $($installedVersion.ToString())";
    if ( $fileVersion -ne $installedVersion ) {
        if( (Test-Path $logFilePath) -eq $True) {
            $errorLines = Get-Content $logFilePath | Select-String -Pattern "Error" -SimpleMatch;
            if($null -ne $errorLines) {
                Write-Output "Errors from install log: $logFilePath";
                foreach($errorLine in $errorLines) {
                    Write-Output $errorLine;
                }
            }
        }
        $errorMessage = "Version of installed software is not the same as installed update";
        Write-Output $errorMessage;
        Throw $errorMessage;
    } else {
        Write-Output "Installation is finished and verified successfully";
    }
}