VMware Cloud Community
erwabovm
Contributor
Contributor
Jump to solution

Powershell Script Results To Attribute

I am using the workflow "Run Script in VM" which is this powershell script:

$searcher = [ADSISearcher]'(&(objectCategory=computer)(name=DOMAINPC*))'

$searcher.PageSize = 1000

$last = $searcher.FindAll() | Foreach-Object { [int]($_.Properties.name -replace '\D').Trim() } | Sort-Object | Select-Object -Last 1

$digitLength = "$last".Length

$vmname= "{0}{1:D}" -f 'DOMAINPC',($last+1)

$vmname

[2017-11-01 12:11:21.515] [I] DOMAINPC205

IN VRO, it returns a name as expected DOMAINPC205. I'm outputting that text (at least I thought I was) to an attribute as a string as seen below

pastedImage_0.png

On my next workflow I'm using that output attribute as an input for creating a PC in an OU, but its always NULL., because the string value in the attribute is null. How can I get the results as a useable attribute?

pastedImage_1.png

I cannot seem to figure this out....any ideas?

Reply
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

One option would be to add a scriptable task between 'Run script in VM guest' and 'Create a computer ...' workflow elements, with the following content:

scriptOutputTextOut = scriptOutputTextOut.trim();

and add scriptOutputTextOut to both IN and OUT tabs of this task.

Need to check if and how this trimming can be done in the PowerShell script code.

View solution in original post

Reply
0 Kudos
29 Replies
sbeaver
Leadership
Leadership
Jump to solution

Couple of questions....

1. Which "Run script in VM" workflow are you using?  Full path of the workflow please

2. When you look at the parameters of the completed workflow is the scriptOutputText parameter populated with anything?

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
Reply
0 Kudos
erwabovm
Contributor
Contributor
Jump to solution

1. Which "Run script in VM" workflow are you using?  Full path of the workflow please

Im using the Guest Script Manager package. Which for me is Guest Operations - Run Script in VM Guest. I believe its almost identical to the built in one

2. When you look at the parameters of the completed workflow is the scriptOutputText parameter populated with anything?

This is the part that i'm not 100% sure how to do. Not sure how to view the output....but im guessing that its not populated. It DOES pass the variable $vmname into a scriptable task further in the flow, so I know the PS Script is at least being run, however...it just isn't taking that value and outputting it as a string that I can reuse in additional work flows.

When I run this by itself, the output in the Log tab is simply a time stamp and the server name  [2017-11-01 15:22:10.047] [I] DOMAINPC206

Reply
0 Kudos
sbeaver
Leadership
Leadership
Jump to solution

in vRO, if you highlight the workflow run and then switch to the Variables tab, you will see all the populated variables from the script.  Can you see if anything was populated in the scriptOutputText variable.  Now I took your script and for fun I run it through my Run script in VM workflow as well as just running the command in straight powershell.  I got the same results as you in the straight powershell but from the workflow this what what my output looked like

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName       -------  ------    -----      ----- -----   ------     --  -- -----------           180      14    15276      13300   516     0.14   1512   0 powershell

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
Reply
0 Kudos
erwabovm
Contributor
Contributor
Jump to solution

So yes...its is adding it in a a string variable but it fails the next piece and I get this

