{"id":2475,"date":"2013-01-12T01:15:31","date_gmt":"2013-01-12T01:15:31","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?p=2475"},"modified":"2013-01-12T01:15:31","modified_gmt":"2013-01-12T01:15:31","slug":"using-powershell-to-query-request-objects","status":"publish","type":"post","link":"https:\/\/www.wapshere.com\/missmiis\/using-powershell-to-query-request-objects","title":{"rendered":"Using PowerShell to query Request objects"},"content":{"rendered":"<p>I&#8217;ve been finding it useful to query Request objects for various reasons, mostly to get pending or historical changes out of them. This post contains a few script snippets and examples. Note I have developed and used these on FIM 2010 R2 only.<\/p>\n<p><!--more--><\/p>\n<h3>Get Requested Value<\/h3>\n<p>This function takes a Request object and an Attribute name and returns the requested value for the attribute. If there are multiple new values in the one Request they are returned as a semi-colon separated string.<\/p>\n<pre>Function RequestedValue\r\n{\r\n    PARAM($ReqObj, $Attribute)\r\n    END\r\n    {\r\n        $Changes = ($ReqObj.ResourceManagementObject.ResourceManagementAttributes | where {$_.AttributeName -eq 'RequestParameter'}).Values\r\n\r\n        $ReturnVal = \"\"\r\n        $Xpath = \"\/RequestParameter[PropertyName='\" + $Attribute + \"' and Operation='Create']\/Value\"\r\n        foreach ($change in $Changes)\r\n        {\r\n            $value = (Select-Xml -content $change -Xpath $Xpath).Node.\"#text\"\r\n            if ($value)\r\n            {\r\n                if ($ReturnVal -eq \"\"){$ReturnVal = $value}\r\n                else {$ReturnVal = $ReturnVal + \";\" + $value}\r\n            }\r\n        }\r\n        Return $ReturnVal\r\n    }\r\n}<\/pre>\n<h3>Retrieving Request Objects<\/h3>\n<p>You&#8217;re going to want a pretty precise filter with Export-FIMConfig as there will be\u00c2\u00a0<em>lots<\/em> of Request objects in your Portal. Also make sure you use the <em>OnlyBaseResources<\/em> switch so you don&#8217;t get all the referenced objects as well.<\/p>\n<p>Some examples:<\/p>\n<p><strong>All Requests against a particular Target<\/strong><\/p>\n<pre>\/Request[Target='5efd9c95-05f1-49a4-9c57-38788c7259db']<\/pre>\n<p><strong>All Requests against a particular Target with status of &#8216;Authorizing&#8217;<\/strong><\/p>\n<pre>\/Request[Target='5efd9c95-05f1-49a4-9c57-38788c7259db' and RequestStatus='Authorizing']<\/pre>\n<p><strong>All Requests from a particular Requestor in the last 10 days<\/strong><\/p>\n<pre>\/Request[Creator='5efd9c95-05f1-49a4-9c57-38788c7259db' and CreatedTime &gt;= op:subtract-dayTimeDuration-from-dateTime(fn:current-dateTime(), xs:dayTimeDuration('P10D'))]<\/pre>\n<p><strong>All Requests from a particular Requestor against a particular object type<\/strong><\/p>\n<pre>\/Request[Creator='5efd9c95-05f1-49a4-9c57-38788c7259db' and TargetObjectType='WorkflowDefinition']<\/pre>\n<p><strong>All Requests that involve a particular MPR<\/strong><\/p>\n<pre>\/Request[ManagementPolicy='bae9fd09-e2c7-49f9-b4ae-bc22c9bec6f7']<\/pre>\n<h3>Selecting Requests based on the Attribute being changed<\/h3>\n<p>Unfortunately you can&#8217;t query inside the Request details as part of the XPath filter, so if you&#8217;re after all requests that update a particular attribute you&#8217;re going to have to make your XPath broad enough to catch them all (while not so broad you kill your server trying to execute the request), and then add an extra for loop.<\/p>\n<p>In this example I want to get all the changes to EmployeeEndDate from the Requests I&#8217;ve retrieved.<\/p>\n<pre>$requests = export-fimconfig -customconfig $filter -onlybaseresources\r\n$AttributeName = 'EmployeeEndDate'\r\n\r\nforeach ($request in $requests)\r\n{\r\n    $ReqValue = RequestedValue -ReqObj $request -Attribute $AttributeName\r\n    if ($ReqValue)\r\n    {\r\n        $CreatedTime = ($request.ResourceManagementObject.ResourceManagementAttributes | where {$_.AttributeName -eq 'CreatedTime'}).Value\r\n        $Target = ($request.ResourceManagementObject.ResourceManagementAttributes | where {$_.AttributeName -eq 'Target'}).Value\r\n        $Creator = ($request.ResourceManagementObject.ResourceManagementAttributes | where {$_.AttributeName -eq 'Creator'}).Value\r\n        $Status = ($request.ResourceManagementObject.ResourceManagementAttributes | where {$_.AttributeName -eq 'RequestStatus'}).Value\r\n\r\n        # Get the Creator Object so we can show its DisplayName instead of the ResourceID\r\n        $filter = \"\/Person[ObjectID='\" + $Creator.Replace(\"urn:uuid:\",\"\") + \"']\"\r\n        $TargetObj = export-fimconfig -customconfig $filter -onlybaseresources\r\n        $CreatorName =  ($TargetObj.ResourceManagementObject.ResourceManagementAttributes | where {$_.AttributeName -eq 'DisplayName'}).Value\r\n\r\n        # Get the Target Object so we can show its DisplayName instead of the ResourceID\r\n        $filter = \"\/Person[ObjectID='\" + $Target.Replace(\"urn:uuid:\",\"\") + \"']\"\r\n        $TargetObj = export-fimconfig -customconfig $filter -onlybaseresources\r\n        $TargetName =  ($TargetObj.ResourceManagementObject.ResourceManagementAttributes | where {$_.AttributeName -eq 'DisplayName'}).Value\r\n\r\n        write-host \"Request\" $request.ResourceManagementObject.ObjectIdentifier\r\n        write-host \"`t Requestor:\" $CreatorName\r\n        write-host \"`t Request Time:\" $CreatedTime\r\n        write-host \"`t Target:\" $TargetName\r\n        write-host \"`t Status:\" $Status\r\n        write-host \"`t Attribute:\" $AttributeName\r\n        write-host \"`t Value:\" $ReqValue\r\n        write-host \"`n\"\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been finding it useful to query Request objects for various reasons, mostly to get pending or historical changes out of them. This post contains a few script snippets and examples. Note I have developed and used these on FIM 2010 R2 only.<\/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],"tags":[],"class_list":["post-2475","post","type-post","status-publish","format-standard","hentry","category-fim-2010-r2","category-powershell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pkp1o-DV","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2475","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=2475"}],"version-history":[{"count":5,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2475\/revisions"}],"predecessor-version":[{"id":2480,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2475\/revisions\/2480"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=2475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/categories?post=2475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/tags?post=2475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}