Ignoring files in Get-ChildItem from objects being loaded from JSON - json

I have a script that I am using to read a JSON file, read the paths, calculate the MD5 hash and then export them in a CSV file. The JSON file contains multiple objects. In another post, I laid out the structure of the JSON file and I'll post that here. Please note that I am not allowed to change this JSON.
{
"destinationpath": "C:\\Destination\\Mobile Phones\\"
"sourcepath": "C:\\Source\\Mobile Phones\\"
"OnePlus" : {
"files": [
{
"source": "6T",
"destination": "Model\\6T",
"log": "logfiles",
"version": "Version.txt",
}
]
}
"Samsung" : {
"files": [
{
"source": "S20",
"destination": "Galaxy\\S20",
"log": "logfiles",
"version": "Version.txt",
}
]
}
}
The destinationpath: C:\\Destination\\Mobile Phones\\, when added to, for example, Samsung it becomes: destinationpath: C:\\Destination\\Mobile Phones\\Galaxy\\S20.
The script that I have so far is this:
$JSON = Get-Content -Path file.json | ConvertFrom-Json
$JSONdestination = $JSON.destination
$JSONsource = $JSON.source
foreach ($i in $JSON.psobject.properties) {
foreach($i2 in $i.Value ){
$IgnoredFiles = #(
$logfiles = $i2.files.log
$newFiles = $i2.files.version
)
$destination = $JSON + $i2.files.destination
$source = $JSONdestination + $i2.files.source
echo $destination
Get-ChildItem -Path $destination -Recurse -Exclude $IgnoredFiles | Get-FileHash -Algorithm MD5 -ErrorAction SilentlyContinue | Export-csv -Append -Path "C:\Users\home\Downloads\hashes.csv"
}
In the Get-ChildItem portion, I want to exclude the "log" and "version" files so that they are not counted in the Get-FileHash. I have tried doing it this way as well:
Get-ChildItem -Path $destination -Recurse -Exclude $i2.files.log, $i2.files.version | Get-FileHash -Algorithm MD5 -ErrorAction SilentlyContinue | Export-csv -Append -Path "C:\Users\home\Downloads\hashes.csv"
However, it doesn't work either and the log and version files are still taken into account in the exported csv.

The json you show us is invalid (missing commas and commas too many..)
Once fixed into
{
"destinationpath": "C:\\Destination\\Mobile Phones\\",
"sourcepath": "C:\\Source\\Mobile Phones\\",
"OnePlus": {
"files": [{
"source": "6T",
"destination": "Model\\6T",
"log": "*.log",
"version": "Version.txt"
}]
},
"Samsung": {
"files": [{
"source": "S20",
"destination": "Galaxy\\S20",
"log": "*.log",
"version": "Version.txt"
}]
}
}
you can do this:
$JSON = Get-Content -Path file.json | ConvertFrom-Json
$JSONdestination = $JSON.destinationpath
$JSONsource = $JSON.sourcepath
$result = foreach ($item in ($JSON | Select-Object * -ExcludeProperty destinationpath, sourcepath)) {
$item.PSObject.Properties.Value | ForEach-Object {
$destPath = Join-Path -Path $JSONdestination -ChildPath $_.files.destination
$logFolder = Join-Path -Path $JSONdestination -ChildPath $_.files.log
$exclude = $_.files.version
Write-Host "Processing files in '$destPath'" -ForegroundColor Cyan
Get-ChildItem -Path $destPath -Recurse -Exclude $exclude -ErrorAction SilentlyContinue |
Where-Object {$_.FullName -notlike "$logFolder*" } |
Get-FileHash -Algorithm MD5
}
}
$result | Export-Csv -Path "C:\Users\home\Downloads\hashes.csv" -NoTypeInformation

Related

(Powershell) Read and change JSON value to a date

