VMware Cloud Community
erwabovm
Contributor
Contributor

Java and Powershell

Im trying to execute a powershell script from a Scriptable task and passing input parameters as variable into my script for execution. I have posted the information below, I have no clue on this one. Any help would be appreciated.

My script is like this:

var session = PSHost.openSession()

// ------------------------

session.addCommandFromString("c:\\PowerShell\\ServerConfig.ps1 -IP ivo")

session.addCommandFromString("c:\\PowerShell\\ServerConfig.ps1 -Mask ivo")

session.addCommandFromString("c:\\PowerShell\\ServerConfig.ps1 -Gateway ivo")

session.addCommandFromString("c:\\PowerShell\\ServerConfig.ps1 -name ivo")

session.addCommandFromString("c:\\PowerShell\\ServerConfig.ps1 -username ivo")

//----------------------------

var cmd = session.addCommandFromString("c:\\PowerShell\\ServerConfig.ps1")

cmd.addParameter('IP');

cmd.addParameter('Mask');

cmd.addParameter('Gateway');

cmd.addParameter('name');

cmd.addParameter('username');

//----------------------------

res = session.invokePipeline();

System.error(res.getErrors());

System.log(res.getHostOutput());

PSHost.closeSession(session);

Here is  my visual binding

pastedImage_0.png

Here is the error im getting when I get to this step:

[2017-10-03 12:25:19.569] [E] Error in (Workflow:Server Build Test / Scriptable task (item3)#9) java.lang.NullPointerException

[2017-10-03 12:25:19.579] [E] Workflow execution stack:

***

item: 'Server Build Test/item3', state: 'failed', business state: 'null', exception: 'java.lang.NullPointerException (Workflow:Server Build Test / Scriptable task (item3)#9)'

workflow: 'Server Build Test' (72804cdd-160b-4044-a23f-9934d1be193f)

|  'attribute': name=vmFolder type=VC:VmFolder value=dunes://service.dunes.ch/CustomSDKObject?id='server%2Cid:group-v517104'&dunesName='VC:VmFolder'

|  'attribute': name=powerOn type=boolean value=true

|  'attribute': name=template type=boolean value=false

|  'attribute': name=datastore type=VC:Datastore value=dunes://service.dunes.ch/CustomSDKObject?id='server%2Cid:datastore-467241'&dunesName='VC:Datastore'

|  'attribute': name=host type=VC:HostSystem value=dunes://service.dunes.ch/CustomSDKObject?id='server%2Cid:host-322517'&dunesName='VC:HostSystem'

|  'attribute': name=pool type=VC:ResourcePool value=dunes://service.dunes.ch/CustomSDKObject?id='server%2Cid:resgroup-320564'&dunesName='VC:ResourcePool'

|  'attribute': name=thinProvision type=boolean value=false

|  'attribute': name=vm type=VC:VirtualMachine value=dunes://service.dunes.ch/CustomSDKObject?id='server%2Cid:vm-519330'&dunesName='VC:VirtualMachine'

|  'attribute': name=ADOU type=AD:OrganizationalUnit value=dunes://service.dunes.ch/CustomSDKObject?id='%23_v2_%23%2C%23OU%23%2C%234506f67e-1d33-4e35-a5af-510ea148b5cb%23%2C%23OU%3DVR%20Orchestrator%2COU%3DUSFS%20Servers%2CDC%3domain%2CDC%3Dcom%23'&dunesName='AD:OrganizationalUnit'

|  'attribute': name=Domain type=string value=domain

|  'attribute': name=PSHost type=PowerShell:PowerShellHost value=dunes://service.dunes.ch/CustomSDKObject?id='407d66be-5db4-41c8-8fa6-94609e3fb5c4'&dunesName='PowerShell:PowerShellHost'

|  'input': name=name type=string value=vrotest004

|  'input': name=IP type=string value=10.8.6.100

|  'input': name=Mask type=string value=255.255.255.0

|  'input': name=Gateway type=string value=10.8.6.1

|  'input': name=username type=string value=myuseraccount

|  'output': name=newComputer type=AD:ComputerAD value=dunes://service.dunes.ch/CustomSDKObject?id='%23_v2_%23%2C%23Computer%23%2C%234506f67e-1d33-4e35-a5af-510ea148b5cb%23%2C%23CN%3Dvrotest004%2COU%3DVR%20Orchestrator%2COU%3DUSFS%20Servers%2CDC%3Dshoremortgage%2CDC%3Dcom%23'&dunesName='AD:ComputerAD'

*** End of execution stack.

Reply
0 Kudos
4 Replies
daphnissov
Immortal
Immortal

Not that I'm saying this is the fix, but make sure you have the latest Powershell plug-in that was posted here.

Reply
0 Kudos
erwabovm
Contributor
Contributor

Yes I saw that prior to configuring the PowerShell module. I am on version 1.0.13 and it was on 1.0.10 I believe.

Reply
0 Kudos
rstoker13
VMware Employee
VMware Employee

We experienced some odd issues when using the powershell plugin. After some discussion with SDK support and reading blog posts, we decided to scrap the powershell plugin method and execute via SSH to a Windows host(our vRA DEM servers, but can be any machine.) We followed this blog: Executing Powershell over SSH (no WinRM, no powershell plugin) — Virtual Nebula

Using this method, you can call a 'One Liner', or call a script stored on the destination powershell host with arguments. I can provide examples of both if you are interested. Hope you find this useful.

Reply
0 Kudos
erwabovm
Contributor
Contributor

So I got this working, its had to do with me trying to pass Complex types like VC:VirtualMachine into a java string

Reply
0 Kudos