<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Time is an illusion. Lunchtime doubly so.</title>
	<atom:link href="http://jtruher3.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jtruher3.wordpress.com</link>
	<description></description>
	<lastBuildDate>Mon, 03 Oct 2011 02:23:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jtruher3.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Time is an illusion. Lunchtime doubly so.</title>
		<link>http://jtruher3.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jtruher3.wordpress.com/osd.xml" title="Time is an illusion. Lunchtime doubly so." />
	<atom:link rel='hub' href='http://jtruher3.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Retrieving projection data with PowerShell</title>
		<link>http://jtruher3.wordpress.com/2011/05/13/retrieving-projection-data-with-criteria/</link>
		<comments>http://jtruher3.wordpress.com/2011/05/13/retrieving-projection-data-with-criteria/#comments</comments>
		<pubDate>Fri, 13 May 2011 23:03:37 +0000</pubDate>
		<dc:creator>jtruher3</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ServiceManager]]></category>

		<guid isPermaLink="false">https://jtruher3.wordpress.com/2011/05/13/retrieving-projection-data-with-criteria/</guid>
		<description><![CDATA[There are a number of cmdlets in the SMLets project (http://smlets.codeplex.com) which retrieve data from Service Manager. To reduce the amount of data in getting simple instances from Service Manager, Get-SCSMObject provides a filter parameter which lets you provide a simple property/operator/value triad to reduce the amount of data that is retrieved from the CMDB. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=66&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are a number of cmdlets in the SMLets project (<a href="http://smlets.codeplex.com">http://smlets.codeplex.com</a>) which retrieve data from Service Manager. To reduce the amount of data in getting simple instances from Service Manager, Get-SCSMObject provides a filter parameter which lets you provide a simple property/operator/value triad to reduce the amount of data that is retrieved from the CMDB. This is really helps performance because the filtering happens on the server. We can see the difference pretty easily:</p>
<pre style="background:#f4f4f4;color:black;">PS<span class="rem"># measure-command { get-scsmobject -Class $incidentclass | ?{ $_.Title -like "Ipsum*" } }</span>
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 460
Ticks             : 14609004
TotalDays         : 1.69085694444444E-05
TotalHours        : 0.000405805666666667
TotalMinutes      : 0.02434834
TotalSeconds      : 1.4609004
TotalMilliseconds : 1460.9004

PS<span class="rem"># measure-command { get-scsmobject -Class $incidentclass -filter { Title -like "Ipsum*" } }</span>
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 134
Ticks             : 1341265
TotalDays         : 1.5523900462963E-06
TotalHours        : 3.72573611111111E-05
TotalMinutes      : 0.00223544166666667
TotalSeconds      : 0.1341265
TotalMilliseconds : 134.1265</pre>
<p>In total time, this might not be impressive, but from a percentage perspective, it is.  In this case, filtering on the server cuts the operation time by 90%, which is pretty substantial. However, simple instances are small potatoes in comparison to what we can save if we implement a filter on projection retrieval.</p>
<p>There is also a filter parameter on Get-SCSMObjectProjection, but it only allows you to filter against properties of the seed object, there&#8217;s no way to query the relationships in this filter. However, since much of the interesting information about a projection is the relationship data, so a simple filter isn&#8217;t as much help as it is for simple instances. Because I wanted to be sure that there was at least <em>some</em> way that you could query against the relationships, I included a criteria parameter which takes an ObjectProjectionCriteria, but left the creation of this criteria as &#8220;an exercise for the reader&#8221;. I&#8217;ve had a few requests for this, so I thought it would be good to build a way to easily create this criteria based on the projection. Behaviorally, I wanted to provide a similar experience to that of the filter&#8217;s property/operator/value trio, so the filter that I created for projections has the same basic shape, but the property part of the trio has a different look.</p>
<p>The property part of the filter is broken into 2 pieces, the relationship (as expressed in the alias) and the property on that relationship. If we look at the System.WorkItem.Incident.View.ProjectionType we see the following structure:</p>
<pre style="background:#f4f4f4;color:black;">PS# get-scsmtypeprojection incident.view.projection

ProjectionType: System.WorkItem.Incident.View.ProjectionType
ProjectionSeed: System.WorkItem.Incident
Components:
   Alias           TargetType        TargetEndPoint
   -----           ----------        ---------------
   AffectedUser    System.User       RequestedWorkItem
   AssignedUser    System.User       AssignedWorkItem</pre>
<p>This projection has two components &#8220;AffectedUser&#8221; and &#8220;AssignedUser&#8221;. With this script, I can construct a filter like this:</p>
<pre style="background:#f4f4f4;color:black;">AssignedUser.DisplayName = <span class="str">'Joe User'</span></pre>
<p>which will check the DisplayName property of the System.User object which is the end point of the relationship. I also wanted to support multiple queries, so I added support for -AND which allows you to create multiple property/operator/value statements.</p>
<p>The savings in retrieving projection data is <em>substantial. </em>Here&#8217;s a query which retrieves incidents which have a priority of 2 and have a related work-item which has a DisplayName which is equal to MA37. Filtering in the query is <strong><span style="color:#ff0000;">200</span></strong> times faster.</p>
<pre style="background:#f4f4f4;color:black;">
PS<span class="rem"># measure-command { </span>
&gt;&gt; .\new-scsmProjectionCriteria.ps1 $ipfull.__Base -<span class="kwrd">filter</span> {
&gt;&gt; priority = 2 -and RelatedWorkItems.DisplayName <span class="preproc">-eq</span> <span class="str">"MA37"</span> } -result
&gt;&gt; }
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 625
Ticks             : 6258242
TotalDays         : 7.24333564814815E-06
TotalHours        : 0.000173840055555556
TotalMinutes      : 0.0104304033333333
TotalSeconds      : <strong>0.6258242</strong>
TotalMilliseconds : 625.8242</pre>
<pre style="background:#f4f4f4;color:black;">PS<span class="rem"># measure-command { Get-SCSMObjectProjection -ProjectionObject $ipfull |</span>
&gt;&gt;  ?{ $_.priority <span class="preproc">-eq</span> <span class="str">"2"</span> -and ($_.RelatesToWorkItem_ |?{$_.DisplayNAme <span class="preproc">-eq</span> <span class="str">"MA37"</span> })} }
Days              : 0
Hours             : 0
Minutes           : 2
Seconds           : 5
Milliseconds      : 888
Ticks             : 1258883302
TotalDays         : 0.0014570408587963
TotalHours        : 0.0349689806111111
TotalMinutes      : 2.09813883666667
TotalSeconds      : <strong>125.8883302</strong>
</pre>
<p>It should be no surprise that it&#8217;s much faster to return only the data that you want, because there&#8217;s so much less information that needs to be passed back from the CMDB. Also, during the first pipeline, the CPU utilization was quite high (ranging between 60-80%) where utilization was split between PowerShell (PowerShell does a lot of adaptation of the returned projection), the SQL server and the DataAccess Service.</p>
<p>Here are some of the filters that I tested against the System.WorkItem.Incident.ProjectionType projection:</p>
<pre style="background:#f4f4f4;color:black;"><span class="str">'title -like "Ipsum*" -and CreatedByUser.DisplayName -like "D*"'</span>
<span class="str">'title -like "Ipsum*" -and RelatedWorkItems.DisplayName -like "M*"'</span>
<span class="str">'title -like "Ipsum*" -and RelatedWorkItems.DisplayName -eq "MA37"'</span>
<span class="str">'RelatedWorkItems.DisplayName -eq "MA37"'</span>
<span class="str">'priority = 2 -and RelatedWorkItems.DisplayName -eq "MA37"'</span>
<span class="str">'priority -gt 1 -and RelatedWorkItems.DisplayName -eq "MA37"'</span>
<span class="str">'priority -lt 3 -and RelatedWorkItems.DisplayName -eq "MA37"'</span>
<span class="str">'priority -le 3 -and RelatedWorkItems.DisplayName -eq "MA37"'</span>
<span class="str">'priority &lt;= 3 -and RelatedWorkItems.DisplayName -eq "MA37"'</span>
<span class="str">'priority -ne 3 -and RelatedWorkItems.DisplayName -eq "MA37"'</span>
<span class="str">'priority -ne 3 -and RelatedWorkItems.DisplayName -notlike "MA3*"'</span>
<span class="str">'priority -eq 3 -and Status -eq "Closed" -and RelatedWorkItems.DisplayName -notlike "MA3*"'</span>
<span class="str">'priority -eq 3 -and AssignedUser.displayname -like "D*" -and Status -eq "Closed" </span></pre>
<pre style="background:#f4f4f4;color:black;"><span class="str">-and RelatedWorkItems.DisplayName -notlike "MA3*"'</span></pre>
<p>Each time, the difference in time between client side and server side filtering is huge!</p>
<p>Here&#8217;s the script:</p>
<div style="background:#f4f4f4;color:black;">
<pre class="alt"><span class="lnum"> 1: </span><span class="rem">###</span>
<span class="lnum"> 2: </span><span class="rem">### filters have the form of:</span></pre>
<pre class="alt"><span class="lnum"> 3: </span><span class="rem">### [alias.]propertyname &lt;operator&gt; value</span>
<span class="lnum"> 4: </span><span class="rem">### if there's no ".", then the assumption is that the </span></pre>
<pre class="alt"><span class="lnum"> 5: </span><span class="rem">### criteria is looking for the property of a seed</span>
<span class="lnum"> 6: </span><span class="rem">### if there is a ".", then it's a property of a relationship</span></pre>
<pre class="alt"><span class="lnum"> 7: </span><span class="rem">### the relationship is described by the alias</span>
<span class="lnum"> 8: </span><span class="rem">### </span></pre>
<pre class="alt"><span class="lnum"> 9: </span>[CmdletBinding()]
<span class="lnum"> 10: </span><span class="kwrd">param</span> (</pre>
<pre class="alt"><span class="lnum"> 11: </span>    [parameter(Mandatory=$true,Position=0)]
<span class="lnum"> 12: </span>    $projection,</pre>
<pre class="alt"><span class="lnum"> 13: </span>    [parameter(Mandatory=$true,Position=1)][string]$<span class="kwrd">filter</span>,
<span class="lnum"> 14: </span>    [parameter()][<span class="kwrd">switch</span>]$results</pre>
<pre class="alt"><span class="lnum"> 15: </span>    )
<span class="lnum"> 16: </span></pre>
<pre class="alt"><span class="lnum"> 17: </span><span class="rem"># determine whether the property is an enumeration type</span>
<span class="lnum"> 18: </span><span class="kwrd">function</span> Test-IsEnum</pre>
<pre class="alt"><span class="lnum"> 19: </span>{
<span class="lnum"> 20: </span>    <span class="kwrd">param</span> ( $property )</pre>
<pre class="alt"><span class="lnum"> 21: </span>    <span class="kwrd">if</span> ( $property.SystemType.Name <span class="preproc">-eq</span> <span class="str">"Enum"</span> ) { <span class="kwrd">return</span> $true }
<span class="lnum"> 22: </span>    <span class="kwrd">return</span> $false</pre>
<pre class="alt"><span class="lnum"> 23: </span>}
<span class="lnum"> 24: </span><span class="rem"># Get the string which provides a reference in our criteria to the</span></pre>
<pre class="alt"><span class="lnum"> 25: </span><span class="rem"># management pack which contains the element we're searching against</span>
<span class="lnum"> 26: </span><span class="kwrd">function</span> Get-ReferenceString</pre>
<pre class="alt"><span class="lnum"> 27: </span>{
<span class="lnum"> 28: </span>    <span class="kwrd">param</span> (</pre>
<pre class="alt"><span class="lnum"> 29: </span>        $ManagementPack,
<span class="lnum"> 30: </span>        [ref]$alias</pre>
<pre class="alt"><span class="lnum"> 31: </span>        )
<span class="lnum"> 32: </span>    $alias.Value = $ManagementPack.Name.Replace(<span class="str">"."</span>,<span class="str">""</span>)</pre>
<pre class="alt"><span class="lnum"> 33: </span>    $refstring = <span class="str">'&lt;Reference Id="{0}" PublicKeyToken="{1}" Version="{2}" Alias="{3}" /&gt;'</span>
<span class="lnum"> 34: </span>    $refstring -f $ManagementPack.Name,$ManagementPack.KeyToken,$ManagementPack.Version,$Alias.Value</pre>
<pre class="alt"><span class="lnum"> 35: </span>}
<span class="lnum"> 36: </span></pre>
<pre class="alt"><span class="lnum"> 37: </span><span class="rem"># retrieve the property from the class</span>
<span class="lnum"> 38: </span><span class="rem"># we want to do this because we may get a property from the user which has the case</span></pre>
<pre class="alt"><span class="lnum"> 39: </span><span class="rem"># incorrect, this allows us to match property names case insensitively</span>
<span class="lnum"> 40: </span><span class="kwrd">function</span> Get-ClassProperty</pre>
<pre class="alt"><span class="lnum"> 41: </span>{
<span class="lnum"> 42: </span>    <span class="kwrd">param</span> ( $Class, $propertyName )</pre>
<pre class="alt"><span class="lnum"> 43: </span>    $property = ($Class.GetProperties(<span class="str">"Recursive"</span>)|?{$_.name <span class="preproc">-eq</span> $propertyName})
<span class="lnum"> 44: </span>    <span class="kwrd">if</span> ( ! $property ) { <span class="kwrd">throw</span> (<span class="str">"no such property '$propertyName' in "</span> + $Class.Name) }</pre>
<pre class="alt"><span class="lnum"> 45: </span>    <span class="kwrd">return</span> $property
<span class="lnum"> 46: </span>}</pre>
<pre class="alt"><span class="lnum"> 47: </span><span class="rem"># in the case that the value that we got is applicable to an enum, look up the</span>
<span class="lnum"> 48: </span><span class="rem"># guid that is needed for the comparison and substitute that guid value</span></pre>
<pre class="alt"><span class="lnum"> 49: </span><span class="rem"># replace the '*' with '%' which is needed by the criteria</span>
<span class="lnum"> 50: </span><span class="kwrd">function</span> Get-ProperValue</pre>
<pre class="alt"><span class="lnum"> 51: </span>{
<span class="lnum"> 52: </span>    <span class="kwrd">param</span> ( $Property, $value )</pre>
<pre class="alt"><span class="lnum"> 53: </span>    <span class="kwrd">if</span> ( Test-IsEnum $property )
<span class="lnum"> 54: </span>    {</pre>
<pre class="alt"><span class="lnum"> 55: </span>        $value = get-scsmenumeration $property.type|?{$_.displayname <span class="preproc">-eq</span> $value}|%{$_.id}
<span class="lnum"> 56: </span>    }</pre>
<pre class="alt"><span class="lnum"> 57: </span>    <span class="kwrd">return</span> $value -replace <span class="str">"\*"</span>,<span class="str">"%"</span>
<span class="lnum"> 58: </span>}</pre>
<pre class="alt"><span class="lnum"> 59: </span><span class="rem"># create the XML expression which describes the criteria</span>
<span class="lnum"> 60: </span><span class="kwrd">function</span> Get-Expression</pre>
<pre class="alt"><span class="lnum"> 61: </span>{
<span class="lnum"> 62: </span>    <span class="kwrd">param</span> (</pre>
<pre class="alt"><span class="lnum"> 63: </span>        $TypeProjection,
<span class="lnum"> 64: </span>        [Hashtable]$POV,</pre>
<pre class="alt"><span class="lnum"> 65: </span>        [ref]$neededReferences
<span class="lnum"> 66: </span>        )</pre>
<pre class="alt"><span class="lnum"> 67: </span>    $Property = $POV.Property
<span class="lnum"> 68: </span>    $Operator = $POV.Operator</pre>
<pre class="alt"><span class="lnum"> 69: </span>    $Value    = $POV.Value
<span class="lnum"> 70: </span>    $ExpressionXML = @<span class="str">'</span></pre>
<pre class="alt"><span class="lnum"> 71: </span>        &lt;Expression&gt;
<span class="lnum"> 72: </span>         &lt;SimpleExpression&gt;</pre>
<pre class="alt"><span class="lnum"> 73: </span>          &lt;ValueExpressionLeft&gt;&lt;Property&gt;{0}&lt;/Property&gt;&lt;/ValueExpressionLeft&gt;
<span class="lnum"> 74: </span>          &lt;Operator&gt;{1}&lt;/Operator&gt;</pre>
<pre class="alt"><span class="lnum"> 75: </span>          &lt;ValueExpressionRight&gt;&lt;Value&gt;{2}&lt;/Value&gt;&lt;/ValueExpressionRight&gt;
<span class="lnum"> 76: </span>         &lt;/SimpleExpression&gt;</pre>
<pre class="alt"><span class="lnum"> 77: </span>        &lt;/Expression&gt;
<span class="lnum"> 78: </span>'@</pre>
<pre class="alt"><span class="lnum"> 79: </span>    [ref]$MPAlias = $null
<span class="lnum"> 80: </span></pre>
<pre class="alt"><span class="lnum"> 81: </span>    <span class="rem"># a proper property reference in a projection criteria looks like this:</span>
<span class="lnum"> 82: </span>    <span class="rem"># &lt;Property&gt;</span></pre>
<pre class="alt"><span class="lnum"> 83: </span>    <span class="rem"># $Context/Path[Relationship='CustomSystem_WorkItem_Library!System.WorkItemAffectedUser' </span>
<span class="lnum"> 84: </span>    <span class="rem"># TypeConstraint='CustomSystem_Library!System.User']/</span></pre>
<pre class="alt"><span class="lnum"> 85: </span>    <span class="rem"># Property[Type='CustomSystem_Library!System.User']/FirstName$</span>
<span class="lnum"> 86: </span>    <span class="rem"># &lt;/Property&gt;</span></pre>
<pre class="alt"><span class="lnum"> 87: </span>    <span class="rem"># we need to collect all the bits and do the same</span>
<span class="lnum"> 88: </span>    <span class="rem"># if the property has a "." in it, we will assume that this is the property</span></pre>
<pre class="alt"><span class="lnum"> 89: </span>    <span class="rem"># of a relationship. Therefore, get the relationship and construct the </span>
<span class="lnum"> 90: </span>    <span class="rem"># appropriate string for the property access</span></pre>
<pre class="alt"><span class="lnum"> 91: </span>    <span class="rem">#</span>
<span class="lnum"> 92: </span>    <span class="rem"># This routine only supports a single ".", anything more complicated and this will </span></pre>
<pre class="alt"><span class="lnum"> 93: </span>    <span class="rem"># fail</span>
<span class="lnum"> 94: </span>    <span class="kwrd">if</span> ( $property <span class="preproc">-match</span> <span class="str">"\."</span> )</pre>
<pre class="alt"><span class="lnum"> 95: </span>    {
<span class="lnum"> 96: </span>        $alias,$prop = $property -split <span class="str">"\."</span></pre>
<pre class="alt"><span class="lnum"> 97: </span>        $component = $projection.TypeProjection[$alias]
<span class="lnum"> 98: </span>        $references = @()</pre>
<pre class="alt"><span class="lnum"> 99: </span>        $NS = <span class="str">"Microsoft.EnterpriseManagement"</span>
<span class="lnum"> 100: </span>        $ConfigNS = <span class="str">"${NS}.Configuration"</span></pre>
<pre class="alt"><span class="lnum"> 101: </span>        $ComponentType = <span class="str">"${ConfigNS}.ManagementPackTypeProjectionComponent"</span>
<span class="lnum"> 102: </span>        <span class="kwrd">if</span> ( $component -isnot $ComponentType)</pre>
<pre class="alt"><span class="lnum"> 103: </span>        {
<span class="lnum"> 104: </span>            <span class="kwrd">throw</span> <span class="str">"'$alias' not found on projection"</span></pre>
<pre class="alt"><span class="lnum"> 105: </span>        }
<span class="lnum"> 106: </span>        $target = $component.TargetType</pre>
<pre class="alt"><span class="lnum"> 107: </span>        $references += Get-ReferenceString $target.GetManagementPack() $MPAlias
<span class="lnum"> 108: </span>        $TargetFQN = <span class="str">"{0}!{1}"</span> -f $MPAlias.Value,$Target.Name</pre>
<pre class="alt"><span class="lnum"> 109: </span>        $property = Get-ClassProperty $target $prop
<span class="lnum"> 110: </span>        $value = Get-ProperValue $property $value</pre>
<pre class="alt"><span class="lnum"> 111: </span>
<span class="lnum"> 112: </span>        $relationship = $component.Relationship</pre>
<pre class="alt"><span class="lnum"> 113: </span>        $references += Get-ReferenceString $relationship.GetManagementPack() $MPAlias
<span class="lnum"> 114: </span>        $relationshipFQN = <span class="str">"{0}!{1}"</span> -f $MPAlias.Value,$relationship.name</pre>
<pre class="alt"><span class="lnum"> 115: </span> 
<span class="lnum"> 116: </span>        $PropString = <span class="str">'$Context/Path[Relationship='</span><span class="str">'{0}'</span><span class="str">' TypeConstraint='</span><span class="str">'{1}'</span><span class="str">']/Property[Type='</span><span class="str">'{1}'</span><span class="str">']/{2}$'</span></pre>
<pre class="alt"><span class="lnum"> 117: </span>        $XPATHSTR = $PropString -f $RelationshipFQN,$TargetFQN,$property.Name
<span class="lnum"> 118: </span></pre>
<pre class="alt"><span class="lnum"> 119: </span>        $Expression = $ExpressionXML -f $XPATHSTR,$QueryOperator,$value
<span class="lnum"> 120: </span>        $neededReferences.Value = $references | sort-object -uniq</pre>
<pre class="alt"><span class="lnum"> 121: </span>        <span class="kwrd">return</span> $Expression
<span class="lnum"> 122: </span>    }</pre>
<pre class="alt"><span class="lnum"> 123: </span>    <span class="kwrd">else</span>
<span class="lnum"> 124: </span>    {</pre>
<pre class="alt"><span class="lnum"> 125: </span>        $SeedClass = get-scsmclass -id $projection.TargetType.Id
<span class="lnum"> 126: </span>        $property = Get-ClassProperty $SeedClass $property</pre>
<pre class="alt"><span class="lnum"> 127: </span>        $value = Get-ProperValue $Property $value
<span class="lnum"> 128: </span></pre>
<pre class="alt"><span class="lnum"> 129: </span>        $SeedMP = $SeedClass.GetManagementPack()
<span class="lnum"> 130: </span>        $reference = Get-ReferenceString $SeedMP $MPAlias</pre>
<pre class="alt"><span class="lnum"> 131: </span>        $typeFQN = <span class="str">"{0}!{1}"</span> -f $MPAlias.Value,$SeedClass.Name
<span class="lnum"> 132: </span></pre>
<pre class="alt"><span class="lnum"> 133: </span>        $PropString = <span class="str">'$Context/Property[Type='</span><span class="str">'{0}'</span><span class="str">']/{1}$'</span> -f $typeFQN,$Property.Name
<span class="lnum"> 134: </span>        $Expression = $ExpressionXML -f $PropString,$Operator,$Value</pre>
<pre class="alt"><span class="lnum"> 135: </span>        $neededReferences.Value = $reference
<span class="lnum"> 136: </span>        <span class="kwrd">return</span> $Expression</pre>
<pre class="alt"><span class="lnum"> 137: </span>    }
<span class="lnum"> 138: </span>}</pre>
<pre class="alt"><span class="lnum"> 139: </span> 
<span class="lnum"> 140: </span><span class="kwrd">trap</span> { $error[0];exit }</pre>
<pre class="alt"><span class="lnum"> 141: </span><span class="kwrd">if</span> ( $projection <span class="preproc">-is</span> <span class="str">"psobject"</span>  )
<span class="lnum"> 142: </span>{</pre>
<pre class="alt"><span class="lnum"> 143: </span>    $projection = $projection.__base
<span class="lnum"> 144: </span>}</pre>
<pre class="alt"><span class="lnum"> 145: </span>$ProjectionType = <span class="str">"Microsoft.EnterpriseManagement.Configuration.ManagementPackTypeProjection"</span>
<span class="lnum"> 146: </span><span class="kwrd">if</span> ( $projection -isnot $ProjectionType )</pre>
<pre class="alt"><span class="lnum"> 147: </span>{
<span class="lnum"> 148: </span>    <span class="kwrd">throw</span> <span class="str">"$projection is not a projection and cannot be converted"</span></pre>
<pre class="alt"><span class="lnum"> 149: </span>}
<span class="lnum"> 150: </span><span class="rem"># right now, only AND is supported,</span></pre>
<pre class="alt"><span class="lnum"> 151: </span><span class="rem"># eventually, OR will be supported</span>
<span class="lnum"> 152: </span>$GroupOperators = <span class="str">" -and "</span></pre>
<pre class="alt"><span class="lnum"> 153: </span><span class="rem"># and the conversion to what is needed in the criteria</span>
<span class="lnum"> 154: </span>$OperatorConverter = @{</pre>
<pre class="alt"><span class="lnum"> 155: </span>    <span class="str">"="</span>     = <span class="str">"Equal"</span>
<span class="lnum"> 156: </span>    <span class="str">"-eq"</span>   = <span class="str">"Equal"</span></pre>
<pre class="alt"><span class="lnum"> 157: </span>    <span class="str">"!="</span>    = <span class="str">"NotEqual"</span>
<span class="lnum"> 158: </span>    <span class="str">"-ne"</span>   = <span class="str">"NotEqual"</span></pre>
<pre class="alt"><span class="lnum"> 159: </span>    <span class="str">"-like"</span> = <span class="str">"Like"</span>
<span class="lnum"> 160: </span>    <span class="str">"-notlike"</span> = <span class="str">"NotLike"</span></pre>
<pre class="alt"><span class="lnum"> 161: </span>    <span class="str">"&lt;"</span>     = <span class="str">"Less"</span>
<span class="lnum"> 162: </span>    <span class="str">"-lt"</span>   = <span class="str">"Less"</span></pre>
<pre class="alt"><span class="lnum"> 163: </span>    <span class="str">"&gt;"</span>     = <span class="str">"Greater"</span>
<span class="lnum"> 164: </span>    <span class="str">"-gt"</span>   = <span class="str">"Greater"</span></pre>
<pre class="alt"><span class="lnum"> 165: </span>    <span class="str">"&gt;="</span>    = <span class="str">"GreaterEqual"</span>
<span class="lnum"> 166: </span>    <span class="str">"-ge"</span>   = <span class="str">"GreaterEqual"</span></pre>
<pre class="alt"><span class="lnum"> 167: </span>    <span class="str">"&lt;="</span>    = <span class="str">"LessEqual"</span>
<span class="lnum"> 168: </span>    <span class="str">"-le"</span>   = <span class="str">"LessEqual"</span></pre>
<pre class="alt"><span class="lnum"> 169: </span>    }
<span class="lnum"> 170: </span><span class="rem"># a list of allowed operators, generated from the converter</span></pre>
<pre class="alt"><span class="lnum"> 171: </span>$Operators = ($OperatorConverter.Keys |%{<span class="str">" $_ "</span>}) -join <span class="str">"|"</span>
<span class="lnum"> 172: </span><span class="rem"># split the filter up based on the GroupOperator</span></pre>
<pre class="alt"><span class="lnum"> 173: </span>$filters = @($<span class="kwrd">filter</span>.ToString() -split $GroupOperators | %{$_.trim()})
<span class="lnum"> 174: </span><span class="rem"># some variables that we will need </span></pre>
<pre class="alt"><span class="lnum"> 175: </span>[ref]$neededrefs = $null
<span class="lnum"> 176: </span>$Expressions = @()</pre>
<pre class="alt"><span class="lnum"> 177: </span>$ReferenceStrings = @()
<span class="lnum"> 178: </span><span class="rem"># loop through the filters and construct some XML which we will use</span></pre>
<pre class="alt"><span class="lnum"> 179: </span><span class="kwrd">foreach</span> ( $filterString <span class="kwrd">in</span> $filters)
<span class="lnum"> 180: </span>{</pre>
<pre class="alt"><span class="lnum"> 181: </span>    <span class="rem"># check to be sure we have a valid filter which includes</span>
<span class="lnum"> 182: </span>    <span class="rem"># a property, an operator and a value</span></pre>
<pre class="alt"><span class="lnum"> 183: </span>    $foundMatch = $filterString.toString() <span class="preproc">-match</span> <span class="str">"(?&lt;p&gt;.*)(?&lt;o&gt;$operators)(?&lt;v&gt;.*)"</span>
<span class="lnum"> 184: </span>    <span class="kwrd">if</span> ( ! $foundMatch )</pre>
<pre class="alt"><span class="lnum"> 185: </span>    {
<span class="lnum"> 186: </span>        <span class="kwrd">throw</span> <span class="str">"bad filter $filter"</span></pre>
<pre class="alt"><span class="lnum"> 187: </span>    }
<span class="lnum"> 188: </span>    <span class="rem"># manipulate the found elements into a PropertyOperatorValue hashtable</span></pre>
<pre class="alt"><span class="lnum"> 189: </span>    <span class="rem"># which we will use to encapsulate the filter</span>
<span class="lnum"> 190: </span>    $Property = $matches[<span class="str">'p'</span>].Trim()</pre>
<pre class="alt"><span class="lnum"> 191: </span>    $Operator = $matches[<span class="str">'o'</span>].Trim()
<span class="lnum"> 192: </span>    $QueryOperator = $OperatorConverter[$Operator]</pre>
<pre class="alt"><span class="lnum"> 193: </span>    <span class="kwrd">if</span> ( ! $Operator ) { <span class="kwrd">throw</span> <span class="str">"Bad Operator '$Operator'"</span> }
<span class="lnum"> 194: </span>    $Value    = $matches[<span class="str">'v'</span>].Trim() -replace <span class="str">'"'</span> -replace <span class="str">"'"</span></pre>
<pre class="alt"><span class="lnum"> 195: </span>    $POV = @{
<span class="lnum"> 196: </span>        Property = $Property</pre>
<pre class="alt"><span class="lnum"> 197: </span>        Operator = $QueryOperator
<span class="lnum"> 198: </span>        Value    = $Value</pre>
<pre class="alt"><span class="lnum"> 199: </span>        }
<span class="lnum"> 200: </span>    <span class="rem"># now go get the expression that we need for the criteria</span></pre>
<pre class="alt"><span class="lnum"> 201: </span>    <span class="rem"># pass the projection, the PropertyOperatorValue hashtable</span>
<span class="lnum"> 202: </span>    <span class="rem"># and the needed references (as a reference variable }</span></pre>
<pre class="alt"><span class="lnum"> 203: </span>    $expressions += get-expression $projection $POV $neededrefs
<span class="lnum"> 204: </span>    $neededRefs.Value | %{ $ReferenceStrings += $_ }</pre>
<pre class="alt"><span class="lnum"> 205: </span>}
<span class="lnum"> 206: </span><span class="rem"># now that we have looped through the filters, construct the XML</span></pre>
<pre class="alt"><span class="lnum"> 207: </span><span class="rem"># which we need to call the ObjectProjectCriteria constructor</span>
<span class="lnum"> 208: </span><span class="rem"># start off with the start of the criteria XML</span></pre>
<pre class="alt"><span class="lnum"> 209: </span>$CriteriaString = <span class="str">'&lt;Criteria xmlns="http://Microsoft.EnterpriseManagement.Core.Criteria/"&gt;'</span>
<span class="lnum"> 210: </span><span class="rem"># now add the references that are needed in the criteria</span></pre>
<pre class="alt"><span class="lnum"> 211: </span>$ReferenceStrings | sort -uniq | %{ $CriteriaString += <span class="str">"`n $_"</span> }
<span class="lnum"> 212: </span><span class="rem"># if we actually had multiple filters, add the </span></pre>
<pre class="alt"><span class="lnum"> 213: </span><span class="rem"># &lt;And&gt;</span>
<span class="lnum"> 214: </span><span class="kwrd">if</span> ( $Filters.count <span class="preproc">-gt</span> 1 )</pre>
<pre class="alt"><span class="lnum"> 215: </span>{
<span class="lnum"> 216: </span>    $CriteriaString += <span class="str">"`n&lt;Expression&gt;"</span></pre>
<pre class="alt"><span class="lnum"> 217: </span>    $CriteriaString += <span class="str">"`n &lt;And&gt;"</span>
<span class="lnum"> 218: </span>}</pre>
<pre class="alt"><span class="lnum"> 219: </span><span class="rem"># now, for each of the expressions, add it to the criteria string</span>
<span class="lnum"> 220: </span><span class="kwrd">foreach</span>($ex <span class="kwrd">in</span> $expressions ) { $CriteriaString += <span class="str">"`n $ex"</span> }</pre>
<pre class="alt"><span class="lnum"> 221: </span><span class="rem"># and in the case where we have filters that have and "-and", add the</span>
<span class="lnum"> 222: </span><span class="rem"># &lt;/And&gt; to finish correctly</span></pre>
<pre class="alt"><span class="lnum"> 223: </span><span class="kwrd">if</span> ( $Filters.Count <span class="preproc">-gt</span> 1)
<span class="lnum"> 224: </span>{</pre>
<pre class="alt"><span class="lnum"> 225: </span>    $CriteriaString += <span class="str">"`n &lt;/And&gt;"</span>
<span class="lnum"> 226: </span>    $CriteriaString += <span class="str">"`n&lt;/Expression&gt;"</span></pre>
<pre class="alt"><span class="lnum"> 227: </span>}
<span class="lnum"> 228: </span>$CriteriaString += <span class="str">"`n&lt;/Criteria&gt;"</span></pre>
<pre class="alt"><span class="lnum"> 229: </span>write-verbose $CriteriaString
<span class="lnum"> 230: </span><span class="rem"># at this stage, the criteria XML should be complete, so we can create the</span></pre>
<pre class="alt"><span class="lnum"> 231: </span><span class="rem"># criteria object</span>
<span class="lnum"> 232: </span>$CTYPE = <span class="str">"Microsoft.EnterpriseManagement.Common.ObjectProjectionCriteria"</span></pre>
<pre class="alt"><span class="lnum"> 233: </span> 
<span class="lnum"> 234: </span>$criteriaobject = new-object $CTYPE $CriteriaString,$projection,$projection.ManagementGroup</pre>
<pre class="alt"><span class="lnum"> 235: </span><span class="kwrd">if</span> ( $criteriaObject -and $Results )
<span class="lnum"> 236: </span>{</pre>
<pre class="alt"><span class="lnum"> 237: </span>    get-scsmobjectprojection -criteria $criteriaobject
<span class="lnum"> 238: </span>}</pre>
<pre class="alt"><span class="lnum"> 239: </span><span class="kwrd">elseif</span> ( $criteriaObject )
<span class="lnum"> 240: </span>{</pre>
<pre class="alt"><span class="lnum"> 241: </span>    $criteriaObject
<span class="lnum"> 242: </span>}</pre>
<pre class="alt"><span class="lnum"> 243: </span></pre>
</div>
<p>I added a Result parameter to the script which calls Get-SCSMObjectProjection, just for convenience. Eventually, I&#8217;ll add this logic into the filter parameter for the cmdlet, so it will be part of the cmdlet rather than this addition.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jtruher3.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jtruher3.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jtruher3.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jtruher3.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jtruher3.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jtruher3.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jtruher3.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jtruher3.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jtruher3.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jtruher3.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jtruher3.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jtruher3.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jtruher3.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jtruher3.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=66&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jtruher3.wordpress.com/2011/05/13/retrieving-projection-data-with-criteria/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/20349e038c0f35188b594c68f20636bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jtruher3</media:title>
		</media:content>
	</item>
		<item>
		<title>A tool for table formatting</title>
		<link>http://jtruher3.wordpress.com/2011/04/19/a-tool-for-table-formatting/</link>
		<comments>http://jtruher3.wordpress.com/2011/04/19/a-tool-for-table-formatting/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 23:18:01 +0000</pubDate>
		<dc:creator>jtruher3</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">https://jtruher3.wordpress.com/?p=57</guid>
		<description><![CDATA[As I was working on the SMLets CodePlex project, I created a bunch of new cmdlets and I wanted to make sure that the output looked good and was useful. In order to do that I needed to sling a bunch of XML and create my own formatting file. If you’ve ever looked at the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=57&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As I was working on the <a href="http://smlets.codeplex.com/">SMLets CodePlex</a> project, I created a bunch of new cmdlets and I wanted to make sure that the output looked good and was useful. In order to do that I needed to sling a bunch of XML and create my own formatting file. If you’ve ever looked at the format files in PowerShell (to see what’s shipped, type get-childitem “$pshome/*.format.ps1xml&#8221;) it can be pretty daunting, so most folks (me included) start with something that works an then modify it. After I did that a few times, I reckoned that I had better build a tool to make life easier. My requirements were pretty straight-forward; build a tool that emitted the XML that I needed to put in the formatting file. Second, commit no unnatural acts with regard to parameters to create the XML. What I really wanted, is to use Format-Table to get the output exactly how I wanted and then just substitute my tool for Format-Table. Thus, New-TableFormat is born.</p>
<p>The following is how I use it, for this example, I’m just using a process object in this example, but the concept applies to any object. First I get the output looking exactly like I want, so in this case, I show the processID, the handles, the name and then the handles in KB. In order to do that last bit, I need to use a script block. After that, it’s simply replace format-table with new-tableformat and hey! presto! I’ve got my XML!</p>
<pre class="csharpcode" style="width:134.36%;height:592px;">PS&gt; get-<span class="kwrd">process</span> lsass|format-table id,handles,name,@{L=<span class="str">"HandlesKB"</span>;E={$_.Handles/1KB};A=<span class="str">"Right"</span>;F=<span class="str">"{0:N2}"</span>} -au

 Id Handles Name  HandlesKB
 -- ------- ----  ---------
552    1274 lsass      1.24

PS&gt; get-<span class="kwrd">process</span> lsass|new-tableformat id,handles,name,@{L=<span class="str">"HandlesKB"</span>;E={$_.Handles/1KB};A=<span class="str">"Right"</span>;F=<span class="str">"{0:N2}"</span>} -au
   &lt;View&gt;
    &lt;Name&gt;ProcessTable&lt;/Name&gt;
    &lt;ViewSelectedBy&gt;
     &lt;TypeName&gt;System.Diagnostics.Process&lt;/TypeName&gt;
    &lt;/ViewSelectedBy&gt;
    &lt;TableControl&gt;
     &lt;AutoSize /&gt;
     &lt;TableHeaders&gt;
      &lt;TableColumnHeader&gt;&lt;Label&gt;id&lt;/Label&gt;&lt;/TableColumnHeader&gt;
      &lt;TableColumnHeader&gt;&lt;Label&gt;handles&lt;/Label&gt;&lt;/TableColumnHeader&gt;
      &lt;TableColumnHeader&gt;&lt;Label&gt;name&lt;/Label&gt;&lt;/TableColumnHeader&gt;
      &lt;TableColumnHeader&gt;
        &lt;Label&gt;HandlesKB&lt;/Label&gt;
        &lt;Alignment&gt;Right&lt;/Alignment&gt;
      &lt;/TableColumnHeader&gt;
     &lt;/TableHeaders&gt;
     &lt;TableRowEntries&gt;
      &lt;TableRowEntry&gt;
       &lt;TableColumnItems&gt;
        &lt;TableColumnItem&gt;&lt;PropertyName&gt;id&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;
        &lt;TableColumnItem&gt;&lt;PropertyName&gt;handles&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;
        &lt;TableColumnItem&gt;&lt;PropertyName&gt;name&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;
        &lt;TableColumnItem&gt;&lt;ScriptBlock&gt;$_.Handles/1KB&lt;/ScriptBlock&gt;&lt;FormatString&gt;{0:N2}&lt;/FormatString&gt;&lt;/TableColumnItem&gt;
       &lt;/TableColumnItems&gt;
      &lt;/TableRowEntry&gt;
     &lt;/TableRowEntries&gt;
    &lt;/TableControl&gt;
   &lt;/View&gt;</pre>
<p>In order for me to take advantage of this, I need to save it to a file and then call update-formatdata on the new file. You might notice that this is not quite complete as it doesn’t have the required elements for the beginning and end of the file, so I’ve got another parameter –complete which emits complete, standalone XML which I can dump into a file and then import. Once imported I can just use format-table as usual (in this case since there’s already a table format for process objects, I need to include the view name, which is “ProcessTable”, in this case. If you’re building formatting for an object of your own creation or doesn’t already exist, this last step is not necessary. Just output the object and the formatter takes care of everything automatically.</p>
<pre class="csharpcode" style="width:139.75%;height:146px;">PS&gt; get-<span class="kwrd">process</span> lsass|new-tableformat id,handles,name,@{L=<span class="str">"HandlesKB"</span>;E={$_.Handles/1KB};A=<span class="str">"Right"</span>;F=<span class="str">"{0:N2}"</span>} -au -comp
 &gt; handleKB.format.ps1xml
PS&gt; update-formatdata handleKB.format.ps1xml
PS&gt; get-<span class="kwrd">process</span> lsass | format-table -view ProcessTable

id  handles name  HandlesKB
--  ------- ----  ---------
552 1263    lsass      1.23</pre>
<p>Here’s the script, it’s only 125 lines or so!</p>
<pre class="csharpcode" style="width:133.71%;height:2046px;"><span class="kwrd">param</span> (
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]$object,
    [Parameter(Mandatory=$true,Position=0)][object[]]$property,
    [Parameter()][<span class="kwrd">switch</span>]$Complete,
    [Parameter()][<span class="kwrd">switch</span>]$auto,
    [Parameter()][string]$name,
    [Parameter()][<span class="kwrd">switch</span>]$force
    )
End
{
<span class="kwrd">if</span> ( $object )
{
    <span class="kwrd">if</span> ( $object <span class="preproc">-is</span> <span class="str">"PSObject"</span>)
    {
        $TN = $object.psobject.typenames[0]
    }
    <span class="kwrd">else</span>
    {
        $TN = $object.gettype().fullname
    }
}
<span class="kwrd">elseif</span> ( $name )
{
    $TN = $name
}
$NAME = $TN.split(<span class="str">"."</span>)[-1]
$sb = new-object System.Text.StringBuilder
<span class="kwrd">if</span> ( $complete )
{
    [void]$sb.Append(<span class="str">"&lt;Configuration&gt;`n"</span>)
    [void]$sb.Append(<span class="str">" &lt;ViewDefinitions&gt;`n"</span>)
}
[void]$sb.Append(<span class="str">" &lt;View&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;Name&gt;${Name}Table&lt;/Name&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;ViewSelectedBy&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;TypeName&gt;${TN}&lt;/TypeName&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;/ViewSelectedBy&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;TableControl&gt;`n"</span>)
<span class="kwrd">if</span> ( $auto )
{
    [void]$sb.Append(<span class="str">" &lt;AutoSize /&gt;`n"</span>)
}
[void]$sb.Append(<span class="str">" &lt;TableHeaders&gt;`n"</span>)
<span class="rem"># </span>
<span class="rem"># Now loop through the properties, creating a header for each </span>
<span class="rem"># provided property</span>
<span class="rem">#</span>
<span class="kwrd">foreach</span>($p <span class="kwrd">in</span> $property)
{
    <span class="kwrd">if</span> ( $p <span class="preproc">-is</span> <span class="str">"string"</span> )
    {
        [void]$sb.Append(<span class="str">" &lt;TableColumnHeader&gt;&lt;Label&gt;${p}&lt;/Label&gt;&lt;/TableColumnHeader&gt;`n"</span>)
    }
    <span class="kwrd">elseif</span> ( $p <span class="preproc">-is</span> <span class="str">"hashtable"</span> )
    {
        $Label = $p.keys | ?{$_ <span class="preproc">-match</span> <span class="str">"^L|^N"</span> }
        <span class="kwrd">if</span> ( ! $Label )
        {
            <span class="kwrd">throw</span> <span class="str">"need Name or Label Key"</span>
        }
        [void]$sb.Append(<span class="str">" &lt;TableColumnHeader&gt;`n"</span>)
        [void]$sb.Append(<span class="str">" &lt;Label&gt;"</span> + $p.$label + <span class="str">"&lt;/Label&gt;`n"</span>)
        $Width = $p.Keys |?{$_ <span class="preproc">-match</span> <span class="str">"^W"</span>}|select -first 1
        <span class="kwrd">if</span> ( $Width )
        {
            [void]$sb.Append(<span class="str">" &lt;Width&gt;"</span> + $p.$Width + <span class="str">"&lt;/Width&gt;`n"</span>)
        }
        $Align = $p.Keys |?{$_ <span class="preproc">-match</span> <span class="str">"^A"</span>}|select -first 1
        <span class="kwrd">if</span> ( $Align )
        {
            [void]$sb.Append(<span class="str">" &lt;Alignment&gt;"</span> + $p.$align + <span class="str">"&lt;/Alignment&gt;`n"</span>)
        }
        [void]$sb.Append(<span class="str">" &lt;/TableColumnHeader&gt;`n"</span>)
        <span class="rem"># write-host -for red ("skipping " + $p.Name + " for now")</span>
    }
}
[void]$sb.Append(<span class="str">" &lt;/TableHeaders&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;TableRowEntries&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;TableRowEntry&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;TableColumnItems&gt;`n"</span>)
<span class="kwrd">foreach</span>($p <span class="kwrd">in</span> $property)
{
    <span class="kwrd">if</span> ( $p <span class="preproc">-is</span> <span class="str">"string"</span> )
    {
        [void]$sb.Append(<span class="str">" &lt;TableColumnItem&gt;&lt;PropertyName&gt;${p}&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;`n"</span>)
    }
    <span class="kwrd">elseif</span> ( $p <span class="preproc">-is</span> <span class="str">"hashtable"</span> )
    {
        [void]$sb.Append(<span class="str">" &lt;TableColumnItem&gt;"</span>)
        $Name = $p.Keys | ?{ $_ <span class="preproc">-match</span> <span class="str">"^N"</span> }|select -first 1
        <span class="kwrd">if</span> ( $Name )
        {
            $v = $p.$Name
            [void]$sb.Append(<span class="str">"&lt;PropertyName&gt;$v&lt;/PropertyName&gt;"</span>)
        }
        $Expression = $p.Keys | ?{ $_ <span class="preproc">-match</span> <span class="str">"^E"</span> }|select -first 1
        <span class="kwrd">if</span> ( $Expression )
        {
            $v = $p.$Expression
            [void]$sb.Append(<span class="str">"&lt;ScriptBlock&gt;$v&lt;/ScriptBlock&gt;"</span>)
        }
        $Format = $p.Keys | ?{$_ <span class="preproc">-match</span> <span class="str">"^F"</span> }|select -first 1
        <span class="kwrd">if</span> ( $Format )
        {
            $v = $p.$Format
            [void]$sb.Append(<span class="str">"&lt;FormatString&gt;$v&lt;/FormatString&gt;"</span>)
        }
        [void]$Sb.Append(<span class="str">"&lt;/TableColumnItem&gt;`n"</span>)
    }
}
[void]$sb.Append(<span class="str">" &lt;/TableColumnItems&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;/TableRowEntry&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;/TableRowEntries&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;/TableControl&gt;`n"</span>)
[void]$sb.Append(<span class="str">" &lt;/View&gt;`n"</span>)
<span class="kwrd">if</span> ( $complete )
{
    [void]$sb.Append(<span class="str">" &lt;/ViewDefinitions&gt;`n"</span>)
    [void]$sb.Append(<span class="str">"&lt;/Configuration&gt;`n"</span>)
}
$sb.ToString()
}</pre>
<p>the thing to note here is that I’m just building the XML string and then emitting it, I’m not using all the XML programming goo, which would probably be better practice, but I didn’t want to be bothered (clearly, that could be a future enhancement). You’ll notice that it handles pretty much everything that format-table does.</p>
<p>I hope this is as useful for you as it is for me</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jtruher3.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jtruher3.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jtruher3.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jtruher3.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jtruher3.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jtruher3.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jtruher3.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jtruher3.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jtruher3.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jtruher3.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jtruher3.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jtruher3.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jtruher3.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jtruher3.wordpress.com/57/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=57&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jtruher3.wordpress.com/2011/04/19/a-tool-for-table-formatting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/20349e038c0f35188b594c68f20636bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jtruher3</media:title>
		</media:content>
	</item>
		<item>
		<title>missing in action</title>
		<link>http://jtruher3.wordpress.com/2011/04/19/missing-in-action/</link>
		<comments>http://jtruher3.wordpress.com/2011/04/19/missing-in-action/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 22:27:46 +0000</pubDate>
		<dc:creator>jtruher3</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ServiceManager]]></category>

		<guid isPermaLink="false">http://jtruher3.wordpress.com/?p=50</guid>
		<description><![CDATA[Clearly i haven&#8217;t posted for a long while, and while there are many reasons which I don&#8217;t need to go into, it&#8217;s not because I&#8217;ve been slacking. Most recently, I&#8217;ve been working on a codeplex project (http://smlets.codeplex.com) which uses PowerShell to interact with Service Manager (i&#8217;ve been working on Service Manager for a while). The project has been great [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=50&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Clearly i haven&#8217;t posted for a long while, and while there are many reasons which I don&#8217;t need to go into, it&#8217;s not because I&#8217;ve been slacking. Most recently, I&#8217;ve been working on a codeplex project (<a href="http://smlets.codeplex.com">http://smlets.codeplex.com</a>) which uses PowerShell to interact with <a title="Service Manager" href="http://www.microsoft.com/systemcenter/en/us/service-manager.aspx" target="_blank">Service Manager</a> (i&#8217;ve been working on Service Manager for a while). The project has been great fun and has let me do a quite a bit of cmdlet development, which I really enjoy. If you use Service Manager, you should definitely check it out.</p>
<p>The real point of this post is to share my experience over the last couple of days at the PowerShell Deep Dive which happened at <a title="the Experts Conference" href="http://www.theexpertsconference.com/us/2011/" target="_blank">the Experts Conference </a>in Las Vegas. I have always been extremely proud of my work while I was working on PowerShell; giving birth to that product was a long, and at times, difficult experience which taught me a lot about myself. However, seeing how it has affected (in a positive way) so many people really makes all that worth while. Seeing the excitement in others cannot help but uplift the spirit and I am so glad that I had the opportunity to contribute. The stories about how PowerShell have improved the lives of others in concrete ways is awesome.</p>
<p>The experience has reinvigorated me as well; talking to so many intelligent, articulate people has given me a whole bunch of new ideas to write on, so I hope you will see a stream of posts here (as long as I can follow through!). My next post will be on creating custom formatting, hopefully posted before the plane lands.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jtruher3.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jtruher3.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jtruher3.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jtruher3.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jtruher3.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jtruher3.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jtruher3.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jtruher3.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jtruher3.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jtruher3.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jtruher3.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jtruher3.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jtruher3.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jtruher3.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=50&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jtruher3.wordpress.com/2011/04/19/missing-in-action/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/20349e038c0f35188b594c68f20636bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jtruher3</media:title>
		</media:content>
	</item>
		<item>
		<title>Service Manager Announcements</title>
		<link>http://jtruher3.wordpress.com/2010/06/13/service-manager-announcements/</link>
		<comments>http://jtruher3.wordpress.com/2010/06/13/service-manager-announcements/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 06:02:47 +0000</pubDate>
		<dc:creator>jtruher3</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jtruher3.wordpress.com/2010/06/13/service-manager-announcements</guid>
		<description><![CDATA[I&#8217;ve written in previous blogs on how to get data out of Service Manager, and generally, that data is usually simple text, numbers or sometimes an enumeration (which is pretty easy to convert to text). However, Service Manager also allows you to store text with formatting (rich text data) which can be pretty difficult to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=3&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="msgcns!7143DA6E51A2628D!974" class="bvMsg">
<p>I&#8217;ve written in previous blogs on how to get data out of Service Manager, and generally, that data is usually simple text, numbers or sometimes an enumeration (which is pretty easy to convert to text). However, Service Manager also allows you to store text with formatting (rich text data) which can be pretty difficult to view. First, let&#8217;s create an announcement:</p>
<p><a rel="WLPP" href="http://jtruher3.files.wordpress.com/2010/06/814927de78c91704000d2ff47cad5174.png"><img src="http://jtruher3.files.wordpress.com/2010/06/814927de78c91704000d2ff47cad5174.png?w=300" border="0" alt="" /></a></p>
<p>We can use one of the scripts created in an earlier posting to retrieve an instance of the announcement:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; get-smclass announcement.item|get-scsmobject|fl
Id             : 2
Title          : Announcement 001
Body           : {\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0
                 Times New Roman;}{\f2\fcharset0 Sego
                 e UI;}{\f3\fcharset0 Calibri;}{\f4\fcharset0 Copperplate Gothic
                 Bold;}}{\colortbl\red0\green0\blue0;\r
                 ed255\green255\blue255;\red255\green0\blue0;}
                 {\*\listtable
                 {\list\listtemplateid1\listhybrid
. . .
                 b0\jclisttab\tx720\fi-360\ql\par}
                 }
                 }
ExpirationDate : 6/28/2010 7:00:00 AM
Priority       : System.Announcement.PriorityEnum.Medium
DisplayName    : Announcement 001
Type           : System.Announcement.Item
Name           : 2
Path           :
FullName       : System.Announcement.Item:2</pre>
</div>
<p>Since the body property of the announcement is rich text, it really isn&#8217;t readable in this format, but we can fix that with a fun little script. This is one of those scripts that allow us to mix the command line and the graphical environment. We&#8217;ll call this script Display-RichText.ps1 since that&#8217;s what it does!</p>
<div style="width:650px;font-family:lucida console;font-size:8pt;overflow:auto;border:black 1px solid;padding:5px;">
<table border="0" cellspacing="0" cellpadding="5">
<tbody>
<tr>
<td valign="top">
<div style="font-family:lucida console;background:#cecece;font-size:8pt;padding:5px;">001<br />
002<br />
003<br />
004<br />
005<br />
006<br />
007<br />
008<br />
009<br />
010<br />
011<br />
012<br />
013<br />
014<br />
015<br />
016<br />
017<br />
018<br />
019<br />
020<br />
021<br />
022<br />
023<br />
024<br />
025<br />
026<br />
027<br />
028<br />
029<br />
030<br />
031<br />
032<br />
033<br />
034<br />
035<br />
036<br />
037<br />
038<br />
039<br />
040<br />
041<br />
042<br />
043<br />
044<br />
045<br />
046<br />
047<br />
048<br />
049<br />
050<br />
051<br />
052</div>
</td>
<td valign="top">
<div style="font-family:lucida console;background:#fcfcfc;font-size:8pt;padding:5px;"><span style="color:#00008b;">param</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$string</span><span style="color:#000000;"> </span><br />
<span style="color:#000000;"> </span><span style="color:#000000;">)</span><br />
<span style="color:#00008b;">begin</span><br />
<span style="color:#000000;">{</span><br />
<span style="color:#000000;"> </span><span style="color:#008080;">[void]</span><span style="color:#008080;">[reflection.assembly]</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">LoadWithPartialName</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&#8220;System.Windows.Forms&#8221;</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#008080;">[void]</span><span style="color:#008080;">[reflection.assembly]</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">LoadWithPartialName</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&#8220;System.Drawing&#8221;</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#006400;">## the form</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$form</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">System.Windows.Forms.Form</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$form</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">size</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">System.Drawing.Size</span><span style="color:#000000;"> </span><span style="color:#800080;">400</span><span style="color:#a9a9a9;">,</span><span style="color:#800080;">400</span>&nbsp;</p>
<p><span style="color:#000000;"> </span><span style="color:#006400;">## the Rich text box</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$text</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">System.Windows.Forms.RichTextBox</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$text</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">multiline</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$true</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$text</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">dock</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&#8220;Fill&#8221;</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$text</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">scrollbars</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&#8220;Both&#8221;</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$text</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">width</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">80</span></p>
<p><span style="color:#000000;"> </span><span style="color:#006400;">## Quit button</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$QuitButton</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">Windows.Forms.Button</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$QuitButton</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Name</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&#8220;QuitButton&#8221;</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$QuitButton</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">TabIndex</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">0</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$QuitButton</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Text</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&#8220;Quit&#8221;</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$QuitButton</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">UseVisualStyleBackColor</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$true</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$QuitButton</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Add_Click</span><span style="color:#000000;">(</span><span style="color:#000000;">{</span><span style="color:#ff4500;">$form</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">dispose</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#000000;">}</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$QuitButton</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Dock</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&#8220;Bottom&#8221;</span></p>
<p><span style="color:#000000;"> </span><span style="color:#ff4500;">$form</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">controls</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">add</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$text</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$form</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">controls</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">add</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$QuitButton</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#00008b;">function</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">loadtext</span><br />
<span style="color:#000000;"> </span><span style="color:#000000;">{</span><br />
<span style="color:#000000;"> </span><span style="color:#00008b;">param</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$string</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#00008b;">try</span><br />
<span style="color:#000000;"> </span><span style="color:#000000;">{</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$bytes</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#008080;">[byte[]]</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$string</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ToCharArray</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$stream</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">io.memorystream</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$bytes</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$true</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$text</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">loadfile</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$stream</span><span style="color:#a9a9a9;">,</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&#8220;richtext&#8221;</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$text</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">DeselectAll</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#008080;">[void]</span><span style="color:#ff4500;">$form</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">showdialog</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#000000;">}</span><br />
<span style="color:#000000;"> </span><span style="color:#00008b;">finally</span><br />
<span style="color:#000000;"> </span><span style="color:#000000;">{</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$stream</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">close</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#ff4500;">$stream</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">dispose</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><br />
<span style="color:#000000;"> </span><span style="color:#000000;">}</span><br />
<span style="color:#000000;"> </span><span style="color:#000000;">}</span></p>
<p><span style="color:#000000;">}</span><br />
<span style="color:#00008b;">end</span><br />
<span style="color:#000000;">{</span><br />
<span style="color:#000000;"> </span><span style="color:#0000ff;">loadtext</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$string</span><br />
<span style="color:#000000;">}</span></p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p>Now let&#8217;s see what we can do!</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $announcement = get-smclass announcement.item|get-scsmobject
PS&gt; display-richtext $announcement.body</pre>
</div>
<p><a rel="WLPP" href="http://jtruher3.files.wordpress.com/2010/06/5764c8c0bdf7bc6d8684db138aec0b6b.png"><img src="http://jtruher3.files.wordpress.com/2010/06/5764c8c0bdf7bc6d8684db138aec0b6b.png?w=300" border="0" alt="" /></a></p>
<p>Now we can see the contents of the announcement!</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jtruher3.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jtruher3.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jtruher3.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jtruher3.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jtruher3.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jtruher3.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jtruher3.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jtruher3.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jtruher3.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jtruher3.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jtruher3.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jtruher3.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jtruher3.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jtruher3.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=3&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jtruher3.wordpress.com/2010/06/13/service-manager-announcements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/20349e038c0f35188b594c68f20636bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jtruher3</media:title>
		</media:content>

		<media:content url="http://jtruher3.files.wordpress.com/2010/06/814927de78c91704000d2ff47cad5174.png?w=300" medium="image" />

		<media:content url="http://jtruher3.files.wordpress.com/2010/06/5764c8c0bdf7bc6d8684db138aec0b6b.png?w=300" medium="image" />
	</item>
		<item>
		<title>Saving Service Manager Instances in a CSV file</title>
		<link>http://jtruher3.wordpress.com/2010/06/04/saving-service-manager-instances-in-a-csv-file/</link>
		<comments>http://jtruher3.wordpress.com/2010/06/04/saving-service-manager-instances-in-a-csv-file/#comments</comments>
		<pubDate>Sat, 05 Jun 2010 01:24:05 +0000</pubDate>
		<dc:creator>jtruher3</dc:creator>
				<category><![CDATA[ServiceManager]]></category>

		<guid isPermaLink="false">http://jtruher3.wordpress.com/2010/06/04/saving-service-manager-instances-in-a-csv-file</guid>
		<description><![CDATA[In a post I did a few months ago, I wrote how it you can retrieve data from Service Manager via a PowerShell script, but I didn&#8217;t show much more than just plain output. Earlier this week I was asked if it would be possible to export configuration items in a CSV file. By using [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=4&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="msgcns!7143DA6E51A2628D!968" class="bvMsg">
<p>In a post I did a few months ago, <a href="http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!794.entry" target="_blank">I wrote how it you can retrieve data from Service Manager via a PowerShell script</a>, but I didn&#8217;t show much more than just plain output. Earlier this week I was asked if it would be possible to export configuration items in a CSV file. By using the script I wrote months ago, we have all the tools we need. Let&#8217;s review the previous script:</p>
<div style="border-bottom:black 1px solid;border-left:black 1px solid;width:792px;font-family:lucida console;height:429px;font-size:8pt;overflow:auto;border-top:black 1px solid;border-right:black 1px solid;padding:5px;">
<table border="0" cellspacing="0" cellpadding="5">
<tbody>
<tr>
<td valign="top">
<div style="font-family:lucida console;background:#cecece;font-size:8pt;padding:5px;">001<br />002<br />003<br />004<br />005<br />006<br />007<br />008<br />009<br />010<br />011<br />012<br />013<br />014<br />015<br />016<br />017<br />018<br />019<br />020<br />021<br />022<br />023<br />024<br />025<br />026<br />027<br />028<br />029<br />030<br />031<br />032<br />033<br />034</div>
</td>
<td valign="top">
<div style="font-family:lucida console;background:#fcfcfc;font-size:8pt;padding:5px;"><span style="color:#00008b;">param</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$classname</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><br /><span style="color:#ff4500;">$emg</span><span style="color:#000000;">      </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">microsoft.enterprisemanagement.enterprisemanagementgroup</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">localhost</span><br /><span style="color:#ff4500;">$class</span><span style="color:#000000;">    </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$emg</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">EntityTypes</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetClasses</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">|</span><span style="color:#0000ff;">?</span><span style="color:#000000;">&#123;</span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">name</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-eq</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$classname</span><span style="color:#000000;">&#125;</span><br /><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">!</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$class</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><br /><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">    </span><span style="color:#0000ff;">Write-Error</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;`nERROR: Class &#8216;$classname&#8217; not found, exiting.&quot;</span><br /><span style="color:#000000;">    </span><span style="color:#00008b;">exit</span><br /><span style="color:#000000;">&#125;</span><br /><span style="color:#ff4500;">$DEFAULT</span><span style="color:#000000;">  </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#008080;">[Microsoft.EnterpriseManagement.Common.ObjectQueryOptions]</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">Default</span><br /><span style="color:#ff4500;">$EMOT</span><span style="color:#000000;">     </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#008080;">[Microsoft.EnterpriseManagement.Common.EnterpriseManagementObject]</span><br /><span style="color:#006400;"># Retrieve the interface for EntityObjects, which we&#8217;ll use when we create our generic method</span><br /><span style="color:#ff4500;">$IMGMT</span><span style="color:#000000;">    </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$emg</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">EntityObjects</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetType</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><br /><span style="color:#006400;"># the types of the parameters, this is so we can find the right method</span><br /><span style="color:#008080;">[type[]]</span><span style="color:#ff4500;">$TYPES</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#008080;">[Microsoft.EnterpriseManagement.Configuration.ManagementPackClass]</span><span style="color:#a9a9a9;">,</span><br /><span style="color:#000000;">                 </span><span style="color:#008080;">[Microsoft.EnterpriseManagement.Common.ObjectQueryOptions]</span><br /><span style="color:#006400;"># Retrieve the method</span><br /><span style="color:#ff4500;">$ObjectReader</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$IMGMT</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetMethod</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;GetObjectReader&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$TYPES</span><span style="color:#000000;">)</span><br /><span style="color:#006400;"># Create a generic method</span><br /><span style="color:#ff4500;">$GenericMethod</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$ObjectReader</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">MakeGenericMethod</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$EMOT</span><span style="color:#000000;">)</span><br /><span style="color:#006400;"># Invoke the method with our arguments</span><br /><span style="color:#008080;">[array]</span><span style="color:#ff4500;">$arguments</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#008080;">[Microsoft.EnterpriseManagement.Configuration.ManagementPackClass]</span><span style="color:#ff4500;">$class</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$DEFAULT</span><br /><span style="color:#ff4500;">$GenericMethod</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">invoke</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$emg</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">EntityObjects</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$arguments</span><span style="color:#000000;">)</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">%</span><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># Create a custom object based on the original object</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$o</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">psobject</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$_</span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># elevate the properties in the Values collection to the top level</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$o</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">values</span><span style="color:#a9a9a9;">|</span><span style="color:#0000ff;">%</span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$o</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">add-member</span><span style="color:#000000;"> </span><span style="color:#000080;">-force</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Type</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Value</span><span style="color:#000000;"> </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># assign a synthetic typename to the object, so we can use our formatting</span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># more easily</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$name</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetLeastDerivedNonAbstractClass</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">name</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$o</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">psobject</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">typenames</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Insert</span><span style="color:#000000;">(</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">,</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;EnterpriseManagementObject#$name&quot;</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># now, emit the object!</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$o</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span></div>
</td>
</tr>
</tbody>
</table>
</div>
<p>We&#8217;ll call this script get-object (and it assumes that you&#8217;ve already loaded the Microsoft.EnterpriseManagement.Core.dll), and just to remind you, here&#8217;s how it works:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; get-object microsoft.windows.computer|format-table DisplayName,Netbios*,PrincipalName -au

DisplayName                   NetbiosComputerName NetbiosDomainName PrincipalName
-----------                   ------------------- ----------------- -------------
JWT-SCDW$                     JWT-SCDW            WOODGROVE         JWT-SCDW.woodgrove.com
WIN-752HJBSX24M.woodgrove.com WIN-752HJBSX24M     WOODGROVE         WIN-752HJBSX24M.woodgrove.com</pre>
</div>
<p>You provide a class name to it and it retrieves all the instances of that class. Now, to save these instances in a CSV file, I just need to use the Export-CSV cmdlet that is part of the PowerShell distribution and <em>viola!</em></p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; get-object microsoft.windows.computer|Export-CSV Microsoft.Windows.Computer.csv
PS&gt; get-content .\Microsoft.Windows.Computer.csv
#TYPE EnterpriseManagementObject#Microsoft.Windows.Computer
&quot;PrincipalName&quot;,&quot;DNSName&quot;,&quot;NetbiosComputerName&quot;,&quot;NetbiosDomainName&quot;,&quot;IPAddres...
&quot;JWT-SCDW.woodgrove.com&quot;,&quot;JWT-SCDW.woodgrove.com&quot;,&quot;JWT-SCDW&quot;,&quot;WOODGROVE&quot;,,,&quot;S...
&quot;WIN-752HJBSX24M.woodgrove.com&quot;,&quot;WIN-752HJBSX24M.woodgrove.com&quot;,&quot;WIN-752HJBSX...
</pre>
</div>
<p>You can use this to create a copy of your data, or use it as a way to exchange your data with other applications. Because of the way that other applications use CSV files, you may need to remove the first line of the CSV file which describes what the object was from the PowerShell perspective, but otherwise, you should be able to use the CSV file easily!</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jtruher3.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jtruher3.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jtruher3.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jtruher3.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jtruher3.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jtruher3.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jtruher3.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jtruher3.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jtruher3.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jtruher3.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jtruher3.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jtruher3.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jtruher3.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jtruher3.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=4&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jtruher3.wordpress.com/2010/06/04/saving-service-manager-instances-in-a-csv-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/20349e038c0f35188b594c68f20636bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jtruher3</media:title>
		</media:content>
	</item>
		<item>
		<title>More and more with management packs</title>
		<link>http://jtruher3.wordpress.com/2010/05/28/more-and-more-with-management-packs/</link>
		<comments>http://jtruher3.wordpress.com/2010/05/28/more-and-more-with-management-packs/#comments</comments>
		<pubDate>Fri, 28 May 2010 22:44:36 +0000</pubDate>
		<dc:creator>jtruher3</dc:creator>
				<category><![CDATA[ServiceManager]]></category>

		<guid isPermaLink="false">http://jtruher3.wordpress.com/2010/05/28/more-and-more-with-management-packs</guid>
		<description><![CDATA[I&#8217;ve been a little slow in updating here, but that doesn&#8217;t reflect any inactivity, just my poor rhythm for posting. Earlier in the year I did a post on what was possible to do in a single line of PowerShell. In this post, I&#8217;m going to take that further and explore this in more detail. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=5&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="msgcns!7143DA6E51A2628D!967" class="bvMsg">
<p>I&#8217;ve been a little slow in updating here, but that doesn&#8217;t reflect any inactivity, just my poor rhythm for posting.</p>
<p>Earlier in the year I did a post on what was possible to do in a single line of PowerShell. In this post, I&#8217;m going to take that further and explore this in more detail. Specifically, we&#8217;ll take a closer look at all of the possibilities with PowerShell and Service Manager management packs. I&#8217;ve blogged on management packs in the past, but generally on what to do with them rather than what they are. In this blog, we&#8217;ll take a look at the various properties of a management pack and how easy they are to access from PowerShell.</p>
<p>First, if we take a look at the available properties of a management pack, we see a pretty rich object model.</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg = new-object Microsoft.EnterpriseManagement.EnterpriseManagementGroup Localhost
PS&gt; $emg.ManagementPacks.GetManagementPacks()|get-member

   TypeName: Microsoft.EnterpriseManagement.Configuration.ManagementPack

Name                                        MemberType Definition
----                                        ---------- ----------
AcceptChanges                               Method     System.Void AcceptChanges(), System.Void AcceptChanges(Micros...
AddService                                  Method     System.Void AddService[T, V](string name, Microsoft.Enterpris...
CheckVersionCompatibility                   Method     System.Void CheckVersionCompatibility(Microsoft.EnterpriseMan...
Configure                                   Method     System.Void Configure(System.IO.Stream configuration)
DeleteEnterpriseManagementObjectGroup       Method     System.Void DeleteEnterpriseManagementObjectGroup(Microsoft.E...
DeleteMonitoringObjectGroup                 Method     System.Void DeleteMonitoringObjectGroup(Microsoft.EnterpriseM...
Dispose                                     Method     System.Void Dispose()
Equals                                      Method     bool Equals(System.Object obj)
FindManagementPackElementByName             Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackEl...
GetCategories                               Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackEl...
. . .
RemoveService                               Method     System.Void RemoveService(string name)
RemoveServices                              Method     System.Void RemoveServices()
ToString                                    Method     string ToString()
Verify                                      Method     System.Void Verify()
ContentReadable                             Property   System.Boolean ContentReadable &#123;get;&#125;
DefaultLanguageCode                         Property   System.String DefaultLanguageCode &#123;get;set;&#125;
Description                                 Property   System.String Description &#123;get;set;&#125;
DisplayName                                 Property   System.String DisplayName &#123;get;set;&#125;
. . .
SchemaVersion                               Property   System.Version SchemaVersion &#123;get;&#125;
Sealed                                      Property   System.Boolean Sealed &#123;get;&#125;
TimeCreated                                 Property   System.DateTime TimeCreated &#123;get;&#125;
Version                                     Property   System.Version Version &#123;get;set;&#125;
VersionId                                   Property   System.Guid VersionId &#123;get;&#125;
</pre>
</div>
<p>In fact, you&#8217;ll see about 150 different methods and properties. I&#8217;ve discussed various aspects of some of the properties and methods in earlier posts, but I want to concentrate on the <em>methods </em>in this posting, and the methods I want to look at specifically are the Get* methods. These methods return information about the contents and configuration of the management packs. Since there are about 100 Get* methods, i&#8217;d like to reduce the list to something more manageable, so we&#8217;ll only look at the get* methods that don&#8217;t take any arguments.</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.ManagementPacks.GetManagementPacks()|get-member get*|?&#123;$_.definition -match &quot;\(\)&quot;&#125;

   TypeName: Microsoft.EnterpriseManagement.Configuration.ManagementPack

Name                        MemberType Definition
----                        ---------- ----------
GetCategories               Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetClasses                  Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetConfigurationGroups      Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetConsoleTasks             Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetDataTypes                Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetDataWarehouseDataSets    Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetDataWarehouseScripts     Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetDiagnostics              Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetDimensionTypes           Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetDiscoveries              Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetEnumerations             Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetFactTypes                Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetFolderItems              Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackItemCollection[Mic...
GetFolders                  Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetForms                    Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetHashCode                 Method     int GetHashCode()
GetImageReferences          Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackItemCollection[Mic...
GetImages                   Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetLanguagePacks            Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetLinkedReports            Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetManagementPackCategories Method     System.Collections.Generic.IList[Microsoft.EnterpriseManagement.Configuration...
GetMeasureTypes             Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetModuleTypes              Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetMonitors                 Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetObjectTemplates          Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetOutriggerTypes           Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetOverrides                Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetRecoveries               Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetRelationshipFactTypes    Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetRelationships            Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetReportParameterControls  Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetReportResources          Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetReports                  Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetResources                Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetRules                    Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetSchemaTypes              Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetSecureReferences         Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetServiceLevelObjectives   Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetServices                 Method     System.Collections.Generic.IList[T] GetServices[T]()
GetStringResources          Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetTasks                    Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetTemplates                Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetType                     Method     type GetType()
GetTypeProjections          Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetUIPages                  Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetUIPageSets               Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetUnitMonitorTypes         Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetViews                    Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetViewTypes                Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...
GetWarehouseModuleTypes     Method     Microsoft.EnterpriseManagement.Configuration.ManagementPackElementCollection[...</pre>
</div>
<p>There are still <strong>50 </strong>of these, but there are some methods that are easier to use. First nearly all of these methods return a ManagementPackElementCollection, only 2 return a ManagementPackItemCollection (GetFolderItems and GetImageReferences), and 4 others require a typed invocation (GetServices, GetManagementPackCategories, GetResources and GetServiceLevelObjectives). We'll focus on the methods that return a ManagementPackElementCollection and that don't take a generic type, because they're the simplest to deal with. Since these methods don't take any arguments, we can just call them and see what we get back. The method name should give us a clue about what we'll get back, and you can check the Service Manager SDK documentation as well for more information.</p>
<p>As a first example, we'll retrieve the System.Library management pack and inspect all the classes declared in the mp and get their  base class.</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.ManagementPacks.GetManagementPacks()|?&#123;$_.Name -eq &quot;System.Library&quot;&#125;|
&gt;&gt; %&#123;$_.GetClasses()&#125;|ft Abstract,Name,@&#123;Label=&quot;Base&quot;;e=&#123;$emg.entitytypes.GetClass($_.base.id).Name&#125;&#125; -au
&gt;&gt;

Abstract Name                        Base
-------- ----                        ----
    True System.Entity
    True System.Collections          System.Entity
    True System.ConfigItem           System.Entity
    True System.LogicalEntity        System.ConfigItem
    True System.ApplicationComponent System.LogicalEntity
    True System.ComputerRole         System.LogicalEntity
    True System.Database             System.ApplicationComponent
    True System.Device               System.LogicalEntity
    True System.Computer             System.Device
    True System.FTPSite              System.ApplicationComponent
    True System.Group                System.LogicalEntity
    True System.LocalApplication     System.LogicalEntity
    True System.LogicalHardware      System.LogicalEntity
    True System.NetworkDevice        System.Device
    True System.OperatingSystem      System.LogicalEntity
    True System.Perspective          System.LogicalEntity
    True System.PhysicalEntity       System.ConfigItem
    True System.Printer              System.Device
    True System.Service              System.LogicalEntity
    True System.SoftwareInstallation System.LogicalEntity
    True System.User                 System.LogicalEntity
   False System.Domain.User          System.User
    True System.WebSite              System.ApplicationComponent</pre>
</div>
<p>Note that I'm using the EMG in the formatting directives to get the base class. </p>
<p>If we wanted to get the XML for one of these classes, we can do that with the CreateNavigator method.  We'll collect the classes and then use the CreateNavigator method to get at the XML. This examples retrieves the System.Library management pack and then displays the XML for the System.ConfigItem class.</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.ManagementPacks.GetManagementPacks()|?&#123;$_.Name -eq &quot;System.Library&quot;&#125;|
&gt;&gt; %&#123;$_.GetClasses()&#125;|?&#123;$_.Name -eq &quot;System.ConfigItem&quot;&#125;|%&#123;$_.CreateNavigator().OuterXML&#125;
&gt;&gt;
&lt;ClassType ID=&quot;System.ConfigItem&quot; Accessibility=&quot;Public&quot; Abstract=&quot;true&quot;
        Base=&quot;System.Entity&quot; Hosted=&quot;false&quot; Singleton=&quot;false&quot; Extension=&quot;false&quot;&gt;
  &lt;Property ID=&quot;ObjectStatus&quot; Type=&quot;enum&quot; AutoIncrement=&quot;false&quot; Key=&quot;false&quot; CaseSensitive=&quot;false&quot;
        MaxLength=&quot;256&quot; MinLength=&quot;0&quot; Required=&quot;false&quot; EnumType=&quot;System.ConfigItem.ObjectStatusEnum&quot;
        DefaultValue=&quot;System.ConfigItem.ObjectStatusEnum.Active&quot; /&gt;
  &lt;Property ID=&quot;AssetStatus&quot; Type=&quot;enum&quot; AutoIncrement=&quot;false&quot; Key=&quot;false&quot; CaseSensitive=&quot;false&quot;
        MaxLength=&quot;256&quot; MinLength=&quot;0&quot; Required=&quot;false&quot; EnumType=&quot;System.ConfigItem.AssetStatusEnum&quot; /&gt;
  &lt;Property ID=&quot;Notes&quot; Type=&quot;richtext&quot; AutoIncrement=&quot;false&quot; Key=&quot;false&quot; CaseSensitive=&quot;false&quot;
        MaxLength=&quot;4000&quot; MinLength=&quot;0&quot; Required=&quot;false&quot; /&gt;
&lt;/ClassType&gt;
</pre>
</div>
<p>Pretty cool! The following example retrieves all the views in the system:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.ManagementPacks.GetManagementPacks()|%&#123;$_.GetViews()&#125;|ft name,description

Name                                                        Description
----                                                        -----------
Microsoft.EnterpriseManagement.ServiceManager.UI.Adminis... Lists all subscriptions
Microsoft.EnterpriseManagement.ServiceManager.UI.Adminis... Lists all Run As accounts
Microsoft.EnterpriseManagement.ServiceManager.UI.Adminis... Contains general, portal, and solution settings
Microsoft.EnterpriseManagement.ServiceManager.UI.Adminis... Administration Overview
Microsoft.EnterpriseManagement.ServiceManager.UI.Adminis... Lists all templates
. . .
AllComputersView                                            Lists all computers
AllPrintersView                                             Lists all printers
QueuesView                                                  Lists all the queues available
TemplatesView                                               Lists all the templates available
TasksView                                                   Lists all the console tasks defined in the system
EnumerationView                                             Displays all the lists available
GroupsView                                                  Lists all groups
WorkItemExclusionRule                                       Work item exclusion workflow view
ChangeManagement.Views.ChangeRequestsCancelled              Lists all canceled change requests
ChangeManagement.Views.ChangeRequestsCompleted              Lists all completed change requests
ChangeManagement.Views.ChangeRequestsClosed                 Lists all closed change requests
ChangeManagement.Views.AllChangeRequests                    Lists all change requests
ChangeManagement.Views.ChangeRequestsRejected               Lists all rejected change requests
ChangeManagement.Views.ChangeRequestsInReview               Change Requests: In Review
. . .
System.WorkItem.Incident.Pending.View                       Lists all pending incidents
System.WorkItem.Incident.OverDue.View                       Lists all overdue incidents
System.WorkItem.Incident.Active.Unassigned.View             Lists all open unassigned incidents
Microsoft.SystemCenter.AllActiveAnnouncementsView           Active Announcements
Microsoft.SystemCenter.AllAnnouncementsView                 All Announcements
</pre>
</div>
<p>And we can inspect the XML for the one of the views (say the AllPrintersView), with this one-liner:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.ManagementPacks.GetManagementPacks()|%&#123;$_.GetViews()&#125;|
&gt;&gt; ?&#123;$_.name -eq &quot;allprintersview&quot;&#125;| %&#123;$_.createnavigator().outerxml&#125;

&lt;View ID=&quot;AllPrintersView&quot; Accessibility=&quot;Public&quot; Enabled=&quot;true&quot; Target=&quot;Windows!Microsoft.AD.Printer&quot; TypeID=&quot;SMConsole!GridViewType&quot; Visible=&quot;true&quot;&gt;
  &lt;Category&gt;NotUsed&lt;/Category&gt;
  &lt;Data&gt;
    &lt;Adapters&gt;
      &lt;Adapter AdapterName=&quot;dataportal:EnterpriseManagementObjectProjectionAdapter&quot;&gt;
        &lt;AdapterAssembly&gt;Microsoft.EnterpriseManagement.UI.SdkDataAccess&lt;/AdapterAssembly&gt;
        &lt;AdapterType&gt;Microsoft.EnterpriseManagement.UI.SdkDataAccess.DataAdapters.EnterpriseManagementObjectProjectionAdapter&lt;/AdapterType&gt;
      &lt;/Adapter&gt;
      &lt;Adapter AdapterName=&quot;viewframework://Adapters/AdvancedList&quot;&gt;
        &lt;AdapterAssembly&gt;Microsoft.EnterpriseManagement.UI.ViewFramework&lt;/AdapterAssembly&gt;
        &lt;AdapterType&gt;Microsoft.EnterpriseManagement.UI.ViewFramework.AdvancedListSupportAdapter&lt;/AdapterType&gt;
      &lt;/Adapter&gt;
      &lt;Adapter AdapterName=&quot;omsdk://Adapters/Criteria&quot;&gt;
        &lt;AdapterAssembly&gt;Microsoft.EnterpriseManagement.UI.SdkDataAccess&lt;/AdapterAssembly&gt;
        &lt;AdapterType&gt;Microsoft.EnterpriseManagement.UI.SdkDataAccess.DataAdapters.SdkCriteriaAdapter&lt;/AdapterType&gt;
      &lt;/Adapter&gt;
    &lt;/Adapters&gt;
    &lt;ItemsSource&gt;
      &lt;AdvancedListSupportClass DataTypeName=&quot;&quot; AdapterName=&quot;viewframework://Adapters/AdvancedList&quot; FullUpdateAdapter=&quot;dataportal:EnterpriseManagementObjectProjectionAdapter&quot; DataSource=&quot;mom:ManagementGroup&quot; IsRecurring=&quot;True&quot; RecurrenceFrequency=&quot;&#123;x:Static s:Int32.MaxValue&#125;&quot; FullUpdateFrequency=&quot;1&quot; Streaming=&quot;true&quot; xmlns=&quot;clr-namespace:Microsoft.EnterpriseManagement.UI.ViewFramework;assembly=Microsoft.EnterpriseManagement.UI.ViewFramework&quot; xmlns:av=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; xmlns:s=&quot;clr-namespace:System;assembly=mscorlib&quot;&gt;
        &lt;AdvancedListSupportClass.Parameters&gt;
          &lt;QueryParameter Parameter=&quot;TypeProjectionName&quot; Value=&quot;Microsoft.Windows.PrinterView.ProjectionType&quot; /&gt;
        &lt;/AdvancedListSupportClass.Parameters&gt;
      &lt;/AdvancedListSupportClass&gt;
    &lt;/ItemsSource&gt;
    &lt;Criteria&gt;
      &lt;QueryCriteria Adapter=&quot;omsdk://Adapters/Criteria&quot; xmlns=&quot;http://tempuri.org/Criteria.xsd&quot;&gt;
        &lt;Criteria&gt;
          &lt;FreeformCriteria&gt;
            &lt;Freeform&gt;
              &lt;Criteria xmlns=&quot;http://Microsoft.EnterpriseManagement.Core.Criteria/&quot;&gt;
                &lt;Expression&gt;
                  &lt;SimpleExpression&gt;
                    &lt;ValueExpressionLeft&gt;
                      &lt;Property&gt;$Context/Property[Type='System!System.ConfigItem']/ObjectStatus$&lt;/Property&gt;
                    &lt;/ValueExpressionLeft&gt;
                    &lt;Operator&gt;NotEqual&lt;/Operator&gt;
                    &lt;ValueExpressionRight&gt;
                      &lt;Value&gt;$MPElement[Name=&quot;System!System.ConfigItem.ObjectStatusEnum.PendingDelete&quot;]$&lt;/Value&gt;
                    &lt;/ValueExpressionRight&gt;
                  &lt;/SimpleExpression&gt;
                &lt;/Expression&gt;
              &lt;/Criteria&gt;
            &lt;/Freeform&gt;
          &lt;/FreeformCriteria&gt;
        &lt;/Criteria&gt;
      &lt;/QueryCriteria&gt;
    &lt;/Criteria&gt;
  &lt;/Data&gt;
  &lt;Presentation&gt;
    &lt;Columns&gt;
      &lt;mux:ColumnCollection xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:mux=&quot;http://schemas.microsoft.com/SystemCenter/Common/UI/Views/GridView&quot; xmlns:s=&quot;clr-namespace:System;assembly=mscorlib&quot; xmlns:datebinding=&quot;clr-namespace:Microsoft.EnterpriseManagement.UI.SdkDataAccess.Common;assembly=Microsoft.EnterpriseManagement.UI.SdkDataAccess&quot;&gt;
        &lt;mux:Column Name=&quot;uncName&quot; DisplayMemberBinding=&quot;&#123;Binding Path=UNCName&#125;&quot; Width=&quot;120&quot; DisplayName=&quot;Header_UNCName&quot; Property=&quot;UNCName&quot; DataType=&quot;s:String&quot; /&gt;
        &lt;mux:Column Name=&quot;printerName&quot; DisplayMemberBinding=&quot;&#123;Binding Path=PrinterName&#125;&quot; Width=&quot;120&quot; DisplayName=&quot;Header_PrinterName&quot; Property=&quot;PrinterName&quot; DataType=&quot;s:String&quot; /&gt;
        &lt;mux:Column Name=&quot;description&quot; DisplayMemberBinding=&quot;&#123;Binding Path=Description&#125;&quot; Width=&quot;120&quot; DisplayName=&quot;Header_Description&quot; Property=&quot;Description&quot; DataType=&quot;s:String&quot; /&gt;
        &lt;mux:Column Name=&quot;location&quot; DisplayMemberBinding=&quot;&#123;Binding Path=Location&#125;&quot; Width=&quot;120&quot; DisplayName=&quot;Header_Location&quot; Property=&quot;Location&quot; DataType=&quot;s:String&quot; /&gt;
      &lt;/mux:ColumnCollection&gt;
    &lt;/Columns&gt;
    &lt;ViewStrings&gt;
      &lt;ViewString ID=&quot;Header_UNCName&quot;&gt;$MPElement[Name=&quot;AllPrintersView.Header_UNCName&quot;]$&lt;/ViewString&gt;
      &lt;ViewString ID=&quot;Header_PrinterName&quot;&gt;$MPElement[Name=&quot;AllPrintersView.Header_PrinterName&quot;]$&lt;/ViewString&gt;
      &lt;ViewString ID=&quot;Header_Description&quot;&gt;$MPElement[Name=&quot;AllPrintersView.Header_Description&quot;]$&lt;/ViewString&gt;
      &lt;ViewString ID=&quot;Header_Location&quot;&gt;$MPElement[Name=&quot;AllPrintersView.Header_Location&quot;]$&lt;/ViewString&gt;
    &lt;/ViewStrings&gt;
  &lt;/Presentation&gt;
&lt;/View&gt;
</pre>
</pre>
</div>
<p>Since the CreateNavigator method is available on ManagementPackElement, we can use that method with every one of the objects returned by the methods mentioned above - it's a great way to explore and see what's going on in your management pack.</p>
<p><em>As an interesting aside, we can invoke all of these simple methods with a couple of lines of script. This will tell us the total number of each of the management pack elements returned by the method. The first line collects the names of the methods that we want to invoke. The second line creates a hash table to hold our results and the last line retrieves all the management packs, and invokes each method on the management pack object and adds the count of those elements to the hash table. This is a pretty cool trick to invoke a method without know what the method name is before hand.</em></p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $names = $emg.ManagementPacks.GetManagementPacks()|
&gt;&gt; get-member get*s|
&gt;&gt; ?&#123;$_.definition -match &quot;ManagementPackElementCollection&quot; -and
&gt;&gt; $_.definition -notmatch &quot;\[T\]&quot;&#125;|%&#123;$_.name&#125;
PS&gt; $counthash = $names | %&#123; $h = @&#123;&#125;&#125;&#123;$h.$_ = 0 &#125; &#123;$h &#125;
PS&gt; $emg.ManagementPacks.GetManagementPacks()|%&#123;
&gt;&gt; $mp = $_
&gt;&gt; $names | %&#123; $counthash.$_ += $mp.$_.invoke().count &#125;
&gt;&gt; &#125;
&gt;&gt;
PS&gt; $counthash

Name                           Value
----                           -----
GetReportResources             28
GetModuleTypes                 293
GetObjectTemplates             24
GetUIPages                     48
GetOutriggerTypes              30
GetRules                       53
GetTasks                       6
GetReports                     23
GetSchemaTypes                 28
GetClasses                     272
GetMonitors                    47
GetTypeProjections             56
GetReportParameterControls     0
GetConfigurationGroups         0
GetDimensionTypes              26
GetRelationships               124
GetViewTypes                   24
GetLinkedReports               0
GetStringResources             1211
GetFactTypes                   4
GetDataTypes                   45
GetTemplates                   3
GetForms                       16
GetFolders                     85
GetWarehouseModuleTypes        9
GetMeasureTypes                2
GetSecureReferences            11
GetRecoveries                  1
GetViews                       138
GetDiscoveries                 31
GetUIPageSets                  225
GetEnumerations                460
GetDataWarehouseScripts        38
GetConsoleTasks                160
GetCategories                  583
GetDataWarehouseDataSets       0
GetOverrides                   49
GetImages                      254
GetDiagnostics                 0
GetLanguagePacks               89
GetRelationshipFactTypes       28
GetUnitMonitorTypes            239
</pre>
</div>
<p>woo hoo!</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jtruher3.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jtruher3.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jtruher3.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jtruher3.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jtruher3.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jtruher3.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jtruher3.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jtruher3.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jtruher3.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jtruher3.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jtruher3.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jtruher3.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jtruher3.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jtruher3.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=5&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jtruher3.wordpress.com/2010/05/28/more-and-more-with-management-packs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/20349e038c0f35188b594c68f20636bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jtruher3</media:title>
		</media:content>
	</item>
		<item>
		<title>Testing the Integrity of a Management Pack</title>
		<link>http://jtruher3.wordpress.com/2010/05/12/testing-the-integrity-of-a-management-pack/</link>
		<comments>http://jtruher3.wordpress.com/2010/05/12/testing-the-integrity-of-a-management-pack/#comments</comments>
		<pubDate>Wed, 12 May 2010 21:49:30 +0000</pubDate>
		<dc:creator>jtruher3</dc:creator>
				<category><![CDATA[ServiceManager]]></category>

		<guid isPermaLink="false">http://jtruher3.wordpress.com/2010/05/12/testing-the-integrity-of-a-management-pack</guid>
		<description><![CDATA[When I&#8217;m creating a new management pack, I want to be sure that the management pack is valid before I import it. I know that if it&#8217;s wrong, the system won&#8217;t import it, but I generally like to know these things before I try. In order to do that, I wrote a pretty simple script [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=6&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="msgcns!7143DA6E51A2628D!964" class="bvMsg">
<p>When I&#8217;m creating a new management pack, I want to be sure that the management pack is valid before I import it. I know that if it&#8217;s wrong, the system won&#8217;t import it, but I generally like to know these things before I try. In order to do that, I wrote a pretty simple script to test the management pack. It takes advantage of the Verify method on management pack object. This verification does a number of things. First, it checks to be sure that there are no XSD validation errors. If there are, the method throws an exception. The Verify method also checks to be sure that references are correct and present. Note that there may still be some errors that are found upon import, but most of the issues will be caught by this script.</p>
<p>When everything works right, you&#8217;ll see something like the following:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; test-managementpack .\PowerShell.WATest.xml|ft Verified,Name,FullName -au

Verified Name              FullName
-------- ----              --------
True     PowerShell.WATest C:\Program Files\System Center Management Packs\PowerShell.WATest.xml</pre>
</pre>
</div>
<p>However, if something is busted, you&#8217;ll see this:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; test-managementpack .\BigHonkingMP.xml|Ft Verified,Name,FullName -au

Verified Name         FullName
-------- ----         --------
False    BigHonkingMP C:\Program Files\System Center Management Packs\BigHonkingMP.xml</pre>
</pre>
</div>
<p>The results of test-managementpack include the error, you can see what happened, by getting the Error property:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; test-managementpack .\BigHonkingMP.xml|Fl name,error

Name  : BigHonkingMP
Error : &#123;Exception calling &quot;Verify&quot; with &quot;0&quot; argument(s): &quot;Verification failed with 1 errors:
        -------------------------------------------------------
        Error 1:
        : Failed to verify class: BigHonkingMP.ConcreteMicroBleh
        Host class BigHonkingMP.AbstractBleh and hosted class BigHonkingMP.ConcreteMicroBleh define the same set of key properties.
        -------------------------------------------------------
        &quot;&#125;</pre>
</div>
<p>You can also pipe files at test-managementpack, so you can use it like this:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; ls *.xml|test-managementpack

Verified Name                                           FullName
-------- ----                                           --------
False    BigHonkingMP                                   C:\Program Files\System Center Management Packs\BigHonkingMP.xml
. . .</pre>
</pre>
</div>
<p>Which I thought was pretty handy &#8211; going through a bunch of MPs at once.</p>
<p>Here&#8217;s the script, it relies on version 2.0 of PowerShell. I&#8217;ve added online help, so you can run get-help test-managementpack to get info on it. The business end of the script is in line 29 and 30. That&#8217;s where I create a management pack object and then call verify. If the management pack object can&#8217;t be created, or if it fails verify, the exception gets caught and then the script builds up an error message. Rather than using Write-Error to indicate the problem, I decided to include the error message in resultant object. I did this because I didn&#8217;t really want the error output at this point, but I wanted to hang on to the verify failure.</p>
<p> </p>
<div style="border-bottom:black 1px solid;border-left:black 1px solid;width:650px;font-family:lucida console;font-size:8pt;overflow:auto;border-top:black 1px solid;border-right:black 1px solid;padding:5px;">
<table border="0" cellspacing="0" cellpadding="5">
<tbody>
<tr>
<td valign="top">
<div style="font-family:lucida console;background:#cecece;font-size:8pt;padding:5px;">001<br />002<br />003<br />004<br />005<br />006<br />007<br />008<br />009<br />010<br />011<br />012<br />013<br />014<br />015<br />016<br />017<br />018<br />019<br />020<br />021<br />022<br />023<br />024<br />025<br />026<br />027<br />028<br />029<br />030<br />031<br />032<br />033<br />034<br />035<br />036<br />037<br />038<br />039<br />040<br />041<br />042<br />043<br />044<br />045<br />046<br />047<br />048<br />049<br />050<br />051<br />052<br />053<br />054<br />055<br />056<br />057<br />058<br />059<br />060<br />061<br />062<br />063<br />064<br />065<br />066<br />067<br />068<br />069<br />070<br />071<br />072<br />073<br />074<br />075<br />076<br />077<br />078<br />079<br />080<br />081<br />082<br />083<br />084<br />085<br />086<br />087<br />088<br />089<br />090</div>
</td>
<td valign="top">
<div style="font-family:lucida console;background:#fcfcfc;font-size:8pt;padding:5px;"><span style="color:#006400;">#requires -version 2.0</span></p>
<p><span style="color:#00008b;">param</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">    </span><span style="color:#a9a9a9;">[</span><span style="color:#add8e6;">parameter</span><span style="color:#000000;">(</span><span style="color:#000000;">Position</span><span style="color:#a9a9a9;">=</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">,</span><span style="color:#000000;">Mandatory</span><span style="color:#a9a9a9;">=</span><span style="color:#ff4500;">$true</span><span style="color:#a9a9a9;">,</span><span style="color:#000000;">ValueFromPipeline</span><span style="color:#a9a9a9;">=</span><span style="color:#ff4500;">$true</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><br /><span style="color:#000000;">    </span><span style="color:#008080;">[string[]]</span><span style="color:#ff4500;">$fullname</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">    </span><span style="color:#000000;">)</span></p>
<p><span style="color:#00008b;">BEGIN</span><br /><span style="color:#000000;">&#123;</span><br /><span style="color:#006400;"># GLOBAL REFERENCES</span><br /><span style="color:#006400;"># $SMDIR</span><br /><span style="color:#ff4500;">$NS</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;Microsoft.EnterpriseManagement&quot;</span><br /><span style="color:#008080;">[reflection.assembly]</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">LoadWithPartialName</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Core&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">|</span><span style="color:#0000ff;">out-null</span><br /><span style="color:#ff4500;">$EMG</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.EnterpriseManagementGroup&quot;</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">localhost</span><br /><span style="color:#ff4500;">$MPSType</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Configuration.IO.ManagementPackFileStore&quot;</span><br /><span style="color:#ff4500;">$MPType</span><span style="color:#000000;">  </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Configuration.ManagementPack&quot;</span><br /><span style="color:#000000;">&#125;</span></p>
<p><span style="color:#00008b;">PROCESS</span><br /><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$fullname</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">%</span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$path</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#0000ff;">resolve-path</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$_</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">path</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$FInfo</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#008080;">[io.fileinfo]</span><span style="color:#ff4500;">$path</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$dir</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$FInfo</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">DirectoryName</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$fil</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$FInfo</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">FullName</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$error</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">clear</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">        </span><span style="color:#00008b;">try</span><br /><span style="color:#000000;">        </span><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$MP</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$MPType</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$fil</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$EMG</span><br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$MP</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Verify</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">        </span><span style="color:#00008b;">catch</span><span style="color:#000000;"> </span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span><span style="color:#000000;">;</span><span style="color:#000000;"> </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">        </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$error</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">count</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">        </span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$Verified</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$false</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$msgs</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$error</span><span style="color:#a9a9a9;">|</span><span style="color:#0000ff;">%</span><span style="color:#000000;">&#123;</span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Exception</span><span style="color:#000000;">;</span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Exception</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">InnerException</span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$ErrorMessage</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$msgs</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-join</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;`n&quot;</span><br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">        </span><span style="color:#00008b;">else</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">        </span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$ErrorMessage</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;No Errors&quot;</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$Verified</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$true</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$PSO</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">PSObject</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$PSO</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">psobject</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">typenames</span><span style="color:#a9a9a9;">[</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">]</span><span style="color:#a9a9a9;">=</span><span style="color:#000000;">  </span><span style="color:#8b0000;">&quot;Microsoft.EnterpriseManagement.Configuration.ManagementPack.CustomVerification&quot;</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$PSO</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">add-member</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">Verified</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Verified</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$PSO</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">add-member</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">Name</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$MP</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Name</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$PSO</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">add-member</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">FullName</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$fil</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$PSO</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">add-member</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">Error</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$error</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">clone</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$PSO</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">add-member</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">ErrorMessage</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$ErrorMessage</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$PSO</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">&#125;</span></p>
<p><span style="color:#006400;">&lt;#<br />.SYNOPSIS<br />    Verify the integrity of a management pack<br />.DESCRIPTION<br />    The cmdlet attempts to create a management pack object based on the<br />    provided file and then calls the verify method to determine whether<br />    the management pack is valid.<br />.PARAMETER fullname<br />    The fullname of the management pack<br />.EXAMPLE<br />ls wf*.xml|test-ManagementPack<br />Verified Name FullName<br />&#8212;&#8212;&#8211; &#8212;- &#8212;&#8212;&#8211;<br />True WF.NoWorkflow C:\Program Files\System Center Management Packs\wf.NoWorkflow.xml<br />True WF.Simple C:\Program Files\System Center Management Packs\wf.Simple.xml<br />True WF.SingleCmdTask C:\Program Files\System Center Management Packs\wf.SingleCmdTask.xml<br />True WF.SingleTask C:\Program Files\System Center Management Packs\wf.SingleTask.xml</p>
<p>.INPUTS<br />    Output from get-childitem<br />    Any object which has a fullname property which represents a management pack<br />.OUTPUTS<br />    A custom object which contains the following:<br />        Verified &#8211; A boolean indicating whether the MP was verified<br />        Name &#8211; The name of the management pack<br />        FullName &#8211; The path to the management pack<br />        Error &#8211; Errors produced while verifying the management pack<br />.LINK<br />    Export-ManagementPack<br />    Get-ManagementPack<br />    Import-ManagementPack<br />    New-ManagementPack<br />    New-SealedManagementPack<br />    Remove-ManagementPack</p>
<p>#&gt;</span> </div>
</td>
</tr>
</tbody>
</table>
</div></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jtruher3.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jtruher3.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jtruher3.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jtruher3.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jtruher3.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jtruher3.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jtruher3.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jtruher3.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jtruher3.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jtruher3.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jtruher3.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jtruher3.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jtruher3.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jtruher3.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=6&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jtruher3.wordpress.com/2010/05/12/testing-the-integrity-of-a-management-pack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/20349e038c0f35188b594c68f20636bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jtruher3</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting Service Manager Incidents with PowerShell</title>
		<link>http://jtruher3.wordpress.com/2010/02/05/getting-service-manager-incidents-with-powershell/</link>
		<comments>http://jtruher3.wordpress.com/2010/02/05/getting-service-manager-incidents-with-powershell/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 01:57:21 +0000</pubDate>
		<dc:creator>jtruher3</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jtruher3.wordpress.com/2010/02/05/getting-service-manager-incidents-with-powershell</guid>
		<description><![CDATA[So far in these blogs, I’ve not addressed one of the main focuses of Service Manager which is how to manage incidents. In Service Manager, incidents are more than simple instances, they’re what’s called a “projection”. Jakub has some great postings on projections (here and here), so I’m not going to go into detail about [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=7&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="msgcns!7143DA6E51A2628D!950" class="bvMsg">
<p>So far in these blogs, I’ve not addressed one of the main focuses of Service Manager which is how to manage incidents. In Service Manager, incidents are more than simple instances, they’re what’s called a “projection”. Jakub has some great postings on projections (<a href="http://blogs.msdn.com/jakuboleksy/archive/2009/01/20/getting-started-with-type-projections.aspx" target="_blank">here</a> and <a href="http://blogs.msdn.com/jakuboleksy/archive/2009/02/04/more-with-type-projections.aspx" target="_blank">here</a>), so I’m not going to go into detail about what a projection is, but rather how it affects PowerShell. The object model of the projection makes it a little tricky to work with from PowerShell, but can be done.</p>
<p>Here’s what the incident view looks like in the console.</p>
<p><a href="http://yxxxiq.blu.livefilestore.com/y1permgLMYXkL5otob8VTCHywL4GdAX1STpcJQklT78DfugO30oEvRzZH5oCZMIL0qhDflUiZiq3H3cdjsT3EpCb_HcLTEfjvms?PARTNER=WRITER" rel="WLPP"><img border="0" src="http://yxxxiq.blu.livefilestore.com/y1pnx1JggTl_po23rT_PGAkMk9TJMFeW3cXO-13eAzDBYylX-Inu3KyOCj7h1Asg1b78G8Mb1qQPoBjYFaKY5abjbz7Xz3zygbi?PARTNER=WRITER" /></a></p>
<p>That’s my target – create a script that has output similar to the console.</p>
<p>The script is constructed into two sections:</p>
<ul>
<li>The BEGIN section makes sure that the assemblies we need are loaded and creates functions that the script will use.
<ul>
<li>The code in lines 20 to 50 make sure Microsoft.EnterpriseManagement.Core.dll is loaded and that we have a successful connection to the Data Access Service.
<li>The Get-SMIncident function does the actual work of retrieving the incident from the Data Access Service. It’s a little tricky because the Service Manager 2010 SDK uses generics, so we need a bit of reflection to invoke the method. This function also creates the criteria that we use to retrieve the incident. This way we filter on the server side rather than the client side which will be <em>much </em>faster if we have any reasonable number of incidents.
<li>The Get-AdaptedEMO function converts the EnterpriseManagementObject to something more idiomatic for PowerShell. I’ve mentioned this before, but since the actual interesting information is actually in the Values property of the EnterpriseManagementObject, this function creates a PSCustom object to which we add NoteProperties. This will help us later when we start formatting. </li>
</li>
</ul>
<li>The END section does the work of retrieving the incident and adapting it from how it is retrieved from the Data Access Service to something more useful.
<ul>
<li>This is done by adapting the main object of the projection (the object property on the projection) and the EnterpriseManagementObjects that represent the various relationships which is in line 166 of the script. It takes advantage of the fact that a projection object has an enumerator. Line 166 retrieves the keys which are then used in the foreach loop in 167-174 to retrieve each related object of the projection. That object is then adapted for PowerShell use and added as a property to the PSCustom object which represents the incident.
<li>Lines 176-188 promote some of the properties of the main object (the incident’s Object property) as well as some of the related objects properties so we can create the formatting we want more easily.
<li>Finally, the adapted incident is output (line 190) </li>
</ul>
</li>
</li>
</ul>
<div style="border-bottom:black 1px solid;border-left:black 1px solid;width:591px;font-family:lucida console;height:315px;font-size:8pt;overflow:auto;border-top:black 1px solid;border-right:black 1px solid;padding:5px;">
<table border="0" cellspacing="0" cellpadding="5">
<tbody>
<tr>
<td valign="top">
<div style="font-family:lucida console;background:#cecece;font-size:8pt;padding:5px;">001 <br />002 <br />003 <br />004 <br />005 <br />006 <br />007 <br />008 <br />009 <br />010 <br />011 <br />012 <br />013 <br />014 <br />015 <br />016 <br />017 <br />018 <br />019 <br />020 <br />021 <br />022 <br />023 <br />024 <br />025 <br />026 <br />027 <br />028 <br />029 <br />030 <br />031 <br />032 <br />033 <br />034 <br />035 <br />036 <br />037 <br />038 <br />039 <br />040 <br />041 <br />042 <br />043 <br />044 <br />045 <br />046 <br />047 <br />048 <br />049 <br />050 <br />051 <br />052 <br />053 <br />054 <br />055 <br />056 <br />057 <br />058 <br />059 <br />060 <br />061 <br />062 <br />063 <br />064 <br />065 <br />066 <br />067 <br />068 <br />069 <br />070 <br />071 <br />072 <br />073 <br />074 <br />075 <br />076 <br />077 <br />078 <br />079 <br />080 <br />081 <br />082 <br />083 <br />084 <br />085 <br />086 <br />087 <br />088 <br />089 <br />090 <br />091 <br />092 <br />093 <br />094 <br />095 <br />096 <br />097 <br />098 <br />099 <br />100 <br />101 <br />102 <br />103 <br />104 <br />105 <br />106 <br />107 <br />108 <br />109 <br />110 <br />111 <br />112 <br />113 <br />114 <br />115 <br />116 <br />117 <br />118 <br />119 <br />120 <br />121 <br />122 <br />123 <br />124 <br />125 <br />126 <br />127 <br />128 <br />129 <br />130 <br />131 <br />132 <br />133 <br />134 <br />135 <br />136 <br />137 <br />138 <br />139 <br />140 <br />141 <br />142 <br />143 <br />144 <br />145 <br />146 <br />147 <br />148 <br />149 <br />150 <br />151 <br />152 <br />153 <br />154 <br />155 <br />156 <br />157 <br />158 <br />159 <br />160 <br />161 <br />162 <br />163 <br />164 <br />165 <br />166 <br />167 <br />168 <br />169 <br />170 <br />171 <br />172 <br />173 <br />174 <br />175 <br />176 <br />177 <br />178 <br />179 <br />180 <br />181 <br />182 <br />183 <br />184 <br />185 <br />186 <br />187 <br />188 <br />189 <br />190 <br />191 <br />192</div>
</td>
<td valign="top">
<div style="font-family:lucida console;background:#fcfcfc;font-size:8pt;padding:5px;"><span style="color:#006400;">#requires -version 2.0</span> <br /><span style="color:#006400;"># Get-Incident</span> <br /><span style="color:#006400;"># Retrieve Service Manager Incidnents</span> <br /><span style="color:#006400;"># The IncidentString may need to include a trailing &#8216;%&#8217;</span> <br /><span style="color:#006400;"># examples:</span> <br /><span style="color:#006400;"># Get-Incident IR17%</span> <br /><span style="color:#006400;"># Retrieves incidents that have a displayname which starts with &#8216;IR17&#8242;</span> <br /><span style="color:#a9a9a9;">[</span><span style="color:#add8e6;">CmdletBinding</span><span style="color:#000000;">(</span><span style="color:#000000;">SupportsShouldProcess</span><span style="color:#a9a9a9;">=</span><span style="color:#ff4500;">$true</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span> <br /><span style="color:#00008b;">param</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span> <br /><span style="color:#000000;">    </span><span style="color:#a9a9a9;">[</span><span style="color:#add8e6;">Parameter</span><span style="color:#000000;">(</span><span style="color:#000000;">Position</span><span style="color:#a9a9a9;">=</span><span style="color:#800080;">0</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[string]</span><span style="color:#ff4500;">$IncidentString</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;%&quot;</span><span style="color:#a9a9a9;">,</span> <br /><span style="color:#000000;">    </span><span style="color:#a9a9a9;">[</span><span style="color:#add8e6;">Parameter</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String]</span><span style="color:#ff4500;">$ComputerName</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;localhost&quot;</span><span style="color:#a9a9a9;">,</span> <br /><span style="color:#000000;">    </span><span style="color:#a9a9a9;">[</span><span style="color:#add8e6;">Parameter</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#ff4500;">$Credential</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">)</span> </p>
<p><span style="color:#006400;"># Set up the enviroment</span> <br /><span style="color:#006400;"># this creates the functions that we need to get the incident</span> <br /><span style="color:#006400;"># and sets up the types and methods we need to work</span> <br /><span style="color:#00008b;">BEGIN</span> <br /><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># Save the NameSpace to save some room</span> <br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$NS</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;Microsoft.EnterpriseManagement&quot;</span> <br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$EMGType</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.EnterpriseManagementGroup&quot;</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># if we don&#8217;t have our EnterpriseManagementObject available, we need to </span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># load the assembly</span> <br /><span style="color:#000000;">    </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">!</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Common.EnterpriseManagementObject&quot;</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-as</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;type&quot;</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">        </span><span style="color:#008080;">[reflection.assembly]</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">LoadWithPartialName</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Core&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">|</span><span style="color:#0000ff;">out-null</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span> </p>
<p><span style="color:#000000;">    </span><span style="color:#006400;"># Create the connection to the ManagementGroup and use a </span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># credential if one was offered.</span> <br /><span style="color:#000000;">    </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Credential</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$SETType</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.EnterpriseManagementConnectionSettings&quot;</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$Settings</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$SETType</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$ComputerName</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$Settings</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">UserName</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Credential</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetNetWorkCredential</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">UserName</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$Settings</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Domain</span><span style="color:#000000;">   </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Credential</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetNetWorkCredential</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Domain</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$Settings</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Password</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Credential</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Password</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$EMG</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMGType</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Settings</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">    </span><span style="color:#00008b;">else</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$EMG</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMGType</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$ComputerName</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># Be sure we have a connection</span> <br /><span style="color:#000000;">    </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMG</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-isnot</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.EnterpriseManagementGroup&quot;</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">        </span><span style="color:#00008b;">Throw</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;Could not connect to $ComputerName&quot;</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span> </p>
<p><span style="color:#000000;">    </span><span style="color:#006400;"># Create some variables that we need for our script</span> <br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$DEFAULT</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Common.ObjectQueryOptions&quot;</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-as</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;type&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">Default</span> <br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$EMOT</span><span style="color:#000000;">    </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Common.EnterpriseManagementObject&quot;</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-as</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;type&quot;</span> <br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$IMGMT</span><span style="color:#000000;">   </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$emg</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">EntityObjects</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetType</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$EMOP</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;EnterpriseManagementObjectProjection&quot;</span> <br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$IPT</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;System.WorkItem.Incident.ProjectionType&quot;</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;">####</span> </p>
<p><span style="color:#000000;">    </span><span style="color:#006400;"># this function retrieves incidents from the Server. It uses reflection to</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># call the GetObjectProjectionReader method on the EntityObjects interface</span> <br /><span style="color:#000000;">    </span><span style="color:#00008b;">function</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">Get-SMIncident</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">        </span><span style="color:#00008b;">param</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$CRString</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$Projection</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$emg</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">EntityTypes</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetTypeProjections</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">|</span> <br /><span style="color:#000000;">            </span><span style="color:#0000ff;">?</span><span style="color:#000000;">&#123;</span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">name</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-eq</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$IPT</span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">        </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">!</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Projection</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">            </span><span style="color:#006400;"># FATAL ERROR</span> <br /><span style="color:#000000;">            </span><span style="color:#00008b;">throw</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;Could not retrieve projection type&quot;</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># Create the criteria which will allow us to retrieve the incident</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$CriteriaType</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Common.ObjectProjectionCriteria&quot;</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># This could be more strict, and the user can provide a SQL like Value</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># so you could use &quot;IR17 %&quot; to get IR17 or &quot;IR%&quot; to get all incidents</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># if you want more (or less) strictness, just change the criteria</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$CriteriaString</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">@&#8217; <br />&lt;Criteria xmlns=&quot;http://Microsoft.EnterpriseManagement.Core.Criteria/&quot;&gt; <br />  &lt;Expression&gt; <br />    &lt;SimpleExpression&gt; <br />      &lt;ValueExpressionLeft&gt; <br />        &lt;GenericProperty&gt;DisplayName&lt;/GenericProperty&gt; <br />      &lt;/ValueExpressionLeft&gt; <br />      &lt;Operator&gt;Like&lt;/Operator&gt; <br />      &lt;ValueExpressionRight&gt; <br />        &lt;Value&gt;&#123;0&#125;&lt;/Value&gt; <br />      &lt;/ValueExpressionRight&gt; <br />    &lt;/SimpleExpression&gt; <br />  &lt;/Expression&gt; <br />&lt;/Criteria&gt; <br />&#8216;@</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-f</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$CRString</span> </p>
<p><span style="color:#000000;">        </span><span style="color:#00008b;">try</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">           </span><span style="color:#ff4500;">$criteria</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$CriteriaType</span><span style="color:#000000;"> </span>` <br /><span style="color:#000000;">               </span><span style="color:#ff4500;">$CriteriaString</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$Projection</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$Projection</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ManagementGroup</span> <br /><span style="color:#000000;">           </span><span style="color:#006400;"># use reflection to retrieve the incident by retrieving the </span><br /><span style="color:#000000;">           </span><span style="color:#006400;"># appropriate method and invoking it</span> <br /><span style="color:#000000;">           </span><span style="color:#008080;">[type[]]</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$TYPES</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$&#123;CriteriaType&#125;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Common.ObjectQueryOptions&quot;</span> <br /><span style="color:#000000;">           </span><span style="color:#ff4500;">$ObjectReader</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$IMGMT</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetMethod</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;GetObjectProjectionReader&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$TYPES</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">           </span><span style="color:#ff4500;">$GenericMethod</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$ObjectReader</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">MakeGenericMethod</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$EMOT</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">           </span><span style="color:#008080;">[array]</span><span style="color:#ff4500;">$arguments</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#ff4500;">$criteria</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-as</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;CriteriaType&#125;&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$DEFAULT</span> <br /><span style="color:#000000;">           </span><span style="color:#006400;"># this will return the incident that matches the criteria</span> <br /><span style="color:#000000;">           </span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$GenericMethod</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">invoke</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$emg</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">EntityObjects</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$arguments</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">        </span><span style="color:#00008b;">catch</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">            </span><span style="color:#00008b;">throw</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;Could not retrieve incidents&quot;</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span> </p>
<p><span style="color:#000000;">    </span><span style="color:#006400;"># this function takes an instance of an EnterpriseManagementObject and</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># creates a PS custom object. The custom object uses some of the properties</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># of the standard EnterpriseManagementObject but promotes the va</span> <br /><span style="color:#000000;">    </span><span style="color:#00008b;">function</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">Get-AdaptedEMO</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">        </span><span style="color:#00008b;">param</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMO</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$Type</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMO</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetType</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Name</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$ClassName</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMO</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetLeastDerivedNonAbstractClass</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">name</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$AdaptedObject</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">psobject</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$AdaptedObject</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">PSObject</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">TypeNames</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Insert</span><span style="color:#000000;">(</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;$&#123;Type&#125;#$&#123;ClassName&#125;&quot;</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># some standard properties that should be populated in our custom </span><br /><span style="color:#000000;">        </span><span style="color:#006400;"># object</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$TERMS</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;LastModified&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;LastModifiedBy&quot;</span><span style="color:#a9a9a9;">,</span> <br /><span style="color:#000000;">            </span><span style="color:#8b0000;">&quot;LeastDerivedNonAbstractManagementPackClassId&quot;</span> <br /><span style="color:#000000;">        </span><span style="color:#0000ff;">Add-Member</span><span style="color:#000000;"> </span><span style="color:#000080;">-input</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedObject</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">Id</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMO</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">id</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">guid</span> <br /><span style="color:#000000;">        </span><span style="color:#00008b;">foreach</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$term</span><span style="color:#000000;"> </span><span style="color:#00008b;">in</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$TERMS</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">            </span><span style="color:#0000ff;">Add-Member</span><span style="color:#000000;"> </span><span style="color:#000080;">-input</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedObject</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Term</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMO</span><span style="color:#a9a9a9;">.</span><span style="color:#ff4500;">$Term</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># get the content of values property and add them to our adapted object</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$EMO</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Values</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">%</span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span> <br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$AdaptedObject</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">Add-Member</span><span style="color:#000000;"> </span><span style="color:#000080;">-force</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Type</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Value</span><span style="color:#000000;"> </span> <br /><span style="color:#000000;">            </span><span style="color:#000000;">&#125;</span><span style="color:#000000;"> </span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$AdaptedObject</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span> </p>
<p><span style="color:#000000;">&#125;</span><span style="color:#000000;"> </span><span style="color:#006400;"># END BEGIN</span> </p>
<p><span style="color:#00008b;">END</span> <br /><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># retrieve the incidents based on the incidentstring</span> <br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$IncidentCollection</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">Get-SMIncident</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$IncidentString</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># if we didn&#8217;t find an incident, provide a message and exit</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># one could argue that we should just exit</span> <br /><span style="color:#000000;">    </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$IncidentCollection</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Count</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-eq</span><span style="color:#000000;"> </span><span style="color:#800080;">0</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$msg</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;ERROR: No incidents match &#8216;$IncidentString&#8217;.&quot;</span> <br /><span style="color:#000000;">        </span><span style="color:#0000ff;">Write-Host</span><span style="color:#000000;"> </span><span style="color:#000080;">-fore</span><span style="color:#000000;"> </span><span style="color:#ff4500;">red</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$b</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$msg</span> <br /><span style="color:#000000;">        </span><span style="color:#0000ff;">Write-Host</span><span style="color:#000000;"> </span><span style="color:#000080;">-fore</span><span style="color:#000000;"> </span><span style="color:#ff4500;">red</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$b</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;Exiting&quot;</span> <br /><span style="color:#000000;">        </span><span style="color:#00008b;">exit</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># for each incident in the incidents that matched the criteria</span> <br /><span style="color:#000000;">    </span><span style="color:#006400;"># construct a custom PSObject and emit</span> <br /><span style="color:#000000;">    </span><span style="color:#00008b;">foreach</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$Incident</span><span style="color:#000000;"> </span><span style="color:#00008b;">in</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$IncidentCollection</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># Create the custom object to hold the incident information</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">psobject</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">PSObject</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">TypeNames</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Insert</span><span style="color:#000000;">(</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;$&#123;EMOP&#125;#$&#123;IPT&#125;&quot;</span><span style="color:#000000;">)</span> </p>
<p><span style="color:#000000;">        </span><span style="color:#006400;"># Construct an adapted object for the main object of the incident</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$object</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">Get-AdaptedEMO</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Incident</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Object</span> <br /><span style="color:#000000;">        </span><span style="color:#0000ff;">add-member</span><span style="color:#000000;"> </span><span style="color:#000080;">-input</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">Object</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$object</span> </p>
<p><span style="color:#000000;">        </span><span style="color:#006400;"># for each one of the component parts of the incident, add an </span><br /><span style="color:#000000;">        </span><span style="color:#006400;"># adapted object to the adapted incident</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$keys</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Incident</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">%</span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">key</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">name</span><span style="color:#000000;"> </span><span style="color:#000000;">&#125;</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">sort</span><span style="color:#000000;"> </span><span style="color:#000080;">-uniq</span> <br /><span style="color:#000000;">        </span><span style="color:#00008b;">foreach</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$key</span><span style="color:#000000;"> </span><span style="color:#00008b;">in</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$keys</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">            </span><span style="color:#0000ff;">Add-Member</span><span style="color:#000000;"> </span><span style="color:#000080;">-Input</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$key</span><span style="color:#000000;"> </span><span style="color:#000000;">@(</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$Incident</span><span style="color:#a9a9a9;">[</span><span style="color:#ff4500;">$key</span><span style="color:#a9a9a9;">]</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">|</span><span style="color:#000000;"> </span><span style="color:#0000ff;">%</span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span> <br /><span style="color:#000000;">                </span><span style="color:#ff4500;">$object</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">Get-AdaptedEMO</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Object</span> <br /><span style="color:#000000;">                </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#a9a9a9;">.</span><span style="color:#ff4500;">$key</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">+=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$object</span> <br /><span style="color:#000000;">                </span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span> </p>
<p><span style="color:#000000;">        </span><span style="color:#006400;"># Add some members to the adapted incident</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># this will aid in presentation and filtering</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># first the simple promotions</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$Terms</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;Id&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Title&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Description&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;DisplayName&quot;</span><span style="color:#a9a9a9;">,</span> <br /><span style="color:#000000;">            </span><span style="color:#8b0000;">&quot;Priority&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;CreatedDate&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;LastModified&quot;</span> <br /><span style="color:#000000;">        </span><span style="color:#00008b;">foreach</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$term</span><span style="color:#000000;"> </span><span style="color:#00008b;">in</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$Terms</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#123;</span> <br /><span style="color:#000000;">            </span><span style="color:#0000ff;">Add-Member</span><span style="color:#000000;"> </span><span style="color:#000080;">-input</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$term</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Object</span><span style="color:#a9a9a9;">.</span><span style="color:#ff4500;">$term</span> <br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># more complex promotions</span> <br /><span style="color:#000000;">        </span><span style="color:#0000ff;">Add-Member</span><span style="color:#000000;"> </span><span style="color:#000080;">-input</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">Status</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Object</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Status</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">DisplayName</span> <br /><span style="color:#000000;">        </span><span style="color:#0000ff;">Add-Member</span><span style="color:#000000;"> </span><span style="color:#000080;">-input</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">AssignedTo</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">AssignedWorkItem</span><span style="color:#a9a9a9;">[</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">]</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">DisplayName</span> <br /><span style="color:#000000;">        </span><span style="color:#0000ff;">Add-Member</span><span style="color:#000000;"> </span><span style="color:#000080;">-input</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">NoteProperty</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">AffectedUser</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$AdaptedIncident</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">RequestedWorkItem</span><span style="color:#a9a9a9;">[</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">]</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">DisplayName</span> <br /><span style="color:#000000;">        </span><span style="color:#006400;"># emit the object</span> <br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$AdaptedIncident</span> <br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span> <br /><span style="color:#000000;">&#125;</span><span style="color:#000000;"> </span><span style="color:#006400;"># END </span></div>
</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<p>When we run the script, we provide an incident ID to reduce the amount of data returned from the Data Access Service.</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; ./get-incident IR17%

Object                 : @&#123;LastModified=1/28/2010 1:30:24 AM; LastModifiedBy=7431e155-3d9e-4724-895e-c03ba951a352; Leas
                         tDerivedNonAbstractManagementPackClassId=a604b942-4c7b-2fb2-28dc-61dc6f465c68; TargetResolutio
                         nTime=; Escalated=False; Source=IncidentSourceEnum.Console; Status=IncidentStatusEnum.Active;
                         ResolutionDescription=; NeedsKnowledgeArticle=False; TierQueue=; HasCreatedKnowledgeArticle=Fa
                         lse; LastModifiedSource=IncidentSourceEnum.Console; Classification=IncidentClassificationEnum.
                         Hardware; ResolutionCategory=; Priority=9; Impact=System.WorkItem.TroubleTicket.ImpactEnum.Hig
                         h; Urgency=System.WorkItem.TroubleTicket.UrgencyEnum.High; ClosedDate=; ResolvedDate=; Id=IR17
                         ; Title=my computer is brokeked; Description=it don't work; ContactMethod=; CreatedDate=1/18/2
                         010 10:58:23 PM; ScheduledStartDate=; ScheduledEndDate=; ActualStartDate=; ActualEndDate=; Dis
                         playName=IR17 - my computer is brokeked&#125;
AppliesToTroubleTicket : &#123;@&#123;LastModified=1/28/2010 1:02:44 AM; LastModifiedBy=7431e155-3d9e-4724-895e-c03ba951a352; Lea
                         stDerivedNonAbstractManagementPackClassId=dbb6a632-0a7e-cef8-1fc9-405d5cd4d911; ActionType=Sys
                         tem.WorkItem.ActionLogEnum.RecordReopened; Title=Action log from 01/27/2010 17:02:44; Descript
                         setStatus=; Notes=; DisplayName=Domain Admninistrator&#125;&#125;
. . .
RequestedWorkItem      : &#123;@&#123;Id=642feed0-7e9a-b516-81cc-7f94be6bce91; LastModified=11/18/2009 5:30:17 PM; LastModifiedBy
                         =8bb08d83-64f1-4230-a8c9-e022beae2819; LeastDerivedNonAbstractManagementPackClassId=eca3c52a-f
                         273-5cdc-f165-3eb95a2b26cf; Domain=WOODGROVE; UserName=blesh; DistinguishedName=CN=Bruce Lesh,
                         CN=Users,DC=woodgrove,DC=com; SID=S-1-5-21-2548544548-3952215810-4123597018-1129; FQDN=woodgro
                         ve.com; UPN=blesh@woodgrove.com; FirstName=Bruce; Initials=; LastName=Lesh; Company=; Departme
                         nt=; Office=; Title=; EmployeeId=; StreetAddress=; City=; State=; Zip=; Country=; BusinessPhon
                         e=; BusinessPhone2=; HomePhone=; HomePhone2=; Fax=; Mobile=; Pager=; ObjectStatus=System.Confi
                         gItem.ObjectStatusEnum.Active; AssetStatus=; Notes=; DisplayName=Bruce Lesh&#125;&#125;
Id                     : IR17
Title                  : my computer is brokeked
Description            : it don't work
DisplayName            : IR17 - my computer is brokeked
Priority               : 9
CreatedDate            : 1/18/2010 10:58:23 PM
LastModified           : 1/28/2010 1:30:24 AM
Status                 : Active
AssignedTo             : Domain Admninistrator
AffectedUser           : Bruce Lesh
</pre>
</div>
<p>These results look pretty bad, it’s difficult to see what’s really important and what isn’t. However we can create a table view which can mimic the view that we see in the console. Here’s the formatting does it.</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">. . .
    &lt;View&gt;
      &lt;Name&gt;IncidentView&lt;/Name&gt;
      &lt;ViewSelectedBy&gt;
        &lt;TypeName&gt;EnterpriseManagementObjectProjection#System.WorkItem.Incident.ProjectionType&lt;/TypeName&gt;
      &lt;/ViewSelectedBy&gt;
      &lt;TableControl&gt;
        &lt;AutoSize /&gt;
        &lt;TableHeaders&gt;
          &lt;TableColumnHeader&gt;&lt;Label&gt;Id&lt;/Label&gt;&lt;/TableColumnHeader&gt;
          &lt;TableColumnHeader&gt;&lt;Label&gt;Title&lt;/Label&gt;&lt;/TableColumnHeader&gt;
          &lt;TableColumnHeader&gt;&lt;Label&gt;AssignedTo&lt;/Label&gt;&lt;/TableColumnHeader&gt;
          &lt;TableColumnHeader&gt;&lt;Label&gt;Status&lt;/Label&gt;&lt;/TableColumnHeader&gt;
          &lt;TableColumnHeader&gt;&lt;Label&gt;Priority&lt;/Label&gt;&lt;Alignment&gt;Right&lt;/Alignment&gt;&lt;/TableColumnHeader&gt;
          &lt;TableColumnHeader&gt;&lt;Label&gt;AffectedUser&lt;/Label&gt;&lt;/TableColumnHeader&gt;
          &lt;TableColumnHeader&gt;&lt;Label&gt;LastModified&lt;/Label&gt;&lt;/TableColumnHeader&gt;
        &lt;/TableHeaders&gt;
        &lt;TableRowEntries&gt;
          &lt;TableRowEntry&gt;
           &lt;TableColumnItems&gt;
              &lt;TableColumnItem&gt;&lt;PropertyName&gt;Id&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;
              &lt;TableColumnItem&gt;&lt;PropertyName&gt;Title&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;
              &lt;TableColumnItem&gt;&lt;PropertyName&gt;AssignedTo&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;
              &lt;TableColumnItem&gt;&lt;PropertyName&gt;Status&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;
              &lt;TableColumnItem&gt;&lt;PropertyName&gt;Priority&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;
              &lt;TableColumnItem&gt;&lt;PropertyName&gt;AffectedUser&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;
              &lt;TableColumnItem&gt;&lt;PropertyName&gt;LastModified&lt;/PropertyName&gt;&lt;/TableColumnItem&gt;
           &lt;/TableColumnItems&gt;
          &lt;/TableRowEntry&gt;
        &lt;/TableRowEntries&gt;
      &lt;/TableControl&gt;
    &lt;/View&gt;
. . .
</pre>
</pre>
</div>
</p>
<p>update the format directives with update-formatdata, and voila!</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; update-formatdata GetIncident.format.ps1xml
PS&gt; ./get-incident IR17%

Id   Title                   AssignedTo            Status Priority AffectedUser LastModified
--   -----                   ----------            ------ -------- ------------ ------------
IR17 my computer is brokeked Domain Admninistrator Active        9 Bruce Lesh   1/28/2010 1:30:24 AM

PS&gt; ./get-incident IR%

Id   Title                   AssignedTo            Status Priority AffectedUser  LastModified
--   -----                   ----------            ------ -------- ------------  ------------
IR17 my computer is brokeked Domain Admninistrator Active        9 Bruce Lesh    1/28/2010 1:30:24 AM
IR2  email is brokoken       Domain Admninistrator Closed        9 Al Young      12/15/2009 11:41:57 PM
IR13 more busted 2           Domain Admninistrator Closed        9 Carlos Garcia 1/28/2010 12:55:28 AM
IR15 busted 3                Domain Admninistrator Closed        9 Greg Adams    1/5/2010 6:50:11 PM</pre>
</pre>
</div>
<p>that looks pretty good!</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jtruher3.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jtruher3.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jtruher3.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jtruher3.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jtruher3.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jtruher3.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jtruher3.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jtruher3.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jtruher3.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jtruher3.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jtruher3.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jtruher3.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jtruher3.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jtruher3.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=7&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jtruher3.wordpress.com/2010/02/05/getting-service-manager-incidents-with-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/20349e038c0f35188b594c68f20636bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jtruher3</media:title>
		</media:content>

		<media:content url="http://yxxxiq.blu.livefilestore.com/y1pnx1JggTl_po23rT_PGAkMk9TJMFeW3cXO-13eAzDBYylX-Inu3KyOCj7h1Asg1b78G8Mb1qQPoBjYFaKY5abjbz7Xz3zygbi?PARTNER=WRITER" medium="image" />
	</item>
		<item>
		<title>Batch Operations in Service Manager 2010 with PowerShell – Removing Instances</title>
		<link>http://jtruher3.wordpress.com/2009/10/22/batch-operations-in-service-manager-2010-with-powershell-%e2%80%93-removing-instances/</link>
		<comments>http://jtruher3.wordpress.com/2009/10/22/batch-operations-in-service-manager-2010-with-powershell-%e2%80%93-removing-instances/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 18:33:14 +0000</pubDate>
		<dc:creator>jtruher3</dc:creator>
				<category><![CDATA[ServiceManager]]></category>

		<guid isPermaLink="false">http://jtruher3.wordpress.com/2009/10/22/batch-operations-in-service-manager-2010-with-powershell-%e2%80%93-removing-instances</guid>
		<description><![CDATA[Sometimes, when I am developing a demo for Service Manager, I wind up creating a lot of Service Requests or Incidents when I’m trying to get the demo just right. However, after I’ve gotten everything working just like I want and then I give the demo, I don’t really want to have all those earlier [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=8&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="msgcns!7143DA6E51A2628D!843" class="bvMsg">
<p>Sometimes, when I am developing a demo for Service Manager, I wind up creating a lot of Service Requests or Incidents when I’m trying to get the demo just right. However, after I’ve gotten everything working just like I want and then I give the demo, I don’t really want to have all those earlier things visible because they get in the way of the what I’m trying to show. The Service Manager 2010 provides a way to removing instances from the console, and I could use that, but I like to script everything so I want to create a script instead of using the UI. With this script, I can use this to remove <em>any instance in the CMDB</em>, including Incidents and Service Requests.  </p>
<p>Our programming interfaces provide a way to remove instances and I’ve written my script to work a couple of ways:</p>
<ul>
<li>If you provide the script with  ClassName parameter, the script will remove every instance of that class!
<li>If you pipe an EnterpriseManagementObject at the script, the script will remove that instance</li>
</ul>
<p>These are pretty big hammers, so I’ve made sure that you can use <font size="2" face="Lucida Console"><strong>–WhatIf</strong></font> and I’ve also set ConfirmImpact as High which will ask for confirmation even if you don’t specify –confirm. My last warning is that you should not put this script <em>anywhere </em>near your production machines. It will remove the data <em>forever, </em>so be sure you are careful!!</p>
<p>I think the most interesting bit of the script is on line 21. This is where an IncrementalDiscoveryData object is created. The IncrementalDiscoveryData object allows you to deal with instances in bulk.  I can use this object to remove instances then remove them all by the single call to Commit in line 73.  The code between lines 29 and 35 represent the code that’s needed to call our generic methods, the script uses reflection to build the generic method and then call it.</p>
<p>The PROCESS block starting on line 50 handles the case when you pipe objects to the script. It first checks to be sure that it’s an EnterpriseManagementObject, and if so, adds the object to the IncrementalDiscoveryData collection which will be used in the END block. Rather than wrapping the call to Commit in another ShouldProcess block, I just check to be sure I have objects to remove. If there are, I make the Commit call. I don’t like it when my scripts ask me “Do you really want to do this” after I’ve already answered it once.</p>
<p>This script is a PowerShell version 2.0 script (as seen in line 1). This way I can take advantage of the ConfirmImpact and the other PowerShell 2.0 goodies.</p>
<table border="0" cellspacing="0" cellpadding="5" width="750">
<tbody>
<tr>
<td valign="top" width="40">
<div style="font-family:lucida console;background:#cecece;font-size:9pt;padding:5px;">001<br />002<br />003<br />004<br />005<br />006<br />007<br />008<br />009<br />010<br />011<br />012<br />013<br />014<br />015<br />016<br />017<br />018<br />019<br />020<br />021<br />022<br />023<br />024<br />025<br />026<br />027<br />028<br />029<br />030<br />031<br />032<br />033<br />034<br />035<br />036<br />037<br />038<br />039<br />040<br />041<br />042<br />043<br />044<br />045<br />046<br />047<br />048<br />049<br />050<br />051<br />052<br />053<br />054<br />055<br />056<br />057<br />058<br />059<br />060<br />061<br />062<br />063<br />064<br />065<br />066<br />067<br />068<br />069<br />070<br />071<br />072<br />073<br />074<br />075<br />076<br />077<br />078<br />079<br />080<br />081<br />082<br />083<br />084<br />085<br />086<br />087<br />088<br />089<br />090<br />091<br />092<br />093<br />094<br />095<br />096<br />097<br />098<br />099<br />100<br />101<br />102<br />103<br />104<br />105<br />106</div>
</td>
<td valign="top" width="710">
<div style="font-family:lucida console;background:#fcfcfc;font-size:9pt;padding:5px;"><span style="color:#006400;">#requires -version 2.0</span><br /><span style="color:#a9a9a9;">[</span><span style="color:#add8e6;">CmdletBinding</span><span style="color:#000000;">(</span><span style="color:#000000;">SupportsShouldProcess</span><span style="color:#a9a9a9;">=</span><span style="color:#ff4500;">$true</span><span style="color:#a9a9a9;">,</span><span style="color:#000000;"> </span><span style="color:#000000;">ConfirmImpact</span><span style="color:#a9a9a9;">=</span><span style="color:#8b0000;">&quot;High&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><br /><span style="color:#00008b;">param</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">    </span><span style="color:#a9a9a9;">[</span><span style="color:#add8e6;">Parameter</span><span style="color:#000000;">(</span><span style="color:#000000;">Position</span><span style="color:#a9a9a9;">=</span><span style="color:#800080;">0</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$classname</span><span style="color:#a9a9a9;">,</span><br /><span style="color:#000000;">    </span><span style="color:#a9a9a9;">[</span><span style="color:#add8e6;">Parameter</span><span style="color:#000000;">(</span><span style="color:#000000;">ValueFromPipeline</span><span style="color:#a9a9a9;">=</span><span style="color:#ff4500;">$true</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#ff4500;">$EMO</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">)</span><br /><span style="color:#00008b;">BEGIN</span><br /><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># oh for a way to specify namespaces</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$NS</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;Microsoft.EnterpriseManagement&quot;</span><br /><span style="color:#000000;">    </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">!</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Common.EnterpriseManagementObject&quot;</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-as</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;type&quot;</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">        </span><span style="color:#008080;">[reflection.assembly]</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">LoadWithPartialName</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Core&quot;</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">     </span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$LFX</span><span style="color:#000000;">          </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;ConnectorFramework&quot;</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$DEFAULT</span><span style="color:#000000;">      </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Common.ObjectQueryOptions&quot;</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-as</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;type&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">Default</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$EMOT</span><span style="color:#000000;">         </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Common.EnterpriseManagementObject&quot;</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-as</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;type&quot;</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$EMG</span><span style="color:#000000;">          </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.EnterpriseManagementGroup&quot;</span><span style="color:#000000;"> </span><span style="color:#8a2be2;">localhost</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$IDD</span><span style="color:#000000;">          </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new-object</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.$&#123;LFX&#125;.incrementaldiscoverydata&quot;</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$guid</span><span style="color:#000000;">         </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMG</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ConnectorFramework</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetDefaultConnectorId</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">guid</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$SDKConnector</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMG</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ConnectorFramework</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetConnector</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$guid</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">    </span><span style="color:#ff4500;">$REMOVECOUNT</span><span style="color:#000000;">  </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">0</span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># only go through this process if you got a classname and are </span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># going to remove all instances of that class</span><br /><span style="color:#000000;">    </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$classname</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$IMgmt</span><span style="color:#000000;">    </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMG</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">EntityObjects</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetType</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$class</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMG</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">EntityTypes</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">GetClasses</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">|</span><span style="color:#0000ff;">?</span><span style="color:#000000;">&#123;</span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">name</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-eq</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$classname</span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">        </span><span style="color:#008080;">[array]</span><span style="color:#ff4500;">$arguments</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#ff4500;">$class</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-as</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Configuration.ManagementPackClass&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$DEFAULT</span><br /><span style="color:#000000;">        </span><span style="color:#008080;">[type[]]</span><span style="color:#ff4500;">$TYPES</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Configuration.ManagementPackClass&quot;</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-as</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;type&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">,</span><br /><span style="color:#000000;">                     </span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Common.ObjectQueryOptions&quot;</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-as</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;type&quot;</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$ObjectReader</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$IMgmt</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">getmethod</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;GetObjectReader&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$TYPES</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$GenericMethod</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$ObjectReader</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">MakeGenericMethod</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$EMOT</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">        </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">!</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$class</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><span style="color:#000000;"> </span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span><span style="color:#00008b;">throw</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;no class $classname&quot;</span><span style="color:#000000;"> </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">        </span><span style="color:#006400;"># GET THE OBJECTS</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$SMObjects</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">=</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$GenericMethod</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">invoke</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$EMG</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">EntityObjects</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$arguments</span><span style="color:#000000;">)</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">        </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">!</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$SMObjects</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><span style="color:#000000;"> </span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;No objects to remove&quot;</span><span style="color:#000000;">;</span><span style="color:#000000;"> </span><span style="color:#00008b;">exit</span><span style="color:#000000;"> </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$SMObjects</span><span style="color:#a9a9a9;">|</span><span style="color:#0000ff;">%</span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">            </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$PSCmdlet</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ShouldProcess</span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">displayname</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><br /><span style="color:#000000;">            </span><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">                </span><span style="color:#ff4500;">$REMOVECOUNT</span><span style="color:#a9a9a9;">++</span><br /><span style="color:#000000;">                </span><span style="color:#ff4500;">$IDD</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Remove</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$_</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">            </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span><span style="color:#000000;"> </span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">&#125;</span></p>
<p><span style="color:#00008b;">PROCESS</span><br /><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">    </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMO</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">-is</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$&#123;NS&#125;.Common.EnterpriseManagementObject&quot;</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">        </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$PSCmdlet</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ShouldProcess</span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMO</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">displayname</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><br /><span style="color:#000000;">        </span><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$REMOVECOUNT</span><span style="color:#a9a9a9;">++</span><br /><span style="color:#000000;">            </span><span style="color:#ff4500;">$IDD</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Remove</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$EMO</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">        </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">    </span><span style="color:#00008b;">elseif</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#a9a9a9;">!</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$EMO</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><span style="color:#000000;"> </span><span style="color:#000000;">&#123;</span><span style="color:#000000;"> </span><span style="color:#000000;">;</span><span style="color:#000000;"> </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">    </span><span style="color:#00008b;">else</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">        </span><span style="color:#0000ff;">Write-Error</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;$_ is not an EnterpriseManagementObject, skipping&quot;</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">&#125;</span></p>
<p><span style="color:#00008b;">END</span><br /><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">    </span><span style="color:#006400;"># only actually call this if there are any to delete</span><br /><span style="color:#000000;">    </span><span style="color:#00008b;">if</span><span style="color:#000000;"> </span><span style="color:#000000;">(</span><span style="color:#000000;"> </span><span style="color:#ff4500;">$REMOVECOUNT</span><span style="color:#000000;"> </span><span style="color:#000000;">)</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#123;</span><br /><span style="color:#000000;">        </span><span style="color:#0000ff;">Write-Verbose</span><span style="color:#000000;"> </span><span style="color:#8b0000;">&quot;Committing Changes&quot;</span><br /><span style="color:#000000;">        </span><span style="color:#ff4500;">$IDD</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Commit</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$&#123;SDKConnector&#125;</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">    </span><span style="color:#000000;">&#125;</span><br /><span style="color:#000000;">&#125;</span></p>
<p><span style="color:#006400;">&lt;#<br />.SYNOPSIS<br />    Remove an instance from the Service Manager 2010 CMDB<br />.DESCRIPTION<br />    The cmdlet removes instances from the Service Manager CMDB.<br />    If the classname parameter is provided, every instance will<br />    be removed from the CMDB.<br />    Optionally, instances may be piped to this cmdlet in which case<br />    only those instances will be removed.<br />.PARAMETER ClassName<br />    A Service Manager 2010 class name<br />.PARAMETER EMO<br />    An instance to be removed from the Service Mangaer 2010 CMDB<br />.EXAMPLE<br />remove-smobject -classname Microsoft.Windows.Computer<br />Removes all instances of Microsoft.Windows.Computer from the <br />Service Manager 2010 CMDB<br />.EXAMPLE<br />get-smobject Microsoft.Windows.Computer | ?&#123;$_.displayname -match &quot;Computer00&quot;&#125;|remove-smobject<br />Removes all instances of Microsoft.Windows.Computer from the <br />Service Manager 2010 CMDB where the displayname matches &quot;Computer00&quot;<br />.INPUTS<br />    Output from get-smobject<br />    Any EnterpriseManagementObject<br />.OUTPUTS<br />    None<br />.LINK<br />    get-smobject-ManagementPack<br />    get-smclass<br />#&gt;</span> </div>
</td>
</tr>
</tbody>
</table>
<p>Here’s an example of removing every Microsoft.Windows.Computer from Service Manager (I’m not actually going to do this, so I’m using –Whatif). If you need a reminder, Get-SmObject.ps1 was a blog posting <a href="http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!794.entry" target="_blank">here</a>.</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; ./get-smobject microsoft.windows.computer|./remove-smobject -whatif
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer028&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer008&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;computer1.contoso.com&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer027&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer001&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;WIN-752HJBSX24M.woodgrove.com&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer002&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer024&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer030&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer007&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer023&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer025&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer026&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer003&quot;.
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer029&quot;.</pre>
</div>
<p>If I want to remove one computer, I can just filter for what I want.</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; ./get-smobject microsoft.windows.computer|?&#123;$_.displayname -match &quot;Computer028&quot;&#125;|
&gt;&gt; ./remove-smobject -whatif
What if: Performing operation &quot;remove-smobject.ps1&quot; on Target &quot;Computer028&quot;.</pre>
</pre>
</div>
<p>This is what it will look like when you really remove it!</p>
<p><a href="http://jtruher3.files.wordpress.com/2009/10/bb41f57e7849c65a0416a69ca6f85369.png" rel="WLPP"><img border="0" src="http://jtruher3.files.wordpress.com/2009/10/bb41f57e7849c65a0416a69ca6f85369.png?w=300" /></a> </p>
<p>Computer028 is gone! Notice that this is really where PowerShell provides lots of value, the interaction to confirm the removal is done with the Cmdlet attribute in line 2 – ConfirmImpact=”High”, that plus the $PSCmdlet.ShouldProcess in lines 41 and 54 make it really easy to write scripts that won’t shoot me in the foot!</p>
</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jtruher3.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jtruher3.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jtruher3.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jtruher3.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jtruher3.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jtruher3.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jtruher3.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jtruher3.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jtruher3.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jtruher3.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jtruher3.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jtruher3.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jtruher3.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jtruher3.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=8&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jtruher3.wordpress.com/2009/10/22/batch-operations-in-service-manager-2010-with-powershell-%e2%80%93-removing-instances/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/20349e038c0f35188b594c68f20636bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jtruher3</media:title>
		</media:content>

		<media:content url="http://jtruher3.files.wordpress.com/2009/10/bb41f57e7849c65a0416a69ca6f85369.png?w=300" medium="image" />
	</item>
		<item>
		<title>Service Manager and the PowerShell 1 liner</title>
		<link>http://jtruher3.wordpress.com/2009/10/02/service-manager-and-the-powershell-1-liner/</link>
		<comments>http://jtruher3.wordpress.com/2009/10/02/service-manager-and-the-powershell-1-liner/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 00:39:13 +0000</pubDate>
		<dc:creator>jtruher3</dc:creator>
				<category><![CDATA[ServiceManager]]></category>

		<guid isPermaLink="false">http://jtruher3.wordpress.com/2009/10/02/service-manager-and-the-powershell-1-liner</guid>
		<description><![CDATA[I’ve written a number of fairly complicated scripts for Service Manager over the last few months, and while talking to a team-mate about something he wanted to do, it looked like it was just 1-line PowerShell script. That got me thinking about what other things in Service Manager could be handled by a really simple [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=9&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="msgcns!7143DA6E51A2628D!833" class="bvMsg">
<p>I’ve written a number of fairly complicated scripts for Service Manager over the last few months, and while talking to a team-mate about something he wanted to do, it looked like it was just 1-line PowerShell script. That got me thinking about what other things in Service Manager could be handled by a really simple (say less than 5 lines) of PowerShell. The list is good sized, so I thought it would be good if I shared them. </p>
<p>The problem we had at hand was how I could help one our development partners figure out in which management pack a particular class resides. It turns out it was 3 lines of script to find out.</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; [reflection.assembly]::LoadWithPartialName(&quot;Microsoft.EnterpriseManagement.Core&quot;)

GAC    Version        Location
---    -------        --------
True   v2.0.50727     C:\Windows\assembly\GAC_MSIL\Microsoft.EnterpriseManagement.Core\7.0.5000.0__31bf3856ad364e35\...

PS&gt; $EMG = new-object Microsoft.EnterpriseManagement.EnterpriseManagementGroup localhost
PS&gt; $EMG.EntityTypes.GetClasses()|ft name,&#123;$_.GetManagementPack().Name&#125;

Name                                                        $_.GetManagementPack().Name
----                                                        ---------------------------
System.Entity                                               System.Library
System.Collections                                          System.Library
System.ConfigItem                                           System.Library
System.LogicalEntity                                        System.Library
. . .</pre>
</pre>
</pre>
</div>
<p>the business end of the script is just the last line. The first two lines are just what I need to get access to the Service Manager Data Access Service. I do this so much that put those two lines in my $profile. </p>
<p>After that, it’s just a matter of adding a filter to find out the actual class of interest.</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $EMG.EntityTypes.GetClasses()|?&#123;$_.name -match &quot;System.Knowledge.Article&quot;&#125;|ft name,&#123;$_.GetManagementPack().Name&#125;

Name                                                        $_.GetManagementPack().Name
----                                                        ---------------------------
System.Knowledge.Article                                    System.Knowledge.Library</pre>
</div>
<p>Now, the label for the second column may not pretty, but I’m not fussed about that, I’ve got the data that I need. The next thing I needed to figure out was the properties of this class. That’s another one liner:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.entitytypes.GetClasses()|?&#123;$_.name -match &quot;System.Knowledge.Article&quot;&#125;|
&gt;&gt; select -expand propertycollection|ft name,key,type -au
&gt;&gt;

Name                Key     Type
----                ---     ----
ArticleType       False     enum
ArticleTemplate   False   string
ArticleOwner      False   string
Category          False     enum
Comments          False   string
CreatedDate       False datetime
CreatedBy         False   string
PrimaryLocaleID   False      int
Status            False     enum
Tag               False     enum
VendorArticleID   False   string
Title             False   string
Abstract          False   string
Keywords          False   string
ArticleId          True   string
EndUserContent    False   binary
AnalystContent    False   binary
ExternalURLSource False   string
ExternalURL       False   string</pre>
</div>
<p>(ok, so it’s a long line, but it’s still a single pipeline) </p>
<p>This doesn’t quite tell the whole story, because if I wanted to create one of these classes, I may have more properties available to me (based on the base classes for the class I want). That’s just *2* lines:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $class = $emg.entitytypes.GetClasses()|?&#123;$_.name -match &quot;System.Knowledge.Article&quot;&#125;
PS&gt; (new-object microsoft.enterprisemanagement.common.CreatableEnterpriseManagementObject $emg,$class).GetProperties()|
&gt;&gt; ft name,key,type -au
&gt;&gt;

Name                Key     Type
----                ---     ----
ArticleType       False     enum
ArticleTemplate   False   string
ArticleOwner      False   string
Category          False     enum
Comments          False   string
CreatedDate       False datetime
CreatedBy         False   string
PrimaryLocaleID   False      int
Status            False     enum
Tag               False     enum
VendorArticleID   False   string
Title             False   string
Abstract          False   string
Keywords          False   string
ArticleId          True   string
EndUserContent    False   binary
AnalystContent    False   binary
ExternalURLSource False   string
ExternalURL       False   string
ObjectStatus      False     enum
AssetStatus       False     enum
Notes             False richtext
DisplayName       False   string</pre>
</div>
<p>I save the class and then use it to create the object I want with new-object.</p>
</p>
<p>Sometimes, I need to know which management pack an enumeration is in. Another 1 liner:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.EntityTypes.GetEnumerations()|?&#123;$_.name -match &quot;high&quot;&#125;|ft name,&#123;$_.getmanagementpack().name&#125; -au

Name                                                    $_.getmanagementpack().name
----                                                    ---------------------------
System.WorkItem.TroubleTicket.ImpactEnum.High           System.WorkItem.Library
System.WorkItem.TroubleTicket.UrgencyEnum.High          System.WorkItem.Library
System.ServiceManagement.ServicePriority.High           ServiceManager.ServiceMaps.Configuration
IncidentResolutionCategoryEnum.FixedByHigherTierSupport ServiceManager.IncidentManagement.Configuration
ChangePriorityEnum.High                                 ServiceManager.ChangeManagement.Configuration
ChangeRiskEnum.High                                     ServiceManager.ChangeManagement.Configuration
ActivityPriorityEnum.High                               ServiceManager.ActivityManagement.Configuration</pre>
</div>
<p>One of my early examples for retrieving management packs. That’s a 1 liner:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.ManagementPacks.GetManagementPacks()|ft Sealed,Version,Name
Sealed Version    Name
------ -------    ----
 False 7.0.5228.0 Microsoft.SystemCenter.ServiceManager.Connector.Configuration
  True 7.0.5228.0 Microsoft.SystemCenter.Internal
  True 7.0.5228.0 ServiceManager.Reporting.Help
  True 7.0.5228.0 Microsoft.SystemCenter.Report.Library
  True 7.0.5228.0 ServiceManager.LinkingFramework.Library
  True 7.0.5228.0 System.ApplicationLog.Library
  True 7.0.5228.0 ServiceManager.IncidentManagement.Library.Datawarehouse
  True 7.0.5228.0 Microsoft.EnterpriseManagement.ServiceManager.UI.Console
  True 7.0.5228.0 ServiceManager.ActivityManagement.Library.Datawarehouse
  True 7.0.5228.0 ServiceManager.ChangeManagement.Library
  True 7.0.5228.0 ServiceManager.IncidentManagement.Report.Library
  True 7.0.5228.0 ServiceManager.ChangeManagement.Report.Library
. . .</pre>
</pre>
</pre>
</div>
<p>What if I wanted to remove a management pack? Before I do, I had better find out whether it’s possible, as if other Management Packs depend on the one I want to remove. So I need to find the dependent management packs &#8211; 2 lines!</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $crLib = $emg.ManagementPacks.GetManagementPacks()|?&#123;$_.name -eq &quot;System.WorkItem.ChangeRequest.Library&quot;&#125;
PS&gt; $emg.ManagementPacks.GetDependentManagementPacks($crLib)|ft sealed,version,name -au

Sealed Version    Name
------ -------    ----
  True 7.0.5228.0 ServiceManager.ActivityManagement.Library.Datawarehouse
  True 7.0.5228.0 ServiceManager.ChangeManagement.Library
  True 7.0.5228.0 ServiceManager.ChangeManagement.Report.Library
  True 7.0.5228.0 ServiceManager.ServiceMaps.Library
  True 7.0.5228.0 ServiceManager.ChangeManagement.Library.Datawarehouse
 False 7.0.5228.0 ServiceManager.ConfigurationManagement.Configuration
  True 7.0.5228.0 ServiceManager.ChangeManagement.Help
  True 7.0.5228.0 Microsoft.SystemCenter.ServiceManager.Portal
 False 7.0.5228.0 ServiceManager.ChangeManagement.Configuration</pre>
</div>
<p>In this case, I won’t be able to remove the ChangeRequest Library, because of all the dependencies, but if I have a management pack that is not needed by other management packs, removing the management pack is another one-liner:</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.ManagementPacks.GetManagementPacks()|?&#123;$_.name -eq &quot;MPToRemove&quot;&#125;|
&gt;&gt; %&#123; $emg.ManagementPacks.UninstallManagementPack($_) &#125;
&gt;&gt;</pre>
</div>
<p>Perhaps I want to find out how much localization I need to do. To understand how much work I will need to do, I should find out how many lines of text I need to localize.  How do I find out how many different English display strings are stored in my management packs? 1 line!</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; ($emg.LanguagePacks.getlanguagepacks()|?&#123;$_.name -eq &quot;ENU&quot;&#125;|select-object -Expand DisplayStringCollection).count
6456</pre>
</div>
<p>and if I wanted to know the count of my display strings for each language? 1 line!</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.LanguagePacks.getlanguagepacks()|select-object -ExpandProperty DisplayStringCollection |
&gt;&gt; Group-Object LanguageCode|format-table Count,Name -au
&gt;&gt;

Count Name
----- ----
 6456 ENU
 6455 DEU
 6282 JPN
</pre>
</div>
<p>and what if I wanted to see the actual English display strings from the System.Library management pack? Just another line!</p>
<div style="display:inline;float:none;margin:0;padding:0;">
<pre style="line-height:8pt;font-size:8pt;">
<pre style="line-height:8pt;font-size:8pt;">PS&gt; $emg.LanguagePacks.GetLanguagePacks()|?&#123;$_.Name -eq &quot;ENU&quot; -and $_.GetManagementPack().Name -eq &quot;System.Library&quot;&#125;|
&gt;&gt; Select-Object -Expand DisplayStringCollection|ft name,description
&gt;&gt;

Name                                                        Description
----                                                        -----------
Display Name                                                Display name
Timeout Seconds
Database                                                    Defines the basic properties of databases
Local Application                                           Defines the basic properties of applications that are di...
Reference                                                   Defines the basic properties of directed relationships
. . .</pre>
</pre>
</div>
<p>yow!</p>
</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jtruher3.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jtruher3.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jtruher3.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jtruher3.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jtruher3.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jtruher3.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jtruher3.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jtruher3.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jtruher3.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jtruher3.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jtruher3.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jtruher3.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jtruher3.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jtruher3.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jtruher3.wordpress.com&amp;blog=17819539&amp;post=9&amp;subd=jtruher3&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jtruher3.wordpress.com/2009/10/02/service-manager-and-the-powershell-1-liner/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/20349e038c0f35188b594c68f20636bc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jtruher3</media:title>
		</media:content>
	</item>
	</channel>
</rss>