I'm trying to write a powershell script that could read a JSON, its extension is .pros, then change the project name from "test" to "test 12/12 13:24" or something similar. So far I was able to read it, but then I can't get to the project_name field.
my code:
$pros = Get-ChildItem -Path $house -Recurse -Include *.pros
$time = Get-Date -Format 'u'
$pog = Get-Content $pros -raw | ConvertFrom-Json
$pog.update | $_.project_name=$time
$pog | ConvertTo-Json -depth 32| set-content $pros
Write-Output $time
The .pros:
{
"py/object": "pros.conductor.project.Project",
"py/state": {
"project_name": "Marcos_Yuck_new",
"target": "v5",
"templates": {
"kernel": {
"location": "/home/dragon/.config/pros/templates/kernel#3.5.3",
"metadata": {

How to get particular values from JSON using powershell

I have to get the values from JSON file for my test report purpose.
Can anyone help me how get values of passes, failure from the below JSON data.
{
"stats": {
"suites": 2,
"tests": 4,
"passes": 3,
"pending": 0,
"failures": 0,
"start": "2021-11-05T15:58:26.817Z",
"end": "2021-11-05T15:59:26.701Z",
"duration": 55162,
"testsRegistered": 4,
"passPercent": 75,
"pendingPercent": 0,
"other": 0,
"hasOther": false,
"skipped": 1,
"hasSkipped": true
},
}
I have tried below code, but not getting correct result
$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.passes)"
write-host "failures : $($json.failures)"
Result:
$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.passes)"
write-host "failures : $($json.failures)"
passes :
failures :
Edit:
I have got the solution by adding below code
$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.stats.passes)"
write-host "failures : $($json.stats.failures)"
try this
$json= Get-Content -Raw -Path <jsonFile>.json | ConvertFrom-Json
//or
$json = $response | ConvertFrom-Json //if you have it from url
write-host "passes : $($json.passes)"
write-host "failures : $($json.failures)"
I have got the solution by adding below code
$json= Get-Content -Raw -Path 'C:\report.json' | ConvertFrom-Json
write-host "passes : $($json.stats.passes)"
write-host "failures : $($json.stats.failures)"
Result:
passes : 14
failures : 0

Manipulate JSON File with multiple objects using Powershell

I have a JSON File called index.json which looks like this :
{
"First": {
"href": "test/one two three.html",
"title": "title one"
},
"Second": {
"href": "test/test test/one two three four.html",
"title": "title two"
}
}
I want to write a powershell script to update the href of each object to replace the spaces with -.
The JSON file should looks like this:
{
"First": {
"href": "test/one-two-three.html",
"title": "title one"
},
"Second": {
"href": "test/test-test/one-two-three-four.html",
"title": "title two"
}
}
I got some help from this post:
Iterating through a JSON file PowerShell
I have already written a script to get all the href values, I dont know how to update the same in the original JSON file. My script looks like this:
function Get-ObjectMembers {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, ValueFromPipeline=$True)]
[PSCustomObject]$obj
)
$obj | Get-Member -MemberType NoteProperty | ForEach-Object {
$key = $_.Name
[PSCustomObject]#{Key = $key; Value = $obj."$key"}
}
}
$a = Get-Content 'index.json' -raw | ConvertFrom-Json | Get-ObjectMembers
foreach($i in $a){
$i.Value.href.Replace(" ","-")
}
I've done it this way.
$path='index.json'
$Content= (Get-Content $path -raw | ConvertFrom-Json)
$memberNames=( $Content | Get-Member -MemberType NoteProperty).Name
foreach($memberName in $memberNames){
($Content.$memberName.href)=($Content.$memberName.href).Replace(" ","-")
}
$Content | ConvertTo-Json | Out-File $path -Append

PowerShell Loop through nested json and remove property

