Change json values - powershell

Is it possible to change the value in a json file.
Little bit of background ground I'm able to pull ticket data from Freshdesk using their api. The status of the ticket comes down as a number. So for example a "open" ticket would have a value of 2. I want it to actually say open instead of two. Any thoughts?

If you are saving it as a file you could just do a find and replace
(Get-Content c:\temp\file.json).replace('status: 1', 'status: \'Open\'') | Set-Content c:\temp\file.json
and repeat for the different possible values of status
Or
$a = Get-Content 'D:\temp\file.json' -raw | ConvertFrom-Json
$a.update | % {if($_.status -eq 1){$_.status='Open'}}
$a | ConvertTo-Json | set-content 'D:\temp\file.json'
and repeat line 2 for each value of status

You could maintain a map and check it against it every time.
For example
const getType = t => ({
2: "Open",
3: "Pending",
4: "Resolved",
5: "Closed"
})[t]
;

Related

Powershell Custom object

This data is store in a file
AUTOM01-AYEHU1:No Updates Available
AUTOM01-AYEHU2:No Updates Available
AUTOM01-AYEHU3:No Updates Available
AUTOM01-AYEHU4:No Updates Available
AUTOM01-AYEHU5:No Updates Available
AUTOM01-AYEHU6:No Updates Available
I have a above dataset in a file i need to create 2 powershell custom object with the name of (SERVERNAME,STATUS) and put respective data into it .
before : is servername and rest is status
This is the answer if you really want 2 objects
$fileLines = #("AUTOM01-AYEHU1:No Updates Available","AUTOM01-AYEHU2:No Updates Available")
$serverArray = [System.Collections.ArrayList]::new()
$statusArray = [System.Collections.ArrayList]::new()
foreach($fileLine in $fileLines)
{
$splittedLine = $fileLine.split(":")
$serverArray.Add([PSCustomObject]#{ServerName=$splittedLine[0]})
$statusArray.add([PsCustomobject]#{Status=$splittedLine[1]})
}
find them in $serverArray and $statusArray
As commented in your previous question, you can simply do
$result = (Get-Content -Path 'thefile.txt' -Raw) -replace ':', '=' | ConvertFrom-StringData
ConvertFrom-StringData returns a Hashtable, in which by default the items are unordered.
As you would like to keep the order as in the input file, you may want to use this instead:
$result = Get-Content -Path 'D:\Test\thefile.txt' | ConvertFrom-String -Delimiter ':' -PropertyNames Server, Status
To return an array of PSCustomObjects:
Server Status
------ ------
AUTOM01-AYEHU1 No Updates Available
AUTOM01-AYEHU2 No Updates Available
AUTOM01-AYEHU3 No Updates Available
AUTOM01-AYEHU4 No Updates Available
AUTOM01-AYEHU5 No Updates Available
AUTOM01-AYEHU6 No Updates Available

Get-sentiment Powershell

How would I go about getting the score decimal returns into just returning the scores as a Percentage?Current powershell line
Get-Sentiment -Text "Im angry", "Hey good morning!" | ForEach-Object documents
Returns
id score
-- -----
1 0.00449720025062561
2 0.98748815059661865
Any Help is appreciated.
You should look at the string tokenization and filtering:
"{0:P2}" -f $SomeVariableContainingANumberToConvertToPercentage
Where '0' is the index for the first token, 'P' is telling PowerShell to format it as a 'percentage' and '2' is telling it to limit the conversion to two places (e.g. 95.22%)
Edit:
Based on your edit with the Return data, you'd want to format the returned object's 'Score' property.
$data = "id,score
1,0.00449720025062561
2,0.98748815059661865
" | ConvertFrom-Csv
$data | Select-Object id,#{n='ScoreFormatted'; e={ ("{0:P2}" -f [float]$_.Score) }}
Outputs:
id ScoreFormatted
-- --------------
1 0.45%
2 98.75%

Parse string in Powershell

Learning powershell, trying to find out how to parse the first value from this resultset (10.60.50.40):
IPAddresses
-----------
{10.60.50.40, fe80::5ddf:a8f4:e29c:b66}
Normally I would just look it up, however, I don't know if {x, x} is a standard datatype of sorts in Powershell land.
Do I have to do rough string parsing, or is there some standard command to extract the first one, such as:
... | Select-Object IPAddresses | Select-String [0]
(I just made the select string part up. I'm lost.)
This is most likely the result of of the IPAddresses property of your object containing an array. The output you're seeing is stylized for display purposes, so it's not a string you would have to parse. Assuming your object is $obj, you should be able to do either of these:
$obj.IPAddresses[0]
$obj.IPAddresses | Select-Object -First 1
One solution is to use split function to convert the string into array and work with that like in the next steps:
Split the string into an array using the split function (comma is the item delimiter).
Grab the first item of the array (or whatever needed) and then also sanitize it (remove unnecessary curly bracket).
Example below:
$str = "{10.60.50.40, fe80::5ddf:a8f4:e29c:b66}"
$strArr = $str.Split(",")
Write-Host $strArr[0].Replace("{", "")
This is what I ended up doing:
$str = ... | Select-Object IPAddresses | ForEach {$_.IpAddresses}
Write-Host $str[0]
Depending on the source of your IPAddresses, this might not be optimal. You might get multiple IPAddresses per devices.
You might want to combine both approaches:
$str = ... | Select-Object -ExpandProperty IPAddresses | Select-Object -First 1
This will return the First IP address in your list per device.

Powershell counting same values from csv

Using PowerShell, I can import the CSV file and count how many objects are equal to "a". For example,
#(Import-csv location | where-Object{$_.id -eq "a"}).Count
Is there a way to go through every column and row looking for the same String "a" and adding onto count? Or do I have to do the same command over and over for every column, just with a different keyword?
So I made a dummy file that contains 5 columns of people names. Now to show you how the process will work I will show you how often the text "Ann" appears in any field.
$file = "C:\temp\MOCK_DATA (3).csv"
gc $file | %{$_ -split ","} | Group-Object | Where-Object{$_.Name -like "Ann*"}
Don't focus on the code but the output below.
Count Name Group
----- ---- -----
5 Ann {Ann, Ann, Ann, Ann...}
9 Anne {Anne, Anne, Anne, Anne...}
12 Annie {Annie, Annie, Annie, Annie...}
19 Anna {Anna, Anna, Anna, Anna...}
"Ann" appears 5 times on it's own. However it is a part of other names as well. Lets use a simple regex to find all the values that are only "Ann".
(select-string -Path 'C:\temp\MOCK_DATA (3).csv' -Pattern "\bAnn\b" -AllMatches | Select-Object -ExpandProperty Matches).Count
That will return 5 since \b is for a word boundary. In essence it is only looking at what is between commas or beginning or end of each line. This omits results like "Anna" and "Annie" that you might have. Select-Object -ExpandProperty Matches is important to have if you have more than one match on a single line.
Small Caveat
It should not matter but in trying to keep the code simple it is possible that your header could match with the value you are looking for. Not likely which is why I don't account for it. If that is a possibility then we could use Get-Content instead with a Select -Skip 1.
Try cycling through properties like this:
(Import-Csv location | %{$record = $_; $record | Get-Member -MemberType Properties |
?{$record.$($_.Name) -eq 'a';}}).Count

PowerShell: Format-Table without headers

In a PowerShell script, I have some objects that I pass to the Format-Table CmdLet.
The output of my script looks like this:
Something...
Operation AttributeName AttributeValue
--------- ------------- --------------
Delete Member John Doe
Something else...
Since the meaning of the fields is pretty self-explanatory, I would like to remove the headers, the '---' separators and the blank lines at the beginning and at the end from the output of Format-Table.
I don't think that the CmdLet supports this (or at least if there's a parameter to do this I couldn't find it).
What would the best way to leave only the lines with the actual values from the output of Format-Table?
Try the -HideTableHeaders parameter to Format-Table:
gci | ft -HideTableHeaders
(I'm using PowerShell v2. I don't know if this was in v1.)
Try -ExpandProperty. For example, I use this for sending the clean variable to Out-Gridview -PassThru , otherwise the variable has the header info stored. Note that these aren't great if you want to return more than one property.
An example:
Get-ADUser -filter * | select name -expandproperty name
Alternatively, you could do this:
(Get-ADUser -filter * ).name
The -HideTableHeaders parameter unfortunately still causes the empty lines to be printed (and table headers appearently are still considered for column width). The only way I know that could reliably work here would be to format the output yourself:
| % { '{0,10} {1,20} {2,20}' -f $_.Operation,$_.AttributeName,$_.AttributeValue }
Here is how I solve this. I just pipe the output to Out-String and then pass that output to the .NET Trim function:
(gci | ft -HideTableHeaders | Out-String).Trim()
This will strip out the line breaks before and after the table.
You can also use TrimStart to just take care of the header's line break if you still want the trailing line breaks.
(gci | ft -HideTableHeaders | Out-String).TrimStart()
Another approach is to use ForEach-Object to project individual items to a string and then use the Out-String CmdLet to project the final results to a string or string array:
gci Microsoft.PowerShell.Core\Registry::HKEY_CLASSES_ROOT\CID | foreach { "CID Key {0}" -f $_.Name } | Out-String
#Result: One multi-line string equal to:
#"
CID Key HKEY_CLASSES_ROOT\CID\2a621c8a-7d4b-4d7b-ad60-a957fd70b0d0
CID Key HKEY_CLASSES_ROOT\CID\2ec6f5b2-8cdc-461e-9157-ffa84c11ba7d
CID Key HKEY_CLASSES_ROOT\CID\5da2ceaf-bc35-46e0-aabd-bd826023359b
CID Key HKEY_CLASSES_ROOT\CID\d13ad82e-d4fb-495f-9b78-01d2946e6426
"#
gci Microsoft.PowerShell.Core\Registry::HKEY_CLASSES_ROOT\CID | foreach { "CID Key {0}" -f $_.Name } | Out-String -Stream
#Result: An array of single line strings equal to:
#(
"CID Key HKEY_CLASSES_ROOT\CID\2a621c8a-7d4b-4d7b-ad60-a957fd70b0d0",
"CID Key HKEY_CLASSES_ROOT\CID\2ec6f5b2-8cdc-461e-9157-ffa84c11ba7d",
"CID Key HKEY_CLASSES_ROOT\CID\5da2ceaf-bc35-46e0-aabd-bd826023359b",
"CID Key HKEY_CLASSES_ROOT\CID\d13ad82e-d4fb-495f-9b78-01d2946e6426")
The benefit of this approach is that you can store the result to a variable and it will NOT have any empty lines.
I know it's 2 years late, but these answers helped me to formulate a filter function to output objects and trim the resulting strings. Since I have to format everything into a string in my final solution I went about things a little differently.
Long-hand, my problem is very similar, and looks a bit like this
$verbosepreference="Continue"
write-verbose (ls | ft | out-string) # this generated too many blank lines
Here is my example:
ls | Out-Verbose # out-verbose formats the (pipelined) object(s) and then trims blanks
My Out-Verbose function looks like this:
filter Out-Verbose{
Param([parameter(valuefrompipeline=$true)][PSObject[]]$InputObject,
[scriptblock]$script={write-verbose "$_"})
Begin {
$val=#()
}
Process {
$val += $inputobject
}
End {
$val | ft -autosize -wrap|out-string |%{$_.split("`r`n")} |?{$_.length} |%{$script.Invoke()}
}
}
Note1: This solution will not scale to like millions of objects(it does not handle the pipeline serially)
Note2: You can still add a -noheaddings option.
If you are wondering why I used a scriptblock here, that's to allow overloading like to send to disk-file or other output streams.