{"id":2325,"date":"2012-10-03T23:19:26","date_gmt":"2012-10-03T23:19:26","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?p=2325"},"modified":"2012-10-03T23:19:26","modified_gmt":"2012-10-03T23:19:26","slug":"rewriting-my-generateunique-activity-as-a-powershell-script","status":"publish","type":"post","link":"https:\/\/www.wapshere.com\/missmiis\/rewriting-my-generateunique-activity-as-a-powershell-script","title":{"rendered":"Rewriting my GenerateUnique activity as a PowerShell script"},"content":{"rendered":"<p>The first custom workflow activity I wrote was one to <a href=\"https:\/\/www.wapshere.com\/missmiis\/generate-unique-attribute-activity\">select a unique value<\/a> from a list of possible values placed in WorkflowData variables. I&#8217;ve now re-written this as a PowerShell script to use with the open source <a href=\"http:\/\/fimpowershellwf.codeplex.com\/\">FIM PowerShell activity<\/a>.<\/p>\n<p>The script here just checks again the Portal, but it would be a simple modification to make it check against AD instead\/as well.<\/p>\n<p><!--more--><\/p>\n<p><strong>Setting up the Workflow<\/strong><\/p>\n<p>First you have to get the FIM PowerShell activity installed. See <a href=\"https:\/\/www.wapshere.com\/missmiis\/having-a-play-with-craig-martins-fim-powershell-activity\">here<\/a> for the extra steps I did to get it working with the FIM PowerShell cmdlets.<\/p>\n<p>The script expects to find one or more options in WorkflowData parameters.\u00c2\u00a0Here is what the Workflow definition looks like:<\/p>\n<p><a href=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniqueWF.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-2326\" title=\"GenerateUniqueWF\" src=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniqueWF.jpg\" alt=\"\" width=\"535\" height=\"378\" srcset=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniqueWF.jpg 764w, https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniqueWF-300x212.jpg 300w\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" \/><\/a>\u00c2\u00a0<\/p>\n<p>The options are just Function Evaluators:<\/p>\n<p><a href=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniqueWFData.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-2327\" title=\"GenerateUniqueWFData\" src=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniqueWFData.jpg\" alt=\"\" width=\"633\" height=\"319\" srcset=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniqueWFData.jpg 791w, https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniqueWFData-300x151.jpg 300w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><\/a><\/p>\n<p>Then the FIM PowerShell activity calls my script:<\/p>\n<p><a href=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniquePS.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-2329\" title=\"GenerateUniquePS\" src=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniquePS.jpg\" alt=\"\" width=\"579\" height=\"86\" srcset=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniquePS.jpg 724w, https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2012\/10\/GenerateUniquePS-300x44.jpg 300w\" sizes=\"auto, (max-width: 579px) 100vw, 579px\" \/><\/a><\/p>\n<p><strong>The Script<\/strong><\/p>\n<pre>### \r\n### Generate Unique\r\n###\r\n### Called using FIMPowershell Workflow Activity.\r\n###     Parameters: \r\n###       -ObjectType The Object Type to be checked\r\n###       -AttributeName The Attribute to be checked for uniqueness\r\n###       -LogFile Verbose logging to a log file if specified\r\n### \r\n###     WorkflowData: The FIM Workflow must provide one or more WorkflowData values to be checked in turn.\r\n###\r\n### The first unique option found will be written back to \\\\Target\\AttributeName\r\n###\r\n\r\nPARAM($ObjectType,$AttributeName,$LogFile)\r\n\r\n### Get FIMPowershell.ps1 from http:\/\/technet.microsoft.com\/en-us\/library\/ff720152(v=ws.10).aspx\r\n. E:\\FIM\\scripts\\FIMPowerShell.ps1\r\n\r\n### The password for the account to use running the scripts must be saved to a file readable by the FIM Service account.\r\n### Execute the following command as the FIM Service account:\r\n###    read-host -assecurestring | convertfrom-securestring | out-file encrypted.txt\r\n$PWFile = 'E:\\FIM\\scripts\\WF\\encrypted.txt'\r\n$AdminAccount = \"fimadmin\"\r\n\r\nif ($LogFile -ne $null) {$Verbose = $true} else {$Verbose = $false}\r\n\r\nif ($Verbose) {get-date | out-file -FilePath $logFile -encoding \"Default\"}\r\n\r\n##\r\n## Create credential to use when running commands against the FIM service\r\n##\r\nif ($Verbose) {\"Creating credential object for account $AdminAccount\" | add-content $logFile}\r\n$password = get-content $PWFile | convertto-securestring\r\n$UserCredential =  New-Object -Typename System.Management.Automation.PSCredential -Argumentlist $AdminAccount,$password\r\n\r\n### \r\n### Get the Request Details\r\n### \r\nif (-not $fimwf)\r\n{\r\n    $msp = \"Failed to get workflow details from the FIM Request\"\r\n    if ($Verbose) {\"Error: \" + $msg | add-content $logFile}\r\n    Throw $msg\r\n}\r\n\r\n$ReqGUID =  $fimwf.RequestId.Guid\r\nif ($Verbose) {\"RequestID: $ReqGUID\"  | add-content $logFile}\r\n$TargetGUID = $fimwf.TargetId.Guid\r\nif ($Verbose) {\"Target: $TargetGUID\" | add-content $logFile}\r\n\r\n### \r\n### Get the WorkflowData\r\n### \r\nif ($Verbose) {\"Getting WorkflowData\"  | add-content $logFile}\r\nif ($Verbose) {$fimwf.WorkflowDictionary  | Out-String -Width 100 | add-content $logFile}\r\n$options = $fimwf.WorkflowDictionary\r\n\r\nforeach ($option in $options.Values)\r\n{\r\n    $filter = \"\/\" + $ObjectType + \"[\" + $AttributeName + \"='\" + $option + \"']\"\r\n    $obj = Export-FimConfig -Custom ($filter) -OnlyBaseResources -Credential $UserCredential\r\n    if ($obj -eq $null)\r\n    {\r\n        $returnOption = $option\r\n        if ($Verbose) {\"$option is unique\"  | add-content $logFile}\r\n        break\r\n    }\r\n    else \r\n    {\r\n        if ($Verbose) {\"$option is not unique\"  | add-content $logFile}\r\n    }\r\n}\r\n\r\nif ($returnOption -ne $null)\r\n{\r\n    if ($Verbose) {\"Updating Target $AttributeName with value $returnOption\"  | add-content $logFile}\r\n\r\n    $importObject = ModifyImportObject  -TargetIdentifier $TargetGUID -ObjectType $ObjectType\r\n    SetSingleValue $importObject $AttributName $returnOption\r\n    $importObject | Import-FIMConfig -Credential $UserCredential   \r\n}\r\nelse { Throw \"Failed to find a unique value\"}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The first custom workflow activity I wrote was one to select a unique value from a list of possible values placed in WorkflowData variables. I&#8217;ve now re-written this as a PowerShell script to use with the open source FIM PowerShell activity. The script here just checks again the Portal, but it would be a simple&#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":[42,60,23,45],"tags":[],"class_list":["post-2325","post","type-post","status-publish","format-standard","hentry","category-fim-2010","category-fim-2010-r2","category-powershell","category-workflow"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pkp1o-Bv","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2325","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=2325"}],"version-history":[{"count":11,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2325\/revisions"}],"predecessor-version":[{"id":2339,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2325\/revisions\/2339"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=2325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/categories?post=2325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/tags?post=2325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}