Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mof Compare Script #89

Open
DeBasti opened this issue Dec 8, 2020 · 1 comment
Open

Mof Compare Script #89

DeBasti opened this issue Dec 8, 2020 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@DeBasti
Copy link

DeBasti commented Dec 8, 2020

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 raandree self-assigned this Dec 9, 2020
@raandree raandree added the enhancement New feature or request label Dec 9, 2020
@raandree
Copy link
Contributor

raandree commented Dec 9, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants