Created
July 2, 2018 19:20
-
-
Save sean-m/ca1f52545dca830aa995ae4eca1b65c2 to your computer and use it in GitHub Desktop.
Code for resolving MIM property references with some caching to optimize network roundtrips.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Modules LithnetRMA | |
Import-Module LithnetRMA | |
Set-ResourceManagementClient -BaseAddress http://mimservice:5725 | |
$default_attr = @("DisplayName","Description","Creator") | |
function GetAttrsByType { | |
<# | |
Resolves list of attributes that should be loaded for a provided type. | |
#> | |
param ([string]$type) | |
begin { | |
if (-not $type_attr_map) { $script:type_attr_map = @{} } | |
} | |
process { | |
if (-not $type) { | |
return $default_attr | |
} | |
if ($type_attr_map.ContainsKey($type)) { | |
return $type_attr_map[$type] | |
} | |
else { | |
$definitions = (Search-Resources -XPath "/$type" -MaxResults 1).PSObject.Properties.Name | ? { $default_attr -notcontains $_ } | ? { $_ -notlike "ObjectID" } | |
if ($definitions) { | |
$type_attr_map.Add($type, $definitions) | |
return GetAttrByType $type | |
} | |
else { | |
return $default_attr | |
} | |
} | |
} | |
} | |
function GetObj { | |
<# | |
Queries for MIM object provided an ObjectID and type (optional) | |
#> | |
param ($type, $objID) | |
if (-not $objID) { | |
Write-Warning "$($MyInvocation.ScriptLineNumber):GetObj: Must pass ObjectID." | |
return | |
} | |
$objID = $objID.ToString().Replace('urn:uuid:','') | |
if (-not $type) { | |
if ($Script:TypeCache -eq $null) { $Script:TypeCache = @{} } | |
if ($Script:TypeCache.ContainsKey($objID)) { $type = $Script:TypeCache[$objID] } | |
else { | |
$_tmp = Search-Resources -XPath "/*[ObjectID='$objID']" | |
$type = $_tmp | select -ExpandProperty ObjectType | |
$Script:TypeCache.Add($objID, $type) | |
} | |
} | |
if ($Script:ObjCache -eq $null) { $Script:ObjCache = @{} } | |
if ($Script:ObjCache.ContainsKey($type)) { | |
if ($Script:ObjCache[$type].ContainsKey($objID)) { | |
$Script:cache_hit++ | |
return $Script:ObjCache[$type][$objID] | |
} | |
} | |
else { | |
$Script:ObjCache.Add($type,@{}) | |
} | |
$Script:cache_miss++ | |
$attrs = GetAttrByType $type | |
$_filter = "/$type[ObjectID = '$objID']" | |
$_export = Search-Resources -XPath $_filter -AttributesToGet $attrs | |
if ($_export -ne $null) { | |
$Script:ObjCache[$type].Add($objID,$_export) | |
return $_export | |
} | |
} | |
filter IsCollection { param ($t) if (-not $t) { return $false } $t.GetType().GetInterface("IEnumerable") -ne $null } | |
function ResolveProperties { | |
<# | |
Enumerates a MIM object's properties and queries for values that are a GUID (reference). | |
* Ignores ObjectID property * | |
#> | |
param ($obj) | |
begin { | |
function ResolveIfNeeded { | |
param ($p) | |
if (-not $p ) { return } | |
if ($p | Get-Member -Name IsGuid) { | |
if ($p.IsGuid) { | |
$value = GetObj -objID ($p.Value.ToString()) | |
$p.Value = $value | |
} | |
} | |
$p | |
} | |
} | |
process { | |
foreach ($p in $obj.PSObject.Properties) { | |
if ($p.Name -like 'ObjectID') { <# Don't need to resolve this #> continue } | |
if (IsCollection ($p.Value)) { | |
foreach ($v in $p.Value) { | |
$v = ResolveIfNeeded $v | |
} | |
} | |
else { | |
$p.Value = ResolveIfNeeded ($p.Value) | |
} | |
} | |
$obj | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment