Running PS 3.0 cmdlets with FIM PS WF

Problem

You want to run PowerShell 3.0 cmdlets through FIM PowerShell Workflow Activity (FIM PS WF) (https://fimpowershellwf.codeplex.com)?

As you may or may not know FIM2010 R2 Service is using a workflow host based on Workflow Foundation 3.5 (.NET 3.5). This means that you can only run PowerShell 2.0 cmdlets through the workflow from FIM.

Background

I just recently did a FIM project were we needed to run some DFS cmdlets to create folder and DFS path when provisioning new users to AD. DFSN module is only available in 3.0 version of PowerShell.
When I try to run my script through PS WF (with PS version 2.0) I got this error:

System.InvalidOperationException: Exception of type 'Microsoft.ResourceManagement.Workflow.WorkflowExtensionException' was thrown.
at FimExtensions.FimActivityLibrary.PowerShellActivity.Execute(ActivityExecutionContext context)
at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(T activity, ActivityExecutionContext executionContext)
at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(Activity activity, ActivityExecutionContext executionContext)
at System.Workflow.ComponentModel.ActivityExecutorOperation.Run(IWorkflowCoreRuntime workflowCoreRuntime)
at System.Workflow.Runtime.Scheduler.Run()

So I figured I should try to do some kind of “wrapper” to fire a new PS session from within the PS 2.0 session that FIM created through the WF. I tried out a lot of variants and finally landed on one that worked. (Please comment if you know a better/other way to solve this, bare in mind that I`am NO PS guru).

Solution

From within FIM Powershell Workflow I called the DFSScript.ps1 with invoke-command, and passed FIM PS WF values to the session:

$username = $fimwf.WorkflowDictionary.usraccountname
$displayname = $fimwf.WorkflowDictionary.usrdisplayname
Invoke-Command -ComputerName SRV-FIM01 -FilePath C:\FIMRun\Workflows\DFSScript.ps1 -ArgumentList $username, $displayname

Image 410

Now the key here is to “receive” the params passed from the invoke-command in the DFSScript.ps1 like this:

param ($username,$displayname) $username,$displayname

Now you can use the variables as you have them in the new PS 3.0 session 🙂

Here is an example of the DFSScript.ps1

Enjoy!

  • Sean McGovern

    i found another way i think, when i put the script in a ps1 file and call it using c:WindowsSystem32WindowsPowerShellv1.0powershell.exe -file “script-path.ps1” -fimwf $fimwf

    i exported the $host.version to a file and verified it was running at 4.0

    • Sean McGovern

      correction to that tip, you will have problems passing in an xml, instead just pass the requestguid and targetguid as a string. nice benefit now that i have it working is i can use start-transcription.

      i used a unc path to a powershell script because i have multiple fim service servers and want to edit a single powershell script. not sure how well this method will work in the long run but so far so good.