Author Posts

October 25, 2017 at 1:12 pm

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?

October 25, 2017 at 1:48 pm

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.

October 25, 2017 at 1:48 pm

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

October 25, 2017 at 2:36 pm

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.

October 25, 2017 at 3:00 pm

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

October 25, 2017 at 3:15 pm

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;
        }

October 25, 2017 at 3:30 pm

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.

October 25, 2017 at 3:35 pm

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