VMware Cloud Community
parakkatil
Contributor
Contributor
Jump to solution

Adding VM tags workflow

Hi,

Based on  this blog - https://code.vmware.com/forums/3055/vrealize-orchestrator#579949, I am testing the workflow to add tag to VMs by invoking a powercli script from vRO workflow.  In short here is what mentioned in the blog.

$DC = "%%location%%"
$appCode = "%%appcode%%"

 

If ($DC -eq "DCNAME1") { $vc = "VCNAME1" }
If ($DC -eq "DCNAME2") { $vc = "VCNAME2" }

 

##### Imports Core Module  #####
If (!(Get-Module VMWare.VimAutomation.Core)) { Import-Module VMWare.VimAutomation.Core }

 

##### Connects to vCenter  #####
Connect-VIServer $vc -Credential $creds | Out-Null

 

##### Validates tag exist, and if not, creates it, and attaches it to specific category  #####
If (!(Get-Tag $appCode -ErrorAction SilentlyContinue))
{
    New-Tag -Name $appCode -Category (Get-TagCategory APP_CODE) -ErrorAction SilentlyContinue
}

 

##### Executes tag assignment  #####
if (!(New-TagAssignment -Entity (Get-VM %%vmName%%) -Tag (Get-Tag $appCode) -ErrorAction SilentlyContinue)) { return $false } else { return $true }

 

##### Closes connection to vCenter  #####
Disconnect-VIServer * -Force -Confirm:$false

 

Custom action:

Invoke Scrip Custom Action.png

Scriptable task.png

 

I am passing my vm tag as input attribute just to test the workflow. When i run the workflow, i can see  the logs where the input attribute replaces the variable in the powercli. But i keep encountering the error "The term '?' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

It seems to be a powercli related error but have verified each line and no "?" term exists here, I even replaced the script to exact same as pasted in the blog( with values pertaining to my environment) and still hit on the same error. If i am to hard code the varible values and run the powercli script, it runs perfectly fine.

Is there anything that i am missing. I am no javascript excpert. Appreciate any help on this.

i am pasting the logs here.

 

***************************************************************

[2018-11-30 16:42:57.325] [E] (io.mlctech.hi/invokeScript) Error in (Dynamic Script Module name : invokeScript#30) PowerShellInvocationError: Errors found while executing script 

System.Management.Automation.CommandNotFoundException: The term '?' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)

   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)

   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)

   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)

   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)

   at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.<InvokeWithPipe>b__0()

   at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)

   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)

   at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)

   at Microsoft.PowerShell.Commands.InvokeExpressionCommand.ProcessRecord()

   at System.Management.Automation.CommandProcessor.ProcessRecord()

Write-Host " Adding Tag to VM"
 
$DC = "DC2"
$vmversion = "v3.3"
$vmname = "HIP02LGLRDSAP01"
 
##### Imports Core Module  #####
If (!(Get-Module VMWare.VimAutomation.Core)) { Import-Module VMWare.VimAutomation.Core }
 
 
 
If ($DC -eq "DC1") { $vc = "mdc1pld-vcs001.ad.mlclife.com.au" }
If ($DC -eq "DC2") { $vc = "mdc2pld-vcs001.ad.mlclife.com.au" }
 
 
 
 
# Access AES Encrypted password
$User = "mlcl\a-sreejith.parakkati"
$PasswordFile = "C:\scripts\keys\healthcheck.txt"
$KeyFile = "C:\scripts\keys\HealthCheckAES.key"
$key = Get-Content $KeyFile
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)
 
 #Connect to each vCenter server
Write-Host "Connecting to vCenter Servers...."
 
Connect-VIServer $vc -Credential $Credentials | Out-Null
    
 
 
 
 
##### Executes tag assignment  #####
if (!(New-TagAssignment -Entity (Get-VM $vmname) -Tag (Get-Tag $vmversion) -ErrorAction SilentlyContinue)) { return $false } else { return $true }
 
 
 
