Home > Blockchain >  How to replace a string in Powershell with blank test
How to replace a string in Powershell with blank test

Time:09-16

Hi I have some output from a json file for which i wish to replace the text so i get the desired results for just software version but i cant figure out the correct replace query current output from convertFromJson to the object i have

for example test json

{
    "variables": {
        "test0": "",
        "test1": "",
        "test3": "",

    },
    "builders": [
        {
            "type": "test4",
            "client_id": "{{user `clientId`}}",
        }
    ],
    "provisioners": [
        {
            "type": "powershell",
            "inline": [
                packer install test-package-x86 --version 3.2.24013.4 -m -y --ignoredetectedreboot --debug --ignorepackagecodes"
            ],
            "max_retries": 5,
            "pause_before": "60s"
        },

    ]
}
packer install test-package-x86 --version 3.2.24013.4 -y --debug --ignorepackagecodes
packer install test-package2-x64 --version 4.5.2.65007 -m -y  --debug --ignorepackagecodes
packer install test-package2-x64 --version 5.7.1.541 -m -y  --debug --ignorepackagecodes
packer install test-package2-x64 --version 3.8.2.3774 -m -y --debug --ignorepackagecodes
packer install test-package4-x64 --version 2.9.1.3940 -m -y --debug --ignorepackagecodes
packer install test-package2-x86 --version 1.11.2.2421 -m -y  --debug --ignorepackagecodes
packer install prod-package4-x64 --version 2.9.1.3940 -m -y --debug --ignorepackagecodes
packer install prod-package2-x86 --version 1.11.2.2421 -m -y  --debug --ignorepackagecodes

Id like to just list the following, also is there any way to add fields such as softwareName and Version e.g.

softwareName     Version
------------     -----------
test-package-x86 3.2.24013.4
test-package2-x64 4.5.2.65007
test-package2-x64 5.7.1.541
test-package2-x64 3.8.2.3774 
test-package4-x64 2.9.1.3940 
test-package2-x86 1.11.2.2421

currently I have the following which gives me the lines i want in the example above, its just not replacing the strings i want and formatting as a table. I have been looking at implementing the replace function but i am not sure where to add this.

$software = $jsonfile.provisioners.scripts | Where-Object {$_ -match 'test-package'}

for example this is not working

$software = $jsonfile.provisioners.scripts | Where-Object {$_ -match 'test-package' -replace "packer install*" -replace "-m*" }

CodePudding user response:

You can use the -match operator along with the $Matches automatic variable to extract the relevant details and construct objects from each line:

$lines = @(
  'packer install test-package-x86 --version 3.2.24013.4 -y --debug --ignorepackagecodes'
  'packer install test-package2-x64 --version 4.5.2.65007 -m -y  --debug --ignorepackagecodes'
  'packer install test-package2-x64 --version 5.7.1.541 -m -y  --debug --ignorepackagecodes'
  'packer install test-package2-x64 --version 3.8.2.3774 -m -y --debug --ignorepackagecodes'
  'packer install test-package4-x64 --version 2.9.1.3940 -m -y --debug --ignorepackagecodes'
  'packer install test-package2-x86 --version 1.11.2.2421 -m -y  --debug --ignorepackagecodes'
  'packer install prod-package4-x64 --version 2.9.1.3940 -m -y --debug --ignorepackagecodes'
  'packer install prod-package2-x86 --version 1.11.2.2421 -m -y  --debug --ignorepackagecodes'
)

$objects = foreach($line in $lines){
  if($line -match 'packer install (?<packageName>[\w-] ) --version (?<version>[\d.] ) '){
    [pscustomobject]@{
      PackageName = $Matches['packageName']
      Version     = $Matches['version'] -as [version]
    }
  }
}

$objects now contain the desired objects

  • Related