2 Replies Latest reply on Jun 12, 2019 6:46 AM by Vsure

    What to do in order to run Powershell Code in VRO

    platforminc Novice

      Hi,

       

      I am looking at running some powershell on VRO, my syntax is as follows. However I am not sure which powershell action to use or whether I need to have a custom one.

       

      I am currently using VRO, however this script was written for the older version which is VCO

       

      var psScript = ''

       

       

      //#trap:

      psScript +='\ntrap { \n';

      psScript +='  foreach ($err in $error) {\n';

      psScript +=' write-output $err\n';

      psScript +='  }\n'

      psScript +='  if ( $session -ne $null) {\n';

      psScript +='    remove-pssession -session $session \n';

      psScript +='  }\n';

      psScript +='  exit 1\n';

      psScript +='}\n';

       

       

      psScript +='$ErrorActionPreference = \'Stop\'\n';

      psScript +='import-module sqlps -WarningAction:SilentlyContinue -ErrorAction:Stop \n';

       

       

      //#create session to targethost:

      psScript +='$username = \''+buildaccount+'\'\n';

      psScript +="$password = Get-Content '"+buildaccountpwdfile+"' | ConvertTo-SecureString\n";

      psScript +='$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password\n';

      psScript +='$session = new-pssession -computerName '+targethost+' -credential $credential -authentication credssp\n';

       

      //obtain server name

      psScript +='$sqlserver_server_name = \'select servername from [dbo].[buildlog] where buildno=\'\''+buildno+'\'\'\'\n';

      psScript +='$instancename = invoke-sqlcmd -QueryTimeout 30 -ConnectionTimeout 30 -ServerInstance '+buildinstance+' -Database inventory -Query $sqlserver_server_name\n';

       

               

      psScript += 'get-pssession -ComputerName '+targethost+'| remove-pssession\n';

       

      System.getModule("").executePowershellDirectOnVCOServer(psScript, "Server Identification", null);

       

      System.log(psScript);

        • 1. Re: What to do in order to run Powershell Code in VRO
          aenagy Hot Shot

          platforminc

           

          I am currently using VRO, however this script was written for the older version which is VCO

          If you read the release notes for vRealize Orchestrator 7.0 (VMware vRealize Orchestrator 7.0 Release Notes ) you will see:

           

          VMware vRealize Orchestrator 7.0 is available as a preconfigured virtual appliance. The appliance significantly reduces the time and skills required to deploy vRealize Orchestrator and provides a low-cost alternative to a traditional Windows-based installation.

           

           

          Is PowerShell installed on the vRO appliance? Based on Microsoft's documentation (Installing PowerShell Core on Linux | Microsoft Docs) it doesn't appear that Suse Enterprise Linux 11 is supported.

           

          My thoughts are:

          1) See if the Linux version of PowerShell will install and run on the vRO appliance and then make the necessary changes to 'executePowershellDirectOnVCOServer' or write something from scratch.

          .... or ....

          2) Leverage the PowerShell vRO plugin to call PowerShell on a remote Windows system. I realize that this is not what you asked about.

          • 2. Re:What to do in order to run Powershell Code in VRO
            Vsure Novice

            A few tips based on my recent experience with PowerShell from vRO

             

            1. This article is very helpful for setting and troubleshooting a PowerShell Plugin - http://kaloferov.com/blog/using-credssp-with-the-vco-powershell-plugin/
            2. Whenever possible convert your PowerShell to native vRO code it just runs faster.  We see up to a 10 second delay with any powershell remote call.
            3. If your PowerShell script is performed multiple times in a loop, consider moving the looping action out of vRO and into the PS Script so that you only need to call it once.  That 10 second delay with each remote call will add up to a noticeable lengthy process after just a few iterations.
            4. Keep your PS Scripts as standalone files. Rename them to text files (.txt) and import them as Resources (renaming it to .txt allows it to be viewable within Resources). Link a workflow attribute to the Resource and read in the resource using the getContentAsMimeAttachement method (see below).  You can now download and upload your PS resource for editing independent of its use in your vRO scripts.

             

            //Get The PowerShell Script from Resource Element (psScript is a workflow attributed linked to a Resource)
            var scriptMime = psScript.getContentAsMimeAttachment();
            
            //The PowerShell script consists of only function(s) so we concatenate the command we want to execute onto the end
            var script = scriptMime.content + "Test-IPv4SubnetUsage -NetworkAddress '" + NetworkAddress + "' -NetworkNumber '" + NetworkNumber + "' -RouterAddress '" + RouterAddress + "'"
            
            //Concatenate any conditional parameters
            if (StartRange != ""){
            script = script + " -StartRange '" + StartRange + "'"
            }
            
            if (EndRange != ""){
            script = script + " -EndRange '" + EndRange + "'"
            }
            
            //Open session to PowerShell host passing creds from Global Configuration attributes
            var session = host.openSessionAs(hostUser, hostPassword);
            
            //Execute script and return results
            var result = session.invokeScript(script)