locked
comparing 2 ps1 files to identify is there is missing variables? RRS feed

  • Question

  • Hi,

    I'm looking for a way to compare 2 ps1 files if they have the same number of variables.

    I'm using a ps1 file which contains a lot of variables to do deployments (server names , folders etc...)

    I'm sending this to the integrator team, they edit the names based on the target environments (QA, UAT, PROD...)

    So I want to insure that the variable files contains the same number of variable, if 1 variable is missing , this can cause issues.

    how to do this?

    thanks.

    Tuesday, May 20, 2014 2:20 PM

Answers

  • oh... cool... thanks... this appear to works fine...

    $sfile = Get-Content variables.dev.ps1
    $errors = $null
    $vars1 = [system.management.automation.psparser]::Tokenize($sfile,[ref]$errors) | Where-Object {$_.Type -eq "Variable"} |Select-Object Content 
    
    $sfile2 = Get-Content variables.prod.ps1
    $vars2 = [system.management.automation.psparser]::Tokenize($sfile2,[ref]$errors) | Where-Object {$_.Type -eq "Variable"} |Select-Object Content
    
    if($vars1.Count -ne $vars2.Count)
    {
    	throw("Variables missing")
    }
    

    • Marked as answer by Bill_Stewart Tuesday, May 20, 2014 6:33 PM
    Tuesday, May 20, 2014 2:49 PM

All replies

  • It's probably possible; it's just a small matter of programming.

    The usual answer is not to hard-code variable values in scripts but rather pass parameters so that you don't need to edit them.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 20, 2014 2:29 PM
  • Hi Willgart,

    the easiest way I know of is integrated into Powershell Studio 2014 from Sapien (the hands down best Powershell Scripting Environment out there, so if you don't have it, get it :) ) : "Tools" > "File" > "Find in Files"

    Except for that, you could probably do some simple string processing or use some free tool for it.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Tuesday, May 20, 2014 2:30 PM
  • unfortunately I cant pass all of them in parameters.

    the list is too big

    and there is multiple "variable files" to load, because each sub element deployed with my deployment system is managed by different team.

    so each team is responsible to deliver its own "variable.ps1" file.

    the integrator team just maintain their "variable.ps1" files and run a sequencer script which do all the job... (load the variables, test the variables, run each sub script, like deploying a DB, an IIS etc...)

    I try to create a code which load the variables files into a separated process, count the number of variables and store it in a txt file. then I can compare the resulting txt file.

    but how to execute in a dedicated process?

    does the invoke-command can do the job?

    Tuesday, May 20, 2014 2:37 PM
  • A script can be loaded into the tokenizer and the script objects can be compared with Compare-Object just like anything else.

    Reading the original post suggests that the OP is asking how to compare the data in the two scripts.  This can aslo be easily done with the tokenizer but we need examples of the data (variables) to be compared.


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Tuesday, May 20, 2014 6:34 PM
    Tuesday, May 20, 2014 2:39 PM
  • oh... cool... thanks... this appear to works fine...

    $sfile = Get-Content variables.dev.ps1
    $errors = $null
    $vars1 = [system.management.automation.psparser]::Tokenize($sfile,[ref]$errors) | Where-Object {$_.Type -eq "Variable"} |Select-Object Content 
    
    $sfile2 = Get-Content variables.prod.ps1
    $vars2 = [system.management.automation.psparser]::Tokenize($sfile2,[ref]$errors) | Where-Object {$_.Type -eq "Variable"} |Select-Object Content
    
    if($vars1.Count -ne $vars2.Count)
    {
    	throw("Variables missing")
    }
    

    • Marked as answer by Bill_Stewart Tuesday, May 20, 2014 6:33 PM
    Tuesday, May 20, 2014 2:49 PM