none
Eliminating RawView from the powershell script result. RRS feed

  • Question

  • I have a function that invokes the powershell script and returns the output. The problem is the output is returning multiple records and an additional raw view of blank record. How to eliminate it from the returned output.

    Here's my function: 

    public List<string> PowerShellExecutorGrd(string scriptPath, string arg)
            {
                string outString = "";
                var shell = PowerShell.Create();
                shell.Commands.AddCommand(scriptPath).AddArgument(arg);
                var results = shell.Invoke();
                if (results.Count > 0)
                {
                    var builder = new StringBuilder();
                    foreach (var psObj in results)
                    {
                        builder.Append(psObj.ToString().Trim('@').Trim('{').Trim('}') + "\n\r");
                    }
                    outString = Server.HtmlEncode(builder.ToString());
                }
                List<string> strLst = outString.Split(new char[] { '\n' }).ToList();
                shell.Dispose();
                return strLst;
            }

    The problem is when I am loading the records in the container table it is creating additional row for raw view as a blank record.

    Is there a way to eliminate it in the function or through the script?

    • Moved by jrv Wednesday, June 12, 2019 4:56 PM correct forum
    Wednesday, June 12, 2019 4:34 PM

Answers

  • You're calling Append on the builder and appending a CRLF on the end of each line. Later when you break the string on LF it is getting the last LF which causes an extra blank line. Also the Server.HtmlEncode seems odd just before you split it.

    I would recommend against using StringBuilder and Split here because all you really seem to care about is the output of each line. So don't bother combining them together to begin with.

    public List<string> PowerShellExecutorGrd ( string scriptPath, string arg )
    {
        var items = new List<string>();
    
        using (var shell = PowerShell.Create())
        {
            shell.Commands.AddCommand(scriptPath).AddArgument(arg);
            var results = shell.Invoke();
            if (results.Any())
            {
                foreach (var psObj in results)
                    items.Add(Server.HtmlEncode(psObj.ToString().Trim('@', '{', '}')));
            };
        };
    
        return items;
    }



    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Nikul Vyas Wednesday, June 12, 2019 7:25 PM
    Wednesday, June 12, 2019 7:12 PM
    Moderator

All replies

  • That Raw View you're seeing is the debugger, not your actual output. The debugger will always provide a raw view of structured data. This has nothing to do with your object. It is there primarily for native code (in my experience) where you can see the underlying raw data outside the confines of what the type might show. The Count property should be returning 6 items in your case. If you enumerated the items via foreach you would get a result for each one.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 12, 2019 5:44 PM
    Moderator
  • Michael, so what you are suggesting is that I should use for instead of for each?
    Wednesday, June 12, 2019 5:56 PM
  • No the foreach is correct. You asked why the Raw View is showing up in the debugger. You had a screenshot of the debugger view with Raw View showing up. That is by design. Your actual code will never see it. It is just the debugger visualizer showing you additional information.

    How many items does your `results.Count` property show? It should be 6. Step through your code and you should only see the foreach called 6 times. If it calls it 7 times then post the contents of `psObj`.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 12, 2019 6:13 PM
    Moderator
  • Yes foreach is only running 6 times, I checked by keeping a breakpoint the extra count is happening when after the outString variable is getting converted to List.

    • Edited by Nikul Vyas Wednesday, June 12, 2019 6:32 PM Wrong Image upload
    Wednesday, June 12, 2019 6:30 PM
  • You're calling Append on the builder and appending a CRLF on the end of each line. Later when you break the string on LF it is getting the last LF which causes an extra blank line. Also the Server.HtmlEncode seems odd just before you split it.

    I would recommend against using StringBuilder and Split here because all you really seem to care about is the output of each line. So don't bother combining them together to begin with.

    public List<string> PowerShellExecutorGrd ( string scriptPath, string arg )
    {
        var items = new List<string>();
    
        using (var shell = PowerShell.Create())
        {
            shell.Commands.AddCommand(scriptPath).AddArgument(arg);
            var results = shell.Invoke();
            if (results.Any())
            {
                foreach (var psObj in results)
                    items.Add(Server.HtmlEncode(psObj.ToString().Trim('@', '{', '}')));
            };
        };
    
        return items;
    }



    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Nikul Vyas Wednesday, June 12, 2019 7:25 PM
    Wednesday, June 12, 2019 7:12 PM
    Moderator