1

I have developed a powershell script which generates a JSON file which am trying to parse using JQuery.

var mydata = jQuery.parseJSON(w3wp);

 w3wp = '[{"Pid" : "6724" , "Apppool" : "myapppool1" , "Usage" : "229.328125"},{"Pid" : "6808" , "Apppool" : "myapppool2" , "Usage" : "152" }]';

There are no issues found when the JSON data is in a single line like above, the script works fine. If the json data is formatted like below i am getting "undefined error in w3wp."

 w3wp = '[{"Pid" : "6724" , "Apppool" : "myapppool1" , "Usage" : "229.328125"},
 {"Pid" : "6808" , "Apppool" : "myapppool2" , "Usage" : "152" }]';

The representation above is with very minimal data in real time scenario the number of app pools are huge and power-shell word wraps in 2.0 and am having issues with the JSON file. Is there any way to format this Json file so i can use this for parsing ?

Based on request here is the powershell script

$host.UI.RawUI.BufferSize = new-object System.Management.Automation.Host.Size 5512,100
function get-apppool {
    [regex]$pattern="-ap ""(.+)"""
    $contentx="w3wp = '["
    gwmi win32_process -filter 'name="w3wp.exe"' | % {
        $name=$_.name
        $cmd = $pattern.Match($_.commandline).Groups[1].Value
        $procid = $_.ProcessId
        $cpuper = get-process | where-object {$_.id -like $procid} | select -Expand CPU
        $contentx = $contentx + "{""Pid"" : ""$procid"" , ""Apppool"" : ""$cmd"" , ""Usage"" : ""$cpuper"" }"
        $contentx = $contentx + ","
    }
    $contentx = $contentx -replace ".$"
    $contentx = $contentx + "]';"
    write-host -nonewline $contentx > dsdf.json 
} 
get-apppool
18
  • it it the carriage return that is the problem? Commented Jul 7, 2014 at 21:55
  • I am not sure, i am looking for a way to split these into multiple lines Commented Jul 7, 2014 at 21:57
  • Well,. am unable to print a single line in powershell, despite using System.Management.Automation.Host.Size the value still warps and the output file has multiple lines which am unable to parse,. if there is a way to split these then i would implement this logic in powershell to format the JSON file. Commented Jul 7, 2014 at 22:03
  • 1
    your code there makes no sense ... nested script tags? and referencing a JSON file via script tag titled "text/javascript" is bad form. it should be <script type="application/json" src="dfdsf.json">. Commented Jul 7, 2014 at 22:38
  • 1
    It's quittin' time for me, so I'm going home, but I'll get back to you about that PowerShell script part if nobody else has in the morning. Commented Jul 7, 2014 at 23:18

3 Answers 3

3

You don't have to construct the Json yourself, let PowerShell do that for you - at least in V3 and higher which has the handy ConvertTo-Json command e.g.:

Get-WmiObject Win32_Process -Filter 'name="w3wp.exe"' | 
    Where {$_.commandline -match '-ap "(.*)"'} | 
    Select @{n='Pid';e={$_.ProcessId}},
           @{n='AppPool';e={$_.matches[1]}},
           @{n='Usage';e={[float](Get-Process -id $_.ProcessId).CPU}} | 
    ConvertTo-Json > dsdf.json
Sign up to request clarification or add additional context in comments.

Comments

1

Supposing you're not using V3 (you said you're using 2.0), I can suggest the following (I'm not able to confirm this because you haven't posted the script's output):

Backticks:

If you want to add Single and Double Quotes in a string I suggest you always use backticks, for example:

$contentx="w3wp = `'["

Bad formatting in JSON:

You're adding a comma (,) after every array object, if you don't remove this one it will generate an undefined error in w3wp, you can correct this by removing it's last comma before finishing the JSON string like this:

$contentx = $contentx.Substring(0,$contentx.Lenght-1);
$contentx = $contentx + "]`';"

Write-host isn't doing what you expect:

Well and as described in CB's answer:

Write-host redirect the output only to the console.

You can use write-output $contentx > dsdf.json instead.

Other possible errors:

You can have also problems in the way you load the data from file, but I don't have enough information to evaluate that right now.

Comments

0

JSON.Stringfy fixed the problem for me, now i am able to parse the string properly from the JSON file. The issue was due to the single quote at the begining and the end which caused issues with JSON parse. I ran JSON.Stringfy and then pass that to JSON.Parse which fixed the issue. Thanks for your comments and suggestions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.