admin管理员组文章数量:1244331
Good evening. Stack Overflow and Powershell and ObjectGraph novice here.
I am looking to enhance a program and append the output of a piece of PowerShell code into a string, then concatenate that string into $Item1_txt.Text = (
- Can you save the output of this PowerShell code into a string (Let's call it $apple ) based on this JSON and PowerShell code? I believe you need |Out-String and .Trim() somewhere in the pipeline but I am struggling
JSON Code:
{
"id": "4fd338f7-746b-420b-9d7e-b41709e9a641",
"RD": {
"PDR": []
},
"TimeStamp": "2025-02-14T16:55:20.1433467Z",
"NNTl": {
"CD": [
{
"EntityType": "Occurrence",
"Characteristics": [
"OFGE"
],
"Cardinality": "Many"
},
{
"EntityType": "Operator",
"Characteristics": [
"ADL",
"EsRO"],
"Cardinality": "Many"
}
],
"RRXC": true
},
"EntitiesData": {
"Entities": [
{
"Id": "b0000000-0000-0000-0000-00000000000b",
"EntityType": "IHUR",
"Ordinal": 0,
"Characteristics": {
"PBIL": {
"StringValue": "DDDD"
}
}
},
{
"Id": "833e2a7f-c173-4062-9cd6-2196432e0001",
"EntityType": "Operator",
"Ordinal": 0,
"Characteristics": {
"FN": {
"StringValue": "Da"
},
"RatedOI": {
"BoolValue": true
},
"fdfdf": {
"DDFDF": "SDFDS43R5"
}
},
"EntityCreationDateTime": "2025-02-12T19:42:59.2852792Z"
},
{
"Id": "6fe06340-072f-49b2-8d5a-449e3db80002",
"EntityType": "Operator",
"Characteristics": {
"Deleted": {
"StringValue": "0"
},
"RatedOI": {
"BoolValue": false
}
}
}
]
},
"OR": "FFFFF01"
}
PowerShell Code:
Set-Location C:\Users\A187515\Downloads\
$file = Get-ChildItem -Path C:\Users\A187515\Downloads\ -Filter *.json | Sort-Object LastAccessTime -Descending | Select-Object -First 1
(Get-Content -Raw $file | ConvertFrom-Json) | ForEach-Object {
foreach ($entity in $_.EntitiesData.Entities) {
if ($entity.EntityType -eq 'Operator' -and $entity.Characteristics -issnot [array] -and $entity.Characteristics.RatedOI) {
$entity | Select-Object `
id,
@{ Name = 'RatedOI'; Expression = {
$_.characteristics.RatedOI.boolValue }
}
}
}
}
This is linked to my question here:
PowerShell code returning correct value. However, looking to enhance program by capturing the ID tag listed above the desired value
Good evening. Stack Overflow and Powershell and ObjectGraph novice here.
I am looking to enhance a program and append the output of a piece of PowerShell code into a string, then concatenate that string into $Item1_txt.Text = (
- Can you save the output of this PowerShell code into a string (Let's call it $apple ) based on this JSON and PowerShell code? I believe you need |Out-String and .Trim() somewhere in the pipeline but I am struggling
JSON Code:
{
"id": "4fd338f7-746b-420b-9d7e-b41709e9a641",
"RD": {
"PDR": []
},
"TimeStamp": "2025-02-14T16:55:20.1433467Z",
"NNTl": {
"CD": [
{
"EntityType": "Occurrence",
"Characteristics": [
"OFGE"
],
"Cardinality": "Many"
},
{
"EntityType": "Operator",
"Characteristics": [
"ADL",
"EsRO"],
"Cardinality": "Many"
}
],
"RRXC": true
},
"EntitiesData": {
"Entities": [
{
"Id": "b0000000-0000-0000-0000-00000000000b",
"EntityType": "IHUR",
"Ordinal": 0,
"Characteristics": {
"PBIL": {
"StringValue": "DDDD"
}
}
},
{
"Id": "833e2a7f-c173-4062-9cd6-2196432e0001",
"EntityType": "Operator",
"Ordinal": 0,
"Characteristics": {
"FN": {
"StringValue": "Da"
},
"RatedOI": {
"BoolValue": true
},
"fdfdf": {
"DDFDF": "SDFDS43R5"
}
},
"EntityCreationDateTime": "2025-02-12T19:42:59.2852792Z"
},
{
"Id": "6fe06340-072f-49b2-8d5a-449e3db80002",
"EntityType": "Operator",
"Characteristics": {
"Deleted": {
"StringValue": "0"
},
"RatedOI": {
"BoolValue": false
}
}
}
]
},
"OR": "FFFFF01"
}
PowerShell Code:
Set-Location C:\Users\A187515\Downloads\
$file = Get-ChildItem -Path C:\Users\A187515\Downloads\ -Filter *.json | Sort-Object LastAccessTime -Descending | Select-Object -First 1
(Get-Content -Raw $file | ConvertFrom-Json) | ForEach-Object {
foreach ($entity in $_.EntitiesData.Entities) {
if ($entity.EntityType -eq 'Operator' -and $entity.Characteristics -issnot [array] -and $entity.Characteristics.RatedOI) {
$entity | Select-Object `
id,
@{ Name = 'RatedOI'; Expression = {
$_.characteristics.RatedOI.boolValue }
}
}
}
}
This is linked to my question here:
PowerShell code returning correct value. However, looking to enhance program by capturing the ID tag listed above the desired value
1 Answer
Reset to default 2Append
| Out-String | ForEach-Object Trim
to your existing pipeline in order to capture the for-display formatting of your pipeline's output as a string, with leading and trailing empty lines (whitespace) removed.Assign the entire pipeline to your
$apple
variable.
# ...
# To *add to* a preexisting string stored in $apple, use += in lieu of =
$apple =
(Get-Content -Raw $file | ConvertFrom-Json) | ForEach-Object {
foreach ($entity in $_.EntitiesData.Entities) {
if ($entity.EntityType -eq 'Operator' -and $entity.Characteristics -isnot [array] -and $entity.Characteristics.RatedOI) {
$entity | Select-Object `
id,
@{ Name = 'RatedOI'; Expression = {
$_.characteristics.RatedOI.boolValue }
}
}
}
} | Out-String | ForEach-Object Trim
本文标签:
版权声明:本文标题:json - PowerShell code returning correct variables. Enhance program by converting code output to string, concatenate to existing 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740139431a2230607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论