[2017-11-01 15:36:23.819] [E] (com.vmware.library.microsoft.activeDirectory/createComputer) Error in (Dynamic Script Module name : createComputer#0) Failed to create computer... 00000523: SysErr: DSID-031A12C8, problem 22 (Invalid argument), data 0

[2017-11-01 15:36:23.833] [E] Workflow execution stack:

***

item: 'Create a computer in an organizational unit/item0', state: 'failed', business state: 'null', exception: 'Failed to create computer... 00000523: SysErr: DSID-031A12C8, problem 22 (Invalid argument), data 0

pastedImage_0.png

Reply
0 Kudos
sbeaver
Leadership
Leadership
Jump to solution

OK my bad so let me try to explain a little better.  Your workflow has failed and as such the failed part of the workflow is highlighted and what you are seeing is just the variables of the sub-workflow that failed.   So when you have the workflow run hightlight take a look at the top of that and you should see 2 tabs, "Schema" and "Events".  Right below, if the workflow is successful you will see 1 button type object that is titled the name of the host workflow.  When the workflow fails and there are multiple sub-workflows another button of the sub-workflow will be displayed so you should see 2 buttons.  Click the one on the far left (should have the same name as the host workflow) and then you can see all the variables used in the complete workflow.

Does that make better sense?

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
Reply
0 Kudos
erwabovm
Contributor
Contributor
Jump to solution

Here is the full error :

pastedImage_0.png

And here are all the variables

pastedImage_1.png

Reply
0 Kudos
sbeaver
Leadership
Leadership
Jump to solution

This is more in-line of what I was referring to....

pastedImage_0.png

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
Reply
0 Kudos
erwabovm
Contributor
Contributor
Jump to solution

pastedImage_0.png

Reply
0 Kudos
sbeaver
Leadership
Leadership
Jump to solution

I guess it helps when I fully open the image to see everything.  So the PowerShell works and you are getting a name in the return so the issue looks to be with the create a computer workflow.  Have you tried running that separately and did it do what you expected?

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
Reply
0 Kudos
erwabovm
Contributor
Contributor
Jump to solution

It works fine if I use an input parameter...I have several server builds that use that workflow, but it wont work with the PS output being passed as an attribute. Likewise, I have a scriptable task that I tried it with as well and again it detects a value of null...but using a standard input parameter works fine.

Reply
0 Kudos
sbeaver
Leadership
Leadership
Jump to solution

For fun can we try to put a scriptable task after the powershell and before the create computer that takes the scriptOutputText as an input and has something like computerName as the output.  What I am thinking is there might be something up with the way the variable is presented and in the scriptable task lets try to massage that value and clean it up some.  Something like this maybe...

var computerName = scriptOutputText.toString().trim() ;

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

erwabovm​ In all the attached screenshots, I don't see any evidence that a null value is detected. Everywhere, the value is shown as a proper string with value tr01VDSIIS223

This 'failed to create computer ...' error could mean you already has a computer with this name, could you verify that?

Reply
0 Kudos
erwabovm
Contributor
Contributor
Jump to solution

Yeah, there definitely is not another computer and if i capture user input with the same value and pass that as an input parameter it works fine.

Reply
0 Kudos
erwabovm
Contributor
Contributor
Jump to solution

sbeaver - so I tried what you said....and I got an error when it tried to run the scriptable task

2017-11-02 07:35:06.737] [E] Error in (Workflow:IPAM IIS TESTING / Scriptable task (item5)#0) ReferenceError: "scriptOutputText" is not defined.

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

This error means your item labelled 'IPAM IIS TESTING' doesn't have such IN parameter defined.

I guess it will be much more productive if you export your actual workflow and attach it here.

Reply
0 Kudos
erwabovm
Contributor
Contributor
Jump to solution

Yes, thats the issue to begin with. That IN parameter should be the Powershell Output that is sent to an attribute in the previous step.

While the output seems to be there, whenever I try to use it as input in a subsequent step, I get those related error.  

Let me see if I can rework the workflow to have no company information present and attach it here

Reply
0 Kudos
erwabovm
Contributor
Contributor
Jump to solution

OKay here is the workflow

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

The script content of the first scriptable task item is wrong. Instead of:

var computerName = scriptOutputText.toString().trim() ;

it should be:

var computerName = scriptOutputTextOut.toString().trim() ;

Reply
0 Kudos
erwabovm
Contributor
Contributor
Jump to solution

Still got this:

2017-11-02 13:09:45.052] [I] tr01VDSIIS223

[2017-11-02 13:09:45.160] [E] (com.vmware.library.microsoft.activeDirectory/createComputer) Error in (Dynamic Script Module name : createComputer#0) java.lang.NullPointerException

[2017-11-02 13:09:45.170] [E] Workflow execution stack:

***

item: 'Create a computer in an organizational unit/item0', state: 'failed', business state: 'null', exception: 'java.lang.NullPointerException'

workflow: 'IPAM IIS TESTING' (72295aea-318b-4b28-afa7-7b926843a6f0)

Reply
0 Kudos