System Center Orchestrator – Running Powershell

I’ve had a lot of trouble with SCORCH and PowerShell, primarily because Orchestrator will always call PowerShell 2.0. Here’s how I get things done.

Reference: RUNNING AND DEBUGGING POWERSHELL SCRIPTS WITH ORCHESTRATOR 2012

  1. Add two new variables
    1. Powershell Scripts Username
    2. Powershell Scripts Password
  2. ‘Encrypt’ the password variable.
  3. Copy the PowerShell script you’d like to run to C:\Scripts on your target system.
  4. Add a ‘Run Script’ activity to your Runbook.
  5. Use the following code template, but replace username/password with variable subscriptions, and replace the PowerShell script name and path.
    $ErrorActionPreference = "Stop"
    try
    {
        $targetComputer = "server.contoso.com"
        $username = "{Scripts Username}"
        $password = "{Scripts Password}"
        $securePassword = $password | ConvertTo-SecureString -AsPlainText -Force
        $creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username,$securePassword
        $retval = Invoke-Command -Credential $creds -ComputerName $targetComputer -ScriptBlock {& "C:\Scripts\Generate-VMHostGuestInfoReport.ps1"}
    }
    Catch
    {
        Throw $_.Exception
    }
    

Getting data back out is a bit of a problem, so I’ve just been writing data to a file inside my target script, and then reading the file with Orchestrator. It’s a bit of a cludge, but it works.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s