how to read the returned values from running a powershell script

I am work on an asp.net mvc web application and i am calling powershell scripts inside my web application, as follow:-

var shell = PowerShell.Create();

string PsCmd = "add-pssnapin VMware.VimAutomation.Core; $vCenterServer = '" + vCenterName + "';$vCenterAdmin = '" + vCenterUsername + "' ;$vCenterPassword = '" + vCenterPassword + "';" + System.Environment.NewLine;

PsCmd = PsCmd + "$VIServer = Connect-VIServer -Server $vCenterServer -User $vCenterAdmin -Password $vCenterPassword;" + System.Environment.NewLine;

PsCmd = PsCmd + "Get-VMHost " + System.Environment.NewLine;  

shell.Commands.AddScript(PsCmd);

var results = shell.Invoke();

now I will get the following values :-

so can anyone adivce how i can access values such as Build, or loop thorugh thr NetworkInfo ? thanks EDIT full trace picture:-

Answers


With dynamic keyword, you wouldn't have to know the strong type of one object. In fact, without having to know the type in advance, you can use this as you expected: a variable can be anything.

For example, I could declare a method like this:

    static void DynamicTest(dynamic arg)
    {
        Console.WriteLine(arg.aaa);
    }

DynamicTest accessed aaa field(or property) in arg without knowing that whether arg itself has aaa or not. Using a dynamic means that you don't want compiler to detect the possible errors that you may or may not access something from a variable that does not actually have it.

You can call this method with:

DynamicTest(new {aaa = "I am accessible"});

it'll run, also you can call this method with:

DynamicTest(1); // I will cause runtime exception

So, in your specific situation, you could define the variable result as dynamic since you now know that you'll get a variable that contains several properties that you want to use.

var res = shell.Invoke()[0];
dynamic obj = res.BaseObject;

Therefore you can use it like a regular variable(without IntelliSense of course).

Console.WriteLine(obj.Build); // Now I can compile yay!

Here.


Need Your Help

Python Twisted: how do use different resource hierarchies for localhost connections?

python resources twisted

I'd like to use one resource hierarchy for localhost connections and another resource hierarchy for all other connections. How would I go about this?

Display the first frame of gif image inside a web page

javascript html gif

i have a web page that displays gif images ,i want to display the first frame without animation and i dont have a still version of it , is this possible ?? any suggestions ??

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.