ConvertTo-Json returns Count PSObject as well

This topic contains 7 replies, has 3 voices, and was last updated by  Ken B 3 weeks, 5 days ago.

  • Author
    Posts
  • #82844

    Ken B
    Participant

    ConvertTo-Json returns two PSObjects. One is a count of the number of records and two is the actual json string. I am having difficulty getting the JSON portion from my C# application with the SharePoint environment and was wondering if there is a way to return just the json string and not the count psobject from within my powershell script?

  • #82849

    Don Jones
    Keymaster

    There's no way to modify what the command does, but you can certainly just keep the object you want. $results[1] would be the second object from $results, for example.

  • #82850

    Jeremy Murrah
    Participant

    not sure if I'm seeing what you're seeing, but if I use convertto-json on a psobject I get a single json output. One of those properties happens to be PropertyCount, but it doesn't look to be a separate object at all. You could use the excludeproperty parameter of select-object to filter that out before converting to a json

    $object | select -excludeproperty PropertyCount | convertto-json
    
  • #82873

    Ken B
    Participant

    Don, Thanks but I tried that knowing every other manner I invoke() the PS script it does return two PSObjects with the exception of the SharePoint environment. I even tried to slow things down and use an asynch approach. Hence the reason I'm trying to handle it on the PS side.

    Jeremy, Nice try! I thought you had it but no luck. Thanks.

  • #82874

    Jeremy Murrah
    Participant

    can you share any code? otherwise we're really just guessing at what you're doing. All PSobjects are not created equal.

  • #82879

    Ken B
    Participant

    Here is my code. Works perfectly within a console or web app. Just not in SharePoint. As you can see in the commented code I've tried multiple methods. I did not try PSCustomObject. I exclude the first psobject[0] based on it's length which I know to be two characters. If I just ask for the second it would throw an exception as it does not exist.

    using (PowerShell shell = PowerShell.Create()) { 
    			
    			        // Add the script to the PowerShell object
    			        shell.Commands.AddScript(LoadFile());
    			
    			        // Execute the script
    		        	//Collection results = shell.Invoke();
                        //PSObject result = (PSObject)results[1];
                        PSDataCollection outputCollection = new PSDataCollection();
    
                        // Execute the script
                        //System.Collections.ObjectModel.Collection results = shell.Invoke();
    
                        // begin invoke execution on the pipeline
                        // use this overload to specify an output stream buffer
                        IAsyncResult result = shell.BeginInvoke(null, outputCollection);
    
                        // do something else until execution has completed.
                        // this could be sleep/wait, or perhaps some other work
                        while (result.IsCompleted == false)
                        {
                            System.Threading.Thread.Sleep(250);
                        }
    
    
                        foreach (PSObject psObject in outputCollection)
    					{
    						
                            if(psObject.BaseObject.ToString().Length > 10) output = psObject.BaseObject.ToString();
    					}
                        return output;
            }
  • #82880

    Don Jones
    Keymaster

    Ah. I did not pick up on the fact that this is C# and you're "hosting" the shell engine. OK. I can only presume that the script you're invoking it ending with ConvertTo-JSON, but that's a presumption you an correct me on if I'm wrong.

    You're just dealing with an odd kind of environment. Unfortunately, I don't have a similar environment in which I can fuss around, but I'd probably end up taking a similar approach. Most of us here are used to using "just" PowerShell, which is pretty different in a lot of ways from hosting the engine as you're doing, let alone doing so from within SharePoint.

  • #82882

    Ken B
    Participant

    Spot On. That's why I posted here to resolve from the PS side. Seems like I need to take my efforts to the SP people.

    $a = $accounts | ConvertTo-Json -Compress 
       $s = [String] $a.Replace('"',"'")
       return $s

You must be logged in to reply to this topic.