Monday 15 September 2014

Powershell from Orchestrator

I’ve recently been trying to call and existing script (quite large) from System Center Orchestrator R2 and hit a number of issues.. The first one being that the script was reporting to run in the runbook, but not actually appearing to do anything, despite a “hello world” working fine.

I suspected it was down to the parameter validation, I found a post here which detailed how to run v3 scripts from orchestrator http://karlprosser.com/coder/2012/04/16/calling-powershell-v3-from-orchestrator-2012/ 

Armed with this I created a bit of code to wrap up the execution using this. The problem with this was I still wasn’t seeing any output, despite my script throwing errors, and logging when run in the console. I attempted to use |out-file to get some debugging, but this didn’t work right well either..

However redirecting the output using
2>&1 | tee -filePath c:\results.txt
me the information I needed..



# Get DataBusVariables and store them ready to pass through to a ps V3/4 Process.
$inobj = new-object pscustomobject -property @{
          ServerName = "\`d.T.~Ed/{4183F8A1-F775-4FC1-81A2-534907708B56}.{D8A8CCDC-C2D4-491B-A639-63E9163D1345}\`d.T.~Ed/"
          ServerDescription = "\`d.T.~Ed/{4183F8A1-F775-4FC1-81A2-534907708B56}.{BD45F4BD-D6E2-4309-BAC4-0AF3A4A519E4}\`d.T.~Ed/"
          DatastoreName = "\`d.T.~Ed/{4183F8A1-F775-4FC1-81A2-534907708B56}.{AB24D540-4CDD-4607-B209-E26F9A9C0825}\`d.T.~Ed/"
          OSFamily = "\`d.T.~Ed/{4183F8A1-F775-4FC1-81A2-534907708B56}.{BFC64A8B-3280-4E45-95D8-EA1A58D869F9}\`d.T.~Ed/"
          WindowsEdition = "\`d.T.~Ed/{4183F8A1-F775-4FC1-81A2-534907708B56}.{9A2ED74C-49BA-410E-9D24-C45FFD3A194E}\`d.T.~Ed/"
          FolderName = "\`d.T.~Ed/{4183F8A1-F775-4FC1-81A2-534907708B56}.{7F137DE7-C64A-454F-A1DA-3827913202E7}\`d.T.~Ed/"
          ClusterName = "\`d.T.~Ed/{4183F8A1-F775-4FC1-81A2-534907708B56}.{8DF61766-4734-4692-86EE-2A5EBD86A0A1}\`d.T.~Ed/"
          Network = "\`d.T.~Ed/{4183F8A1-F775-4FC1-81A2-534907708B56}.{C5FF09B0-FDE4-4968-8FD6-3B10DF29B2FE}\`d.T.~Ed/"
}

# Call V3/4
$PSE = $inobj | Powershell{
    $inp = $input | select -first 1
         
          $command = 'C:\scripts\CreateVMandDeployOS.ps1 -ServerName $($inp.ServerName) -ServerDescription $($inp.ServerDescription) -DatastoreName $($inp.DatastoreName) -OSFamily $($inp.OSFamily) -WindowsEdition $($inp.WindowsEdition) -FolderName $($inp.FolderName) -ClusterName $($inp.ClusterName) -Network $($inp.Network)'
          # write-output "Invoking Command " $command.ToString()
          invoke-expression $command
          } 2>&1 | tee -filePath c:\results.txt

Second problem was then that it couldn’t  import the relevant modules using import-module, main tip with this? Remember to launch x86 powershell window when you are testing, and if necessary drop the modules into the syswow64 modules path…