Quantcast
Channel: VMware Communities : All Content - All Communities
Viewing all articles
Browse latest Browse all 175326

Adding VM tags workflow

$
0
0

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()

Viewing all articles
Browse latest Browse all 175326

Trending Articles