Hello readers! PowerShell scripts executed within a System Center Orchestrator runbook uses built-in “Run .NET Script” activity which by default uses PowerShell version 2.0. At many times we would require PowerShell script’s to be executed in version 3.0 and one of the way to do it is by executing PowerShell.exe from “Run .NET Script” activity. A script can be run in PowerShell Version 3.0, 64-bit PowerShell environment using C:WindowssysnativeWindowsPowerShellv1.0powershell.exe { <your code> }.
As part of System center orchestrator runbook workflow the data might be required to be published on data bus from “Run .NET Script” activity and for the above kind of scenarios a PowerShell custom object has to be created in PowerShell Version 3 . The below example briefs how this can be done using “Run .NET Script” activity.
[sourcecode language='powershell' ] $inputobjs1 = C:WindowssysnativeWindowsPowerShellv1.0powershell.exe { $SvchostPID = get-process | where { $_.ProcessName -eq 'svchost'} | select -ExpandProperty id $NotepadPID = get-process | where { $_.ProcessName -eq 'notepad'} | select -ExpandProperty id New-Object pscustomobject -Property @{ SvchostPID_OP = $SvchostPID NotepadPID_OP = $NotepadPID } } $SvchostPID =$inputobjs1.SvchostPID_OP $NotepadPID = $inputobjs1.NotepadPID_OP [/sourcecode]
Using the similar type of code, I was working upon a script wherein I was not able to retrieve the data stored in PS custom object out of PowerShell version 3.0 and after troubleshooting for hours, I was able to identify the issue.
Let me explain the issue using a sample script to provide an better understanding, the below script will connect to SCVMM server and gets information of Number of VCPU’s , Memory and Generation of a specific virtual machine.
[sourcecode language='powershell' ] $inputobjs2 = .$env:windirsysnativewindowspowershellv1.0Powershell.exe{ import-module virtualmachinemanager Get-SCVMMServer -ComputerName scvmm2012R2 $vmvalues = get-vm testvm3 | select -Property cpucount, memory, generation New-Object pscustomobject -Property @{ CPUCount_OP = $vmvalues.cpucount Mem_OP = $vmvalues.Memory Generation_OP = $vmvalues.Generation } } $cpucount = $inputobjs2.CPUCount_OP $Mem = $inputobjs2.Mem_OP $generation = $inputobjs2.Generation_OP [/sourcecode]
This script will not be able to pass the values to Orchestrator data bus using PS custom object and to make this script work, line number 3 has to be updated with storing SCVMM connection values to a variable.
$session = Get-SCVMMServer -ComputerName scvmm2012R2
This is required because whenever we connect to SCVMM server, an another shell is invoked and further script gets executed in the shell due to which the PS custom object created cannot be retrieved to Orchestrator data bus.
The updated script is as below:
[sourcecode language='powershell' ] $inputobjs2 = .$env:windirsysnativewindowspowershellv1.0Powershell.exe{ import-module virtualmachinemanager $session = Get-SCVMMServer -ComputerName scvmm2012R2 $vmvalues = get-vm testvm3 | select -Property cpucount, memory, generation $session.disconnect() New-Object pscustomobject -Property @{ CPUCount_OP = $vmvalues.cpucount Mem_OP = $vmvalues.Memory Generation_OP = $vmvalues.Generation } } $cpucount = $inputobjs2.CPUCount_OP $Mem = $inputobjs2.Mem_OP $generation = $inputobjs2.Generation_OP [/sourcecode]
So folks, whenever you are connecting to applications through PS cmdlets which invokes its own shell and you require data to be passed back to Orchestrator, add a point to save the connection parameters to a variable.
Cheers !!