##### Closes connection to vCenter  #####
Disconnect-VIServer * -Force -Confirm:$false
[2018-11-30 16:42:57.325] [E] (io.mlctech.hi/invokeScript) Error in (Dynamic Script Module name : invokeScript#30) PowerShellInvocationError: Errors found while executing script 
System.Management.Automation.CommandNotFoundException: The term '?' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
   at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.<InvokeWithPipe>b__0()
   at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
   at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)
   at Microsoft.PowerShell.Commands.InvokeExpressionCommand.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
Reply
0 Kudos
1 Solution

Accepted Solutions
parakkatil
Contributor
Contributor
Jump to solution

hawks76daphnissov

My update. i got it work after passing the script as a string instead of the resource element. Something that I need to troubleshoot later as to why using resource element did not work.

Another issue that i came across was the "payload" properties you specify in the workflow script is case sensitive and need to be exactly the way it is in vRA property dictionary. I just used logging to find where the error is at each stage.

Hopefully this helps those who encounter similar issue.

Thank you hawks76​ for posting and reaching out to help.

daphnissov​ Thank you again.

View solution in original post

Reply
0 Kudos
7 Replies
hawks76
Enthusiast
Enthusiast
Jump to solution

Copy the Powershell code out and run it on the powershell host in ISE.  that should give you more detail where the error is actually occuring.
Reply
0 Kudos
daphnissov
Immortal
Immortal
Jump to solution

Is your end goal here to assign vSphere tags to VMs resulting from vRA deployments, or is this a standalone vRO thing you're trying to make work?

Reply
0 Kudos
parakkatil
Contributor
Contributor
Jump to solution

Thank you so much for responding. I have tried that and found no errors and tagging works like a charm via the script. It is only when I invoke the same over vRO workflow I get this error.

I used the same script in the default PowerShell library workflow - invoke PowerShell Script. This also works as expected.

My next troubleshoot would be to use the script as a string instead of the resource element. I will let you know how that goes.

Thanks again.

Reply
0 Kudos
parakkatil
Contributor
Contributor
Jump to solution

@daphnissov. Thank you for responding.

Yes. That is correct. I am trying to add vsphere tags.

But in order to that I am testing the workflow first. My VM deployments are successful and find that the workflow that invokes a PowerShell script to tag VM fails with the above error I pasted.

I am also trying another step of adding the script as string instead of resource element. I will update how it goes.

Thank you for your help.

Reply
0 Kudos
daphnissov
Immortal
Immortal
Jump to solution

Ok, well you can certainly continue to grind away on this, but I've found in my experience that applying vSphere tags via PS and via vRO usually doesn't work out well in the end. It also dramatically complicates your data path as well. My strong recommendation to you, since you are trying to get this through vRA, is to get the SovLabs module for vSphere tagging. I have written an article covering exactly how this works and, more importantly, how dead simple it is to accomplish with no vRO code and no PowerShell translation. It does cost a couple bucks, yes, but you can get a free trial and it leverages the vSphere API directly and you don't write any code whatsoever to maintain or configure it.

Reply
0 Kudos
parakkatil
Contributor
Contributor
Jump to solution

daphnissov Firstly thanks for your time and effort here. Respect and appreciate it.

As of now, it is highly unlikely to get Sovlabs implemented.Cost is one factor along with other complications. It sounds odd but this is how it is. While i am hopeful, that in the long run we will have this sorted, invoking Powershell script is more reasonable in the interim.

But thank you again for sharing your recommendations.

Reply
0 Kudos
parakkatil
Contributor
Contributor
Jump to solution

hawks76daphnissov

My update. i got it work after passing the script as a string instead of the resource element. Something that I need to troubleshoot later as to why using resource element did not work.

Another issue that i came across was the "payload" properties you specify in the workflow script is case sensitive and need to be exactly the way it is in vRA property dictionary. I just used logging to find where the error is at each stage.

Hopefully this helps those who encounter similar issue.

Thank you hawks76​ for posting and reaching out to help.

daphnissov​ Thank you again.

Reply
0 Kudos