VMware Cloud Community
Mozeps
Contributor
Contributor
Jump to solution

How do i get a Powershell:PowershellHost Object?

Hello guys,

is there a way to get an object of my Powershell Host? I want to open Sessions and send commands directly in my script without a workflow.

Something like this:

//modify properties

psHost.name = "myPSHost01";

psHost.transportProtocol = "HTTPS";

//invoke methods

psHost.openSession();

psHost.invokeScript(script);

psHost.closeSession(id);

With some research here i found this but i cannot use the output of this statement.

var psHosts = Server.findAllForType("PowerShell:PowerShellHost",null);

//com.vmware.o11n.plugin.powershell.scripting.PowerShellHost

for(psHost in psHosts) {

  System.log(psHosts[psHost]);

}

Log:

[2016-11-14 14:17:49.306] [I] DynamicWrapper (Instance) : [PowerShellHost]-[class com.vmware.o11n.plugin.powershell.scripting.PowerShellHost] -- VALUE : com.vmware.o11n.plugin.powershell.scripting.PowerShellHost@3a5c781f

Does anyone of you know how the answer?

Thanks

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

findAllForType() enumerates all object of a given type

var psHosts = Server.findAllForType("PowerShell:PowerShellHost",null); 

for (psHost in psHosts) { 

  var pshostObj = psHosts[psHost]; // object of type PowerShellHost

  var session = pshostObj.openSession();

}

In the above code, pshostObj is of type PowerShellHost, and you can use all its properties/methods like openSession(), etc.

View solution in original post

0 Kudos
7 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

In your sample, is myPSHost01 one of the registered/configured PowerShell hosts, or you want to create a new temporary object on the fly without registering it?

If the latter, I think it is not possible with PS plug-in; only REST plug-in has something similar allowing creation of 'transient' host objects.

If the former, why can't you use the object(s) returned from findAllForType() ?

0 Kudos
Mozeps
Contributor
Contributor
Jump to solution

Hi,

myPSHost01 is one of the registered ones.

I cannot use findAllForType() because the properties and methods of a Powershell Host cannot be used which means i cannot even open a Session.

15-11-2016 08-47-14.png

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

findAllForType() enumerates all object of a given type

var psHosts = Server.findAllForType("PowerShell:PowerShellHost",null); 

for (psHost in psHosts) { 

  var pshostObj = psHosts[psHost]; // object of type PowerShellHost

  var session = pshostObj.openSession();

}

In the above code, pshostObj is of type PowerShellHost, and you can use all its properties/methods like openSession(), etc.

0 Kudos
Mozeps
Contributor
Contributor
Jump to solution

Thank you very much this worked for me.

0 Kudos
xian_
Expert
Expert
Jump to solution

Is there a way to pass a filter to findAllForType (XPath or OData?)

Wanted to search for a specific PS host without looping through the whole array.

PowerShellHostManager does not seem to have a search function Smiley Sad

Thanks.

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Support for XPath or OData filters/queries as a second argument to findAllForType() API is plug-in specific. That is, whatever expression/value you provide there will be passed to the plug-in, but the plug-in should contain some logic to actually parse this expression and utilize it during search.

The PowerShell plug-in does not implement such logic. It simply ignores the value passed as second argument to findallForType() call.

0 Kudos
xian_
Expert
Expert
Jump to solution

Thanks Ilian.

0 Kudos