1

Hello I have a json file below:

{
  "supports": {},
  "dependencies": {
    "AST.NxTestware.main": "1.1.0",
    "otherPackagenName": "7.7.7"
  },
  "frameworks": {
    ".NETPortable,Version=v4.5,Profile=Profile78": {}
  }
}

and I am trying to write a .ps1 powershell script that will look at every item under 'dependencies', check if the name matches, and if so, retrieve the version number.

but I am having trouble parsing through each item in 'dependencies', and cannot retrieve the package name string or version number

My ps1 looks like this:


$targetName = "otherPackagenName"
Write-Host "Trying to get version # of $targetName if it exists inside project.json"

# get json file contents
$json = Get-Content .\project.json | ConvertFrom-Json

# retrieve dependencies 
$dependencies = $json.dependencies
Write-Host "dependencies = $dependencies"

# iterate through each dependency
foreach ($dep in $dependencies) {
    Write-Host "dep = $dep"

    # try to get dependency name (attempt1)
    $depName1 = $dep[0]
    Write-Host "depName1 = $depName1"
    
    # try to get dependency name (attempt2)
    $depName2 = $dep.Name
    Write-Host "depName2 = $depName2"

    if($depName1 -eq $targetName) {
        write-host "Found it! now get version number"
    } else {
        write-host "Did not find our package"
    }  
}



output shows it correctly retrieves the json key/value pair, but I cant figure out how to retrieve the package name and version number, as every thing ive tried just returns blank:

Trying to get version # of otherPackagenName if it exists inside project.json
dependencies = @{AST.NxTestware.main=1.1.0; otherPackagenName=7.7.7}
dep = @{AST.NxTestware.main=1.1.0; otherPackagenName=7.7.7}
depName1 = @{AST.NxTestware.main=1.1.0; otherPackagenName=7.7.7}
depName2 =
Did not find our package

my dependencies is coming through like so:

@{AST.NxTestware.main=1.1.0; otherPackagenName=7.7.7}

How can I iterate through this object?

How can I retrieve the key ('AST.NxTestware.main') and value ('1.1.0') for each dependency?

POVR2
  • 87
  • 10

1 Answers1

0

I'm guessing you're looking for this:

$json.Dependencies | ForEach-Object {
    $prop = $_.PSObject.Properties

    [pscustomobject]@{
        DependencyName = $prop.Name
        Version        = [version] $prop.Value
    }
}

You can get an object's property Name and property Value by accessing the object's PSObject member, from there you can reference the Properties property of each object.

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • how do i retrieve these values from the `[pscustomobject]@{` ? can i print out 'DependencyName" and 'Version" ? I need to make a function which returns these values – POVR2 Jun 07 '22 at 22:44
  • @POVR2 the code as is will print to the console, you don't really need to use `Write-Host` to print to the console – Santiago Squarzon Jun 07 '22 at 22:46
  • the code you provided does not write to console `AST.NxTestware.main` or `1.1.0` – POVR2 Jun 08 '22 at 16:58