Executing PowerShell Agent Commands

This chapter contains the following sections:

Cisco UCS Director Orchestrator Workflow and PowerShell Command

Cisco UCS Director Orchestrator automates complex tasks by organizing them into workflows. Once built and validated, these workflows perform the same way every time, no matter who runs the workflows. These tasks encompass a wide variety of supported Cisco and non-Cisco hardware and software data center components.

A workflow, for example, can consist of the following Orchestrator elements:

  • Start icon

  • Task icons
 (like the Execute PowerShell Command)

  • Both Completed (Success) task and Competed (Failed) task icons

For more information about workflows, see Cisco UCS Director Orchestration Guide.

PowerShell commands are used for executing workflows on a target server. Cisco UCS Director offers the following two types of command tasks:

  • Execute PowerShell Command Task

  • Execute Native PowerShell Command Task

Execute PowerShell Command Task

The Execute PowerShell Command Task can run PowerShell scripts only on a remote server. When a command is executed, PowerShell Agent opens a remote PowerShell connection (PSSession) to the target server. Scripts are then executed on the target server. The connection is closed once the execution is complete.

Limitations of the Execute PowerShell Command Task

  1. In certain scenarios, PowerShell cmdlets are not executable and the script fails. This failure occurs because opening the remote session (PSSession) does not provide a complete PowerShell interactive desktop capability. As a result, the scripts are run in an environment that is different from the native PowerShell Agent console. For example, some Windows Active Directory cmdlets cannot be executed under PSSession unless they are run from a device that has a specific Windows Active Directory role associated with it.

  2. PowerShell scripts may also fail if your environment has a multi-hop delegation and you have not enabled the CredSSP protocol in your infrastructure. This failure occurs because the scripts that are executed remotely cannot connect to other Windows machines.

Example of Execute PowerShell Command Task Inputs:

  • PowerShell Agent to be used for executing the script.

  • Target Server's credentials (IP address, username and password, and domain)

  • Commands or Scripts of up to 64 kb.

When you execute the workflow in Cisco UCS Director, you are prompted to enter the PowerShell Agent commands to run on a target server. Use a ";" to separate multiple commands (for example, Hostname; Get-Process). Cisco UCS Director runs the commands against the target server and displays the output as an XML string in a service request log window.

See Executing PowerShell Commands for detailed steps.

Execute Native PowerShell Command Task

The Execute Native PowerShell Command Task creates a native PowerShell instance on a Windows machine and executes the scripts natively on the PowerShell Agent. As a result, the target scripts are not sent over a remote WinRM session for execution. They are run locally on the PowerShell Agent. This feature allows you to bypass the limitations encountered when the scripts are run in a PSSesion using the Execute PowerShell Command Task.

The Execute Native PowerShell Command task simulates the PowerShell Console on a Windows machine. As a result, the command runs natively in a PowerShell CLI session. All the functionality offered by the PowerShell console is available through this task.

Limitations of the Execute Native PowerShell Command Task

  1. Certain windows commands are not supported when the output format is set to JSON and as a result a task may fail. There are no issues if the output format is set to XML.

    Run the following command to convert the output format to XML:

    dir| ConvertTo-Xml -As String
  2. The Write-Error cmdlet causes the task to fail because this cmdlet is not compatible with the one on the console.

  3. Certain cmdlets, such as Enter-PSSession and Write-Error, require an interactive shell fail with the error message method not implemented. You can use Invoke-Command if the cmdlet Enter-PSSession fails.

  4. When executing the ExecutingNativePowerShellComand task in certain scenarios such as Import-Module cmdlets on SCVMM 2012, you might get the following error message:
    Mixed mode assembly is built against version 'v2.0.50727′ of the runtime and cannot
     be loaded in the 4.0 runtime without additional configuration information.

    In such scenarios, it is recommended to use the ExecutePowershellCommand task instead of the ExecutingNativePowerShellComand task.

See Executing PowerShell Commands for detailed steps.

Executing PowerShell Commands

Open a web browser and log on to Cisco UCS Director to execute the PowerShell commands.

