Home > OS >  I need PowerShell script to read and increment value inside json file everytime after checkout i.e.
I need PowerShell script to read and increment value inside json file everytime after checkout i.e.

Time:06-04

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "adminUsername": {
      "value": "APORIA"
    },
    "adminPassword": {
      "value": "Password@1234"
    },
    "VMName": {
      "value": "AEU-EHSSAS19"
    },
    "VMSize": {
      "value": "Standard_B2ms"
    },
    "LabName": {
      "value": "EUW-TSE-D-SELFSERV"
    },
      "windowsOSVersion": {
      "value": "2016-Datacenter"
    }     
    }
  }

CodePudding user response:

PowerShell script to read and increment value inside json file everytime after checkout

Refer to the following PowerShell script:

$pathToJson = "path/test.json"
$a = Get-Content $pathToJson | ConvertFrom-Json

$VMName = $a.parameters.VMName.value
echo $VMname
$len = $VMName.Length
$number= "$VMName".Substring($len - 2, 2)
echo $number

$number = [int]$number 1

echo $number

$NewVMName = $VMName.replace('19',$number)

echo $NewVMName

$a.parameters.VMName.value = $NewVMName

$a | ConvertTo-Json | set-content $pathToJson

The PowerShell script will find the value of the VMName field. And then it will get the number of in the string and Increment 1.

Result:

enter image description here

  • Related