How to use Powershell to Copy an MPR (en-US)
FIM ScriptBox Item
Summary
This Powershell script is intended to simply the process of copying an MPR - usually in order to duplicate or split out functionality with the fewest possible necessary manual changes needed.
Script Code
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
#-------------------------------------------------------------------------------------------------------------------- #-- #-- CopyMPR #-- #-- Copies a source MPR to a new Target #-- #-------------------------------------------------------------------------------------------------------------------- Param ($srcMPR, $newMPR) $uri = "http://localhost:5725/ResourceManagementService" #-------------------------------------------------------------------------------------------------------------------- Function ExportResources { Param($Filter) $CurObjects = export-fimconfig -uri $URI ` –onlyBaseResources ` -customconfig ($Filter)` -ErrorVariable Err ` -ErrorAction SilentlyContinue If($Err){Throw $Err} Return $CurObjects } #-------------------------------------------------------------------------------------------------------------------- Function ExportSingleResource { Param($Filter) $CurObject = ExportResources -Filter $Filter #If($CurObject -eq $null) {Throw "Resource not found: $Filter"} If(@($CurObject).Count -ne 1) {Throw "Multiple objects found: $Filter"} Return $CurObject } #-------------------------------------------------------------------------------------------------------------------- #-------------------------------------------------------------------------------------------------------------------- #-- Function AddChangeToResource { Param($Resource, $AttributeName, $AttributeValue, $Operation) $ImportChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange $ImportChange.Operation = $Operation $ImportChange.AttributeName = $AttributeName $ImportChange.AttributeValue = $AttributeValue $ImportChange.FullyResolved = 1 $ImportChange.Locale = "Invariant" If($Resource.Changes -eq $null) {$Resource.Changes = (,$ImportChange)} Else {$Resource.Changes += $ImportChange} } #-------------------------------------------------------------------------------------------------------------------- #-------------------------------------------------------------------------------------------------------------------- #-- Function CreateResource { Param($ObjectType, $DisplayName) $NewResource = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportObject $NewResource.ObjectType = $ObjectType $NewResource.SourceObjectIdentifier = [System.Guid]::NewGuid().ToString() AddChangeToResource $newResource "DisplayName" $DisplayName "1" Return $NewResource } #-------------------------------------------------------------------------------------------------------------------- #-------------------------------------------------------------------------------------------------------------------- #-- function SubmitResource { Param($Resource) $Resource | Import-FIMConfig -uri $URI ` -ErrorVariable Err ` -ErrorAction SilentlyContinue If($Err){Throw $Err} } #-------------------------------------------------------------------------------------------------------------------- Function AddChangeToResource { Param($Resource, $AttributeName, $AttributeValue, $Operation) End { $ImportChange = New-Object Microsoft.ResourceManagement.Automation.ObjectModel.ImportChange $ImportChange.Operation = $Operation $ImportChange.AttributeName = $AttributeName $ImportChange.AttributeValue = $AttributeValue $ImportChange.FullyResolved = 1 $ImportChange.Locale = "Invariant" If($Resource.Changes -eq $null) {$Resource.Changes = (,$ImportChange)} Else {$Resource.Changes += $ImportChange} } } #-------------------------------------------------------------------------------------------------------------------- $src_mpr = ExportSingleResource "/ManagementPolicyRule[DisplayName='$srcMPR']" $new_mpr = ExportSingleResource "/ManagementPolicyRule[DisplayName='$newMPR']" if ($src_mpr -eq $null) { Write-Output "Unable to locate Source MPR: '$srcMPR'"; exit } if ($new_mpr -ne $null) { Write-Output "MPR '$newMPR' already exists - unable to create" exit } Write-Output "begin copy" $mpr = CreateResource "ManagementPolicyRule" $newMPR foreach ($rma in $src_mpr.ResourceManagementObject.ResourceManagementAttributes) { $attr = $rma.AttributeName $value = $rma.Value switch ($attr) { "CreatedTime" {} "ObjectId" {} "DisplayName" { AddChangeToResource $mpr "DisplayName" $newMPR "1" } "ActionParameter" { foreach ($mv in $rma.Values) { AddChangeToResource $mpr $attr $mv "0" } } "ActionType" { foreach ($mv in $rma.Values) { AddChangeToResource $mpr $attr $mv "0" } } "PrincipalSet" { $id = $value.Split(":")[2] AddChangeToResource $mpr $attr $id "1" } "ResourceCurrentSet" { $id = $value.Split(":")[2] AddChangeToResource $mpr $attr $id "1" } "ResourceFinalSet" { $id = $value.Split(":")[2] AddChangeToResource $mpr $attr $id "1" } "ActionWorkflowDefinition" { foreach ($mv in $rma.Values) { $id = $mv.Split(":")[2] AddChangeToResource $mpr $attr $id "0" } } "AuthenticationWorkflowDefinition" { foreach ($mv in $rma.Values) { $id = $mv.Split(":")[2] AddChangeToResource $mpr $attr $id "0" } } "AuthorizationWorkflowDefinition" { foreach ($mv in $rma.Values) { $id = $mv.Split(":")[2] AddChangeToResource $mpr $attr $id "0" } } default { AddChangeToResource $mpr $attr $value "1" } } } SubmitResource $mpr |
Note
To provide feedback about this script, create a post on the FIM TechNet Forum.
For more FIM related Windows PowerShell scripts, see the FIM ScriptBox.