{"id":2305,"date":"2012-10-01T22:05:34","date_gmt":"2012-10-01T22:05:34","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?p=2305"},"modified":"2012-10-01T22:06:04","modified_gmt":"2012-10-01T22:06:04","slug":"having-a-play-with-craig-martins-fim-powershell-activity","status":"publish","type":"post","link":"https:\/\/www.wapshere.com\/missmiis\/having-a-play-with-craig-martins-fim-powershell-activity","title":{"rendered":"Having a play with Craig Martin&#8217;s FIM PowerShell Activity"},"content":{"rendered":"<p>I&#8217;ve been wanting to explore the possibilities of Craig Martin&#8217;s <a href=\"http:\/\/fimpowershellwf.codeplex.com\/\">FIM PowerShell Workflow Activity<\/a> for a while, and now my lab is out of it&#8217;s TechEd bubble-wrap I can get back to play.<\/p>\n<p>In this post a couple of extra steps I had to take to get it working on R2. I&#8217;ll post sample scripts later.<!--more--><\/p>\n<p><strong>Recompiling<\/strong><\/p>\n<p>I downloaded the pre-compiled R2 version from codeplex but it didn&#8217;t work for me straight away. I got this error:<\/p>\n<pre>System.InvalidOperationException: Could not load file or assembly 'Microsoft.ResourceManagement, Version=4.1.1906.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)\r\n   at FimExtensions.FimActivityLibrary.PowerShellActivity.Execute(ActivityExecutionContext context)\r\n   at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(T activity, ActivityExecutionContext executionContext)\r\n   at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(Activity activity, ActivityExecutionContext executionContext)\r\n   at System.Workflow.ComponentModel.ActivityExecutorOperation.Run(IWorkflowCoreRuntime workflowCoreRuntime)\r\n   at System.Workflow.Runtime.Scheduler.Run()<\/pre>\n<p>Craig suggested I recompile which did the trick. I downloaded the source from codeplex, opened in VS2010 on my FIM2010 R2 server, and ran a Build. I did not change any references. The new DLL works fine.<\/p>\n<p><strong>Installing in the GAC<\/strong><\/p>\n<p>Craig includes a nice <a href=\"http:\/\/fimpowershellwf.codeplex.com\/releases\/view\/86291\">script<\/a> to install the activity for you &#8211; however I&#8217;m pretty used to\u00c2\u00a0the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ff720152(v=ws.10).aspx\">FIMPowershell.ps1 helper script from\u00c2\u00a0TechNet<\/a> so I modified the &#8220;Create AIC&#8221; part of Craig&#8217;s script to use those functions instead of the ones from his module.<\/p>\n<pre>###\r\n### Create the Activity Information Configuration\r\n### (requires FIMPowershell.ps1 from http:\/\/technet.microsoft.com\/en-us\/library\/ff720152(v=ws.10).aspx)\r\n###\r\n\r\n. .\\FIMPowerShell.ps1\r\n\r\n$importObj = CreateImportObject -ObjectType ActivityInformationConfiguration\r\nSetSingleValue $importObj  DisplayName  'PowerShell Activity'\r\nSetSingleValue $importObj Description 'PowerShell Activity from CodePlex (fimpowershellwf.codeplex.com)'\r\nSetSingleValue $importObj ActivityName 'FimExtensions.FimActivityLibrary.PowerShellActivity'  \r\nSetSingleValue $importObj TypeName  'FimExtensions.FimActivityLibrary.PowerShellActivitySettingsPart'\r\nSetSingleValue $importObj  AssemblyName  $wfAssembly.FullName\r\nSetSingleValue $importObj IsActionActivity $true\r\nSetSingleValue $importObj IsAuthenticationActivity $false\r\nSetSingleValue $importObj IsAuthorizationActivity  $true\r\nSetSingleValue $importObj IsConfigurationType $true\r\nimport-fimconfig -importObject $importObj<\/pre>\n<p><strong>Allow the Activity to run FIM PowerShell cmdlets<\/strong><\/p>\n<p>It turns out there is a config file modification to make if you want your activity to run a PowerShell script that uses the FIM cmdlets, such as Export-FIMConfig and Import-FIMConfig. The details of the change are in this forum thread: <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en-US\/ilm2\/thread\/295fc491-ffc8-4e02-b923-43af1c3aef1d\">http:\/\/social.technet.microsoft.com\/Forums\/en-US\/ilm2\/thread\/295fc491-ffc8-4e02-b923-43af1c3aef1d<\/a><\/p>\n<p><strong>Using\u00c2\u00a0a FIM-enabled Account<\/strong><\/p>\n<p>If you want the script to run FIM cmdlets you&#8217;ll also have to think about the account being used.<\/p>\n<p>When you run the script from the FIMPowersell activity it will run as the FIM Service service account. This account does not exist in the FIM Portal by default. You could add the service account as a Portal user. I preferred to take the approach of (securely) hard-coding the credentials of\u00c2\u00a0another account in the script:<\/p>\n<ol>\n<li><em>As the FIM Service user <\/em>encrypt and store the password of the account you want to use to run FIM cmdlets:\n<pre>read-host -assecurestring | convertfrom-securestring | out-file encrypted.txt\r\n&lt;<em>type the password<\/em>&gt;<\/pre>\n<\/li>\n<li>Create a credential object as part of your script:\n<pre>$AdminAccount = <em>accountname<\/em>\r\n$password = get-content encrypted.txt | convertto-securestring\r\n$UserCredential = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist $AdminAccount,$password<\/pre>\n<\/li>\n<li>Then just use the credential object whenever calling a FIM cmdlet in your script:\n<pre>$importObject | Import-FIMConfig -Credential $UserCredential<\/pre>\n<\/li>\n<\/ol>\n<p><strong>Testing<\/strong><\/p>\n<p>I made a simple MPR\/WF combo to run a script every time a particular attribute is changed. Then it&#8217;s a simple matter to modify the attribute and trigger the WF.<\/p>\n<p>For errors you need to check both the Request object and the Forefront Identity Manager application event log.\u00c2\u00a0I also found it useful to write every variable out to a log file so I could see what was going on. Have a look at the FIMRequestLogger.ps1 script included in the source code download for ideas.<\/p>\n<p><strong>In Conclusion<\/strong><\/p>\n<p>I&#8217;m really very interested in this activity as an alternative to developing custom WF, at least in the Action phase. The fact that I can access the Request object and WorkflowData from the PowerShell script is nothing short of brilliant. I can then do all the querying, updating and creating of objects from a PowerShell script as I can do in a custom WF.<\/p>\n<p>Thanks Craig!<\/p>\n<p>Coming soon &#8211; I re-write my <a href=\"https:\/\/www.wapshere.com\/missmiis\/generate-unique-attribute-activity\">GenerateUnique<\/a> activity as a PowerShell script.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been wanting to explore the possibilities of Craig Martin&#8217;s FIM PowerShell Workflow Activity for a while, and now my lab is out of it&#8217;s TechEd bubble-wrap I can get back to play. In this post a couple of extra steps I had to take to get it working on R2. I&#8217;ll post sample scripts&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[60,23,64,45],"tags":[],"class_list":["post-2305","post","type-post","status-publish","format-standard","hentry","category-fim-2010-r2","category-powershell","category-troubleshooting","category-workflow"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pkp1o-Bb","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/comments?post=2305"}],"version-history":[{"count":12,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2305\/revisions"}],"predecessor-version":[{"id":2317,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2305\/revisions\/2317"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=2305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/categories?post=2305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/tags?post=2305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}