Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell , JSON and UTF8 with cyrrylic problems

I have some web site with JSON on it. JSON is in utf8 codepage ( as in RFC)

Web server answer:Content-Type:application/json; charset=utf-8

I need to convert it and send to scom agent. All is ok except that powershell destroys any Cyrillic symbol to '?'

$api = New-Object -comObject 'MOM.ScriptAPI'
$discoveryData = $api.CreateDiscoveryData(0, $sourceId, $managedEntityId)


$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials 

$rowdata = Invoke-WebRequest 'https://monitoring.net/monitoring.json' -UseBasicParsing

$jsondata = ConvertFrom-Json $rowdata


foreach ($urls in $jsondata.monitors) 
{
    $instance = $discoveryData.CreateClassInstance("$MPElement[Name='58MCLibrary!F.058MC.Json.Url.Class']$")

    $instance.AddProperty("$MPElement[Name='058MCLibrary!058MC.Json.Url.Class']/Name$", $urls.name)               
    $instance.AddProperty("$MPElement[Name='58MCLibrary!058MC.Json.Url.Class']/Resource$", $urls.resource) 
    $instance.AddProperty("$MPElement[Name='058MCLibrary!058MC.Json.Url.Class']/Description$", $urls.description) 


    $discoveryData.AddInstance($instance)


}

$discoveryData

Story is that $urls.name and $urls.resource are ok - no cyrrylic in it

But $urls.description looks like Description ??????????????, ?????????????????? ?????????? ?????????????? ??????????. ????????????????????

Any way to fix it ? I tryed to set default env codepage with .NET to utf8 - no changes...

Its very strange to see codepage problems in PS...

like image 208
Igor Kuznetsov Avatar asked Apr 24 '26 14:04

Igor Kuznetsov


1 Answers

$rowdata = Invoke-WebRequest '123/monitoring.json' -UseBasicParsing

$utf8_ready_data = [system.Text.Encoding]::UTF8.GetString($rowdata.RawContentStream.ToArray());

Looks like Invoke-WebRequest hates utf8 in 2016 year..

like image 99
Igor Kuznetsov Avatar answered Apr 27 '26 07:04

Igor Kuznetsov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!