none
Invoke-RestMethod auswerten RRS feed

  • Frage

  • Hallo Zusammen,

    ich mache folgendes:


    $jsonLog = @"
    [{"cmd":"Login","action":0,"param":{"User":{"userName":"xxxx","password":"xxxx"}}}]
    "@
    
    
    $params = @{
        Uri         = 'http://192.xxx.xxx.xx/cgi-bin/api.cgi?cmd=Login&token=null'
        Method      = 'POST'
        Body        = $jsonLog
        ContentType = 'application/json'
        }
    
    Invoke-RestMethod @params
    
    Invoke-WebRequest @params

    Ausgabe 

    Invoke-RestMethod @params:

    cmd   code value    
    ---   ---- -----    
    Login    0 @{Token=}

    --------------------------

    Invoke-WebRequest @params

    StatusCode        : 200
    StatusDescription : OK
    Content           : [
                           {
                              "cmd" : "Login",
                              "code" : 0,
                              "value" : {
                                 "Token" : {
                                    "leaseTime" : 3600,
                                    "name" : "54de260b4820bfb"
                                 }
                              }
                           }
                        ]
                        
    RawContent        : HTTP/1.1 200 OK
                        Transfer-Encoding: chunked
                        Connection: keep-alive
                        X-Frame-Options: SAMEORIGIN
                        X-XSS-Protection: 1; mode=block
                        X-Content-Type-Options: nosniff
                        Content-Type: text/html
                        Date: Sun, ...
    Forms             : {}
    Headers           : {[Transfer-Encoding, chunked], [Connection, keep-alive], [X-Frame-Options, SAMEORIGIN], [X-XSS-Protection, 1; 
                        mode=block]...}
    Images            : {}
    InputFields       : {}
    Links             : {}
    ParsedHtml        : System.__ComObject
    RawContentLength  : 184

    Ich möchte gerne an "name" : "54de260b4820bfb" rankommen. Da der Content json ist musste doch 

    Invoke-RestMethod @params gehen. aber wie?

    Danke

    Sonntag, 21. März 2021 18:35

Antworten

Alle Antworten

  • Scheinbar stecken die Daten, an denen Du interessiert bist in der Property "Content". Ich würde also damit beginnen, die Ausgabe auf diese Property zu beschränken. Wenn es dann auch noch valides JSON ist, könntest Du mit dem cmdlet ConvertFrom-JSON weitermachen.

    Also ungefähr so:

    Invoke-RestMethod @params | 
        Select-Object -ExpandProperty Content |
            ConvertFrom-Json

    Übrigens - Beispiel-Daten oder Konsolen-Output solltest Du hier im Forum auch als Code formatieren, bitte.

    Danke schon mal im Voraus.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Sonntag, 21. März 2021 18:57
  • Danke für die Hilfe. Folgendes hat jetzt funktioniert:

    $WebResponse = Invoke-RestMethod @params
    $WebResponse.value.Token.name

    Sonntag, 21. März 2021 22:45