dsccommunity / DscWorkshop

Blueprint for a full featured DSC project for Push / Pull with or without CI/CD
MIT License
202 stars 163 forks source link

Mof Compare Script #89

Open DeBasti opened 3 years ago

DeBasti commented 3 years ago

Script checks if newly built mof files have differences to the mof files on the pull server. Necessary exeptions are in line 17 counting from the bottom.

param(
    [Parameter(Mandatory=$false)]
    [String]$PullServer = ''
)

Write-Output "Testserver? $($UseTestServer)"

$teamName = $env:TEAM
Write-Output "Detected team name: $($teamName)" 

$buildMofsPath = (Join-Path -Path $env:ci_project_dir -ChildPath "$($teamName)\BuildOutput\MOF")
$oldMofsPath = "\\$($PullServer)\DscService\Configuration"

$buildMofs = Get-ChildItem -Path $buildMofsPath -Filter '*.mof' -File

foreach($mof in $buildMofs)
{
    $oldMof = Get-ChildItem -Path $oldMofsPath -Filter "$($mof.BaseName).mof" -File
    if($oldMof.Count -eq 0)
    {
        throw "Could not find mof file for $($mof.BaseName) in $($oldMofsPath)!"
    }
    elseif($oldMof.Count -ge 2)
    {
        throw "Found 2 or more mof file for $($mof.BaseName) in $($oldMofsPath)!"
    }

    $mofContent = Get-Content -Path $mof.FullName
    $index = $mofContent.IndexOf("instance of OMI_ConfigurationDocument")
    $lastLines = $mofContent.Length - $index
    $mofContent = $mofContent | Select-Object -Skip 6 | Select-Object -SkipLast $lastLines #Skip first and last lines as they contain the build date but no config data

    $oldMofContent = Get-Content -Path $oldMof.FullName
    $oldIndex = $oldMofContent.IndexOf("instance of OMI_ConfigurationDocument")
    $oldLastLines = $oldMofContent.Length - $oldIndex
    $oldMofContent = $oldMofContent | Select-Object -Skip 6 | Select-Object -SkipLast $oldLastLines

    $res = Compare-Object -ReferenceObject $mofContent -DifferenceObject $oldMofContent

    [string[]]$out = @()
    #Filter out some config data that changes after every build although yml files are the same
    foreach($r in $res.InputObject)
    { #Matches: Password, BuildDate&Time, Path to Dsc Modules (incl. runner path), StartTime of scheduled Task (varies based on build time), version, buildnumber, git commit id
        if(($r -match '^Password = ') -or ($r -match '^\s{4}\"\d{2}.\d{2}.\d{4}\s\d{2}:\d{2}:\d{2}\"$') -or ($r -match '^ SourceInfo = ') -or ($r -match '^ StartTime = \"\d{14}.\d{6}\+\d{3}\";$') -or ($r -match '^\s{4}\"\d{1,2}.\d{1,2}.\d{1,4}\"$') -or ($r -match '^\s{4}\"\d{1,4}\"$') -or ($r -match '^\s{4}\"\w{40}\"$'))
        {        
        }else
        {
            $out += $r
        }  
    }
    if($out.Count -ne 0)
    {
        Write-Output "Differences in MOF files $($mof.BaseName) found (see below)! Deploy will be triggered..."
        $out | foreach-object {
            Write-Output $_
        }
        $SKIPDEPLOY = $false #Deploy will be triggered
    }# If no mof has differences, do nothing as default value for SKIPDEPLOY is "True"
}
if($SKIPDEPLOY -ne $false)
{
    $SKIPDEPLOY = $true
    Write-Output "No differences in MOF files found. Skipping deploy..."
}
#$SKIPDEPLOY | Out-File -FilePath envVars.env
Add-Content -Path envVars.env -Value "SKIPDEPLOY=$($SKIPDEPLOY)"`
raandree commented 3 years ago

This is going to be part of the deploy task when running in a Azure DevOps Release Pipeline.