I am getting some json data from a API, however I don't need most of this data. I am trying to remove some fields so that the when I save this data as a json file it isn't so large. I doesnt seem to be removing any of the fields I am trying to remove.
Code:
$Response = Invoke-RestMethod -Uri "https://mtgjson.com/api/v5/AllPrintings.json" -Method GET
$Obj = ConvertFrom-Json $Response
$Obj.PSObject.Properties.Remove('booster')
$Obj.PSObject.Properties.Remove('cards')
$Obj | ConvertTo-Json | Out-File ./All-Sets-Data.json -Force
Json:
{
"data": {
"10E": {
"baseSetSize": 383,
"block": "Core Set",
"booster": "#{default=}",
"cards": "",
"code": "10E",
...
},
"2ED": {
"baseSetSize": 302,
"block": "Core Set",
"booster": "#{default=}",
"cards": "",
"code": "2ED",
...
},
"2XM": {
"baseSetSize": 332,
"booster": "#{default=}",
"cards": "",
"code": "2XM",
...
},
...
}
}
$Obj.data.'10E'.PSObject.Properties.Remove('booster')
$Obj.data.'10E'.PSObject.Properties.Remove('cards')
$Obj.data.'2ED'.PSObject.Properties.Remove('booster')
# and so on
The above code snippet should work. However, you can do all in a one step by calling the following (recursive) function RemoveProperty:
Function RemoveProperty {
param (
# A PSCustomObject
[Parameter( Mandatory, ValueFromPipeline )] $Object,
# A list of property names to remove
[Parameter( Mandatory )] [string[]]$PropList,
# recurse?
[Parameter()] [Switch]$Recurse
)
# Write-Host $Object -ForegroundColor Cyan
foreach ( $Prop in $PropList ) {
$Object.PSObject.Properties.Remove($prop)
}
# Write-Host $Object -ForegroundColor Green
if ( $Recurse.IsPresent ) {
foreach ($ObjValue in $Object.PSObject.Properties.Value) {
# Write-Host $ObjValue -ForegroundColor Yellow
if ( $ObjValue.GetType().Name -eq 'PSCustomObject' ) {
$ObjValue | RemoveProperty -PropList $PropList -Recurse
}
}
}
}
# sample usage:
$Obj = ConvertFrom-Json $Response
RemoveProperty -Object $Obj -PropList 'booster','cards' -Recurse
$Obj | ConvertTo-Json | Out-File ./All-Sets-Data.json -Force
(Please note that the RemoveProperty function contains some Write-Host in commented lines; originally used used for debugging purposes).

Unable to copy json block

I'm trying to copy condition block from id 106 to id 107 with script below; however, I got error "ConvertFrom-Json : Invalid JSON primitive:". Am I missing something? Please advise.
$a = Get-Content 'C:\DevOps\mytest.json' -raw | ConvertFrom-Json
$condition = $a | select -expand environments | select -expand conditions
Write-Host $condition[0]
$a | add-member -Name $condition[1].name -value (Convertfrom-Json $condition[0]) -MemberType NoteProperty
Write-Host $update
$a | ConvertTo-Json -Depth 20 | set content 'C:\DevOps\updatedmytest.json'
The JSON file contains:
{
"source": "userInterface",
"id": 25,
"revision": 3,
"environments": [
{
"id": 106,
"conditions": [
{
"name": "ReleaseStarted",
"conditionType": "event",
"value": ""
}
]
},
{
"id": 107,
"conditions": [
]
}
]
}
Best Regards,
Andy Pham
The error message you are receiving is because you convert the JSON into a PowerShell Object and then saved it into the variable $a. $condition then is still a PowerShell object that you have expanded and is not JSON. So -value (Convertfrom-Json $condition[0]) doesn't work because it's already converted.
Convert to an object, make changes directly to the object, convert from the object.
$ConvertedJSON = Get-Content 'C:\DevOps\mytest.json' -Raw | ConvertFrom-Json
$ConvertedJSON.environments[1].conditions = $ConvertedJSON.environments[0].conditions
$ConvertedJSON | ConvertTo-Json -Depth 20 | Set-Content 'C:\DevOps\updatedmytest.json'
Alternatively, you could use Where-Object to filter for the ids.
$ConvertedJSON = Get-Content 'C:\DevOps\mytest.json' -Raw | ConvertFrom-Json
($ConvertedJSON.environments | Where-Object {$_.id -eq 107}).conditions = ($ConvertedJSON.environments | Where-Object {$_.id -eq 106}).conditions
$ConvertedJSON | ConvertTo-Json -Depth 20 | Set-Content 'C:\DevOps\updatedmytest.json'