0

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 :-

enter image description here

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

6
  • Isn't Build there for you to use? Commented Aug 18, 2015 at 10:52
  • @CliveDM i mean how i can access it as i usually deal with strongly typed objects where i write something such as modelobkect.buiild;.. Commented Aug 18, 2015 at 10:53
  • Uhh, that you can use dynamic keyword. Commented Aug 18, 2015 at 10:54
  • can yuo adivce more please Commented Aug 18, 2015 at 11:14
  • I composed an answer, please have a try. Commented Aug 18, 2015 at 11:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

but when i try the following "results.Build" i got the following error 'System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject>' does not contain a definition for 'Build'
@johnG Well, this result seem to be a collection, set a breakpoint on it and inspect what does it have. The picture doesn't seem to be the root, try post the actual tree structure here.
it is under a [0] then i got Base
@johnG, Yep, I edited my answer, have a try then ;-)

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.