Procedure
    Step 1   Choose Orchestration.
    Step 2   On the Orchestration page, click Workflows.
    Step 3   Click Add.
    Step 4   Complete the fields for the Add Workflow wizard.
    Step 5   Click Submit.
    Step 6   On the Available Tasks screen, select the Execute PowerShell Command task. Drag and drop the task in the Workflow Designer pane.
    Step 7   Double click the Execute PowerShell Command.
    Step 8   On the Task Information screen, leave the default values. Click Next.
    Step 9   On the User Input Mapping screen, check the Map to User Input box if you want prompts for any of the task values during workflow execution. Click Next.
    Step 10   On the Task Inputs screen, complete the following fields.

    Name

    Description

    Label field

    Enter a name for the task.

    PowerShell Agent drop-down list

    Select the PowerShell agent to be used for executing the script.

    Target Machine IP field

    Enter the target machine IP address. Provide the DNS or NetBIOS name for Kerberos authentication.

    User ID field

    Specify the local admin user for basic authentication.

    Password field

    Enter the password.

    Domain field

    (Optional) Enter the domain name of the target server.

    Authentication Mechanism drop-down list

    Select the authentication type.

    See Authentication Mechanisms

    Commands/Script

    Provide a script for any task that you want to run later (like add a domain, GET-Process, and so on).

    Commands/Rollback Script

    The Commands and Scripts to be executed on the PowerShell Agent if the executed scripts fail.

    This is an optional field.

    Output Format drop-down list

    Choose the output format of the PowerShell script. Choices are XML or JSON.

    Depth drop-down list

    Enter the level to which the contained objects are shown in the XML/JSON output.

    Maximum Wait Time drop-down list

    Enter the time (in minutes) for which the task waits for the scripts to be executed.

    Note   

    In the Commands/Script field, certain character sequences, like / and $ may not work because the PowerShell Agent runs an Invoke-Command cmdlet with the remote PSSession and there are some limitations in sending special characters to that cmdlet.

    Step 11   Click Submit.
    Step 12   Click Validate to verify the new workflow.
    Step 13   Click Execute.

    The Command Output window displays the execution results.


    Example: Setting Up PowerShell Agent and Running a Test Task

    The following example outlines how you can set up PowerShell Agent on a Windows server and run a test task.

    Procedure
      Step 1   Create a Microsoft Windows Server 2008 R2 or 2012 R2 VM.
      Step 2   Make sure that the VM has the required .NET Framework and Windows PowerShell versions.
      Step 3   Open a web browser and log on to Cisco UCS Director.
      Step 4   Choose Administration > Virtual Accounts.
      Step 5   On the Virtual Accounts page, click PowerShell Agents.
      Step 6   Click Download Installer and install the PowerShell Agent. See Downloading Cisco UCS Director PowerShell Agent
      Step 7   In Windows Firewall, open the port that has been configured for the PowerShell Agent (the default port is 43891).
      Step 8   Open PowerShell and run the following commands:

      Enable-PSRemoting -Force

      Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*" -Force

      Restart-Service WinRM

      Set-ExecutionPolicy unrestricted -Force

      Step 9   Log on to Cisco UCS Director again and run the Execute the PowerShell command. See Executing PowerShell Commands.

      Limitations of Execute Native PowerShell Command Task

      The Execute Native PowerShell Command task helps you to execute PowerShell scripts on the PowerShell Agent server. This task overcomes the limitations of the some of the third party cmdlets, which might not execute in remote sessions.


      Note


      To run PowerShell scripts from a remote server, use the Execute PowerShell Command task.


      Though the Execute Native PowerShell Command task simulates the PowerShell Console on a Windows server to the closest extent possible, there are a few limitations:

      • The Write-Error cmdlet does not work similarly to the one on the PowerShell console. The Write-Error cmdlet causes the task to fail.

      • Certain cmdlets (for example, Enter-PSSession and Write-Host) that require an interactive shell do not work. Such cmdlets fail with the following error message:
        Method not implemented
        
        

        As the Enter-PSSession cmdlet does not work for establishing remote sessions, the workaround is to use Invoke-Command.