Removing all data from a horribly messy output before and after a string – Azure CLI Command Output

Today I came across the Azure CLI which seems like it’s going to make my life a lot easier for some things I need to do. Only thing is, it seems like the output from Azure was at one point JSON and has come into powershell as an array of character objects. See below:

PS C:> $output = az vm run-command invoke -g [resource group] -n [vm name] --command-id RunPowerShellScript --scripts "Get-Service AppInfo" PS C:>$output.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array PS C:> $output { "additionalProperties": {}, "endTime": "2018-02-01T21:30:30.588032+00:00", "error": null, "name": "[removed in case it has personal info]", "output": [ { "code": "ComponentStatus/StdOut/succeeded", "displayStatus": "Provisioning succeeded", "level": "Info", "message": "Status Name DisplayName \n------ ---- ----------- \nRunning AppIn fo Application Information \n\n" }, { "code": "ComponentStatus/StdErr/succeeded", "displayStatus": "Provisioning succeeded", "level": "Info", "message": "" } ], "startTime": "2018-02-01T21:30:01.291739+00:00", "status": "Succeeded" } 

Meanwhile just running the Get-Service command in powershell gives me this nice result:

PS C:> Get-Service AppInfo Status Name DisplayName ------ ---- ----------- Running AppInfo Application Information 

The data I care about is in what at one point was in “Message” in JSON. Notice how the status, name, and DisplayName has sort of been reduced into one string.

First off – Anyone familiar with using Azure CLI that knows a better way to do this? Seems pretty crappy, but compared to the other methods I found to actually run scripts on a remote Azure server through powershell it’s a godsend. If I could get a cleaner input it would be real nice to have the data easily available.

Beyond that though – how would you deal with this input? I was considering potentially using Split, but when I was trying because there’s so many lines that end in brackets it was really getting messy. In this case since I just care about whether or not the service shows as “Running” I can just use a -match, but my concern is as I start running other commands I may end up with results that I need to at least parse out the “message” line to handle.

Dealing with it the way it currently is is far from the cleanest, but at least it’s something so hopefully people have some thoughts on how they’d handle it.



submitted by /u/rs-ps
[link] [comments]

Leave a Reply