Install-Dynamics365ReportingExtensions.psm1

function Install-Dynamics365ReportingExtensions {
    param (
        [Parameter(Mandatory=$true)]
        [string]
        $MediaDir,
        [string]
        $InstanceName,
        [string]
        $ConfigDBServer,
        [switch]
        $AutoGroupManagementOff,
        [switch]
        $MUOptin = $false,
        [string]
        $LogFilePath = $null,
        [ValidateRange(1,3600)]
        [int]
        $LogFilePullIntervalInSeconds = 30,
        [switch]
        $LogFilePullToOutput = $False
    )
    $AutoGroupManagementOffBool = [bool]$AutoGroupManagementOff;
    $setupFilePath = "$mediaDir\SetupSrsDataConnector.exe";
    $fileVersion = ( Get-Command $setupFilePath ).FileVersionInfo.FileVersionRaw.ToString();
    Write-Output "Version of software to be installed: $fileVersion";
    $languageCode = ( Get-Item $mediaDir\LangPacks\* ).Name;
    Write-Output "Language code of software to be installed: $languageCode";
    $Dynamics365Resources | Get-Member -MemberType NoteProperty | Where-Object { $Dynamics365Resources.( $_.Name ).ReportingExtensionsIdentifyingNumber } | ForEach-Object {
        if ( ( $Dynamics365Resources.( $_.Name ).MediaFileVersion -eq $fileVersion ) -and ( $Dynamics365Resources.( $_.Name ).LanguageCode -eq $languageCode ) ) { $foundFileResource = $_.Name }
    }
    $installedProducts = Get-WmiObject Win32_Product -ComputerName $env:COMPUTERNAME | ForEach-Object { $_.IdentifyingNumber }
    if ( $foundFileResource )
    {
        Write-Output "Found corresponding resource in the catalog: $foundFileResource";
        $expectedProductIdentifyingNumber = $Dynamics365Resources.$foundFileResource.ReportingExtensionsIdentifyingNumber;
    } else {
        Write-Output "Corresponding resource is not found in the catalog. Installation verification will be skipped";
    }
    if ( $expectedProductIdentifyingNumber )
    {
        $isInstalled = $installedProducts -contains "{$expectedProductIdentifyingNumber}";
    } else {
        Write-Output "IdentifyingNumber is not specified for this product, installation verification will be skipped";
    }
    # Starting installation only when not installed or if id is not found in catalog.
    if ( !$expectedProductIdentifyingNumber -or !$isInstalled ) {
        $xml = [xml]"";
        $crmSetupElement = $xml.CreateElement( "CRMSetup" );
            $srsDataConnectorElement = $xml.CreateElement( "srsdataconnector" );
                $configDBServerElement = $xml.CreateElement( "configdbserver" );
                    $configDBServerElement.InnerText = $configDBServer;
                $srsDataConnectorElement.AppendChild( $configDBServerElement ) | Out-Null;
                $autoGroupManagementOffElement = $xml.CreateElement( "autogroupmanagementoff" );
                    $autoGroupManagementOffElement.InnerText = [int]$AutoGroupManagementOffBool;
                $srsDataConnectorElement.AppendChild( $autoGroupManagementOffElement ) | Out-Null;
                if ( $InstanceName ) {
                    $instanceNameElement = $xml.CreateElement( "instancename" );
                        $instanceNameElement.InnerText = $InstanceName;
                    $srsDataConnectorElement.AppendChild( $instanceNameElement ) | Out-Null;
                }
                $patchElement = $xml.CreateElement( "Patch" );
                    $patchElement.SetAttribute( "Update", $false ) | Out-Null;
                $srsDataConnectorElement.AppendChild( $patchElement ) | Out-Null;
                $MUOptinElement = $xml.CreateElement( "muoptin" );
                    $MUOptinElement.SetAttribute( "optin", $MUOptin.ToString().ToLower() ) | Out-Null;
                $srsDataConnectorElement.AppendChild( $MUOptinElement ) | Out-Null;
            $crmSetupElement.AppendChild( $srsDataConnectorElement ) | Out-Null;
        $xml.AppendChild( $crmSetupElement ) | Out-Null;

        $stringWriter = New-Object System.IO.StringWriter;
        $xmlWriter = New-Object System.Xml.XmlTextWriter $stringWriter;
        $xmlWriter.Formatting = "indented";
        $xml.WriteTo( $xmlWriter );
        $xmlWriter.Flush();
        $stringWriter.Flush();

        $localInstallationScriptBlock = {
            param( $setupFilePath, $xmlConfig, $logFilePath, $logFilePullIntervalInSeconds, $logFilePullToOutput)
            $tempFileName = [guid]::NewGuid().Guid;
            $tempFilePath = "$env:Temp\$tempFileName.xml";
            Write-Output "$(Get-Date) Saving configuration temporary to $tempFilePath";
            Set-Content -Path $tempFilePath -Value $xmlConfig -Encoding utf8;
            Get-Content $tempFilePath | Write-Debug;
            Write-Output "$(Get-Date) Starting $setupFilePath";
            $installCrmScript = {
                param( $setupFilePath, $tempFilePath, $logFilePath );
                Write-Output "Start-Process '$setupFilePath' -ArgumentList '/Q /config $tempFilePath /L $logFilePath' -Wait;";
                Start-Process "$setupFilePath" -ArgumentList "/Q /config $tempFilePath /L $logFilePath" -Wait;
            }
            $job = Start-Job -ScriptBlock $installCrmScript -ArgumentList $setupFilePath, $tempFilePath, $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 reporting extensions 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;
            Start-Sleep 10;
            Remove-Item $tempFilePath;
        }
        if([String]::IsNullOrEmpty($logFilePath) -eq $True) {
            $timeStamp = ( Get-Date -Format u ).Replace(" ","-").Replace(":","-");
            $logFilePath = "$env:APPDATA\Microsoft\MSCRM\Logs\DynamicsReportingExtensionsInstallationLog_$timeStamp.txt";
        }
        Invoke-Command -ScriptBlock $localInstallationScriptBlock `
            -ArgumentList $setupFilePath, $stringWriter.ToString(), $logFilePath, $logFilePullIntervalInSeconds, $logFilePullToOutput;
        Write-Output "The following products were installed:"
        Get-WmiObject Win32_Product -ComputerName $env:COMPUTERNAME | ForEach-Object {
            if ( !$expectedProductIdentifyingNumber -or ( $_.IdentifyingNumber -eq "{$expectedProductIdentifyingNumber}" ) ) {
                $isInstalled = $true;
            }
            if ( !( $installedProducts -contains $_.IdentifyingNumber ) ) {
                Write-Output $_.IdentifyingNumber, $_.Name;
            }
        }
        if ( $expectedProductIdentifyingNumber )
        {
            if ( $isInstalled ) {
                Write-Output "Installation is finished and verified successfully";
            } else {
                $getErrorLogScriptBlock = {
                    param( $logFilePath )
                    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;
                            }
                        }
                    }
                }
                Invoke-Command -ScriptBlock $getErrorLogScriptBlock `
                    -ArgumentList $logFilePath
                $errorMessage = "Installation job finished but the product $expectedProductIdentifyingNumber is still not installed";

                Write-Output $errorMessage;
                Throw $errorMessage;
            }
        } else {
            Write-Output "Installation is finished but verification cannot be done without IdentifyingNumber specified.";
        }
    } else {
        Write-Output "Product is already installed, skipping";
    }
}