<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>vmweaver.com</title>
	
	<link>http://vmweaver.com</link>
	<description>Mindless ramblings of a geek...</description>
	<lastBuildDate>Fri, 26 Feb 2010 17:02:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/vmweavercom" /><feedburner:info uri="vmweavercom" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Powershell and AD Object Recovery (Prologue)</title>
		<link>http://feedproxy.google.com/~r/vmweavercom/~3/1klc-hNf9Ts/</link>
		<comments>http://vmweaver.com/index.php/2010/02/powershell-ad-recovery-prologue/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 02:11:46 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[AD]]></category>
		<category><![CDATA[Recovery]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=229</guid>
		<description><![CDATA[I have been toying with an idea for a while since I have been diving deeper into Active Directory stuff with Powershell.
The idea is to develop a full Active Directory object backup and recovery tool.  There are several excellent tools available commercially that do this, but with IT staffs and budgets shrinking it is [...]]]></description>
			<content:encoded><![CDATA[<p>I have been toying with an idea for a while since I have been diving deeper into Active Directory stuff with Powershell.</p>
<p>The idea is to develop a full Active Directory object backup and recovery tool.  There are several excellent tools available commercially that do this, but with IT staffs and budgets shrinking it is often difficult to justify the cost.  </p>
<p>I am sure others have thought about it, maybe dabbled with it, but it seems to me that all of tools and components are available to do this, just that nobody (that I know of) has kind of glued them all together in an easy-to-use interface.</p>
<p>I know many of you may say &#8220;Hey, doesn&#8217;t Microsoft have an AD Recycle Bin now?&#8221;.  Well yes, they do&#8230;BUT many folks, I believe, are probably still running 2003 AD instances.  Regardless of all of that, though, this sounds like a good exercise to at least explore doing a larger-scale &#8220;application&#8221; using Powershell.</p>
<p>This will also be my first major dive in the Powershell V2.0, so I hope to be discovering new and interesting ways to put these tools together.</p>
<p>From my perspective, I see several main components that will need development:<br />
1. A user Interface.  While I have done some of this with &#8220;visual&#8221; languages and a touch with Powershell, I haven&#8217;t done anything very extensive.<br />
2. Interfacing with some type of database solution (probably SQL Server)<br />
3. Access to AD objects without 3rd party utilities. I think this will be fairly straightforward but  may end up using the Quest tools.<br />
4. Access to the &#8220;delete objects&#8221; container in the directory and a means to reanimate those objects.  (<a href="http://www.sdmsoftware.com/freeware.php">SDM Software Grou</a>p has some great cmdlets for doing this which I will probably look at using).<br />
5. General functions for manipulating AD objects (again&#8230;pretty easy)</p>
<p>One question keeps coming to mind, though is whether something like this would be useful?  I am not entirely sure, but I hope so!  To that end, I think the journey may be more worthwhile than the outcome.</p>
<p>Anyway, I hope to be spending more time with (when I *HAVE* spare time) and I hope to chronicle this &#8220;journey&#8221; here as I go.</p>
<p>If anyone has an opinion on this undertaking or has advice, please let me know.</p>
<p>Stay tuned for updates.  Right now I am standing up a small infrastructure at home to support this development effort.</p>
<p>Thanks and Happy Scripting!</p>
<p> &#8212; Mark</p>
<img src="http://feeds.feedburner.com/~r/vmweavercom/~4/1klc-hNf9Ts" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2010/02/powershell-ad-recovery-prologue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://vmweaver.com/index.php/2010/02/powershell-ad-recovery-prologue/</feedburner:origLink></item>
		<item>
		<title>Powershell and FSMO Roles</title>
		<link>http://feedproxy.google.com/~r/vmweavercom/~3/oNQj_KOglgE/</link>
		<comments>http://vmweaver.com/index.php/2009/11/powershell-and-fsmo-roles/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 16:37:38 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[AD]]></category>
		<category><![CDATA[Domain]]></category>
		<category><![CDATA[FSMO]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=218</guid>
		<description><![CDATA[Okay, this will be a quick and dirty post due to lack of time right now.
This one is kind of a tip, rather than a full-blown script or topic.  Basically I was looking to grab which system was the PDC Emulator for my current domain (or NOT my current domain) and so I did [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, this will be a quick and dirty post due to lack of time right now.<br />
This one is kind of a tip, rather than a full-blown script or topic.  Basically I was looking to grab which system was the PDC Emulator for my current domain (or NOT my current domain) and so I did some google-ing and finally ended up with these little functions.   </p>
<p>All I need to do is pass in the DomainName and it spits out the info.  For the FSMO roles, it will return an object and for the DomainMode, just the text is returned.</p>
<p>Hopefully you will find them useful.<br />
That&#8217;s it for now&#8230;<br />
Happy Scripting..<br />
 &#8211; Mark</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">Function</span> get<span style="color: pink;">-</span>PDCERole <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DomainName</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
   <span style="color: #008000;">## Return the PDC Emulator Role Owner for the specified Domain</span>
   <span style="color: #800080;">$DomainContext</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.DirectoryServices.ActiveDirectory.DirectoryContext<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Domain&quot;</span><span style="color: pink;">,</span> <span style="color: #800080;">$DomainName</span><span style="color: #000000;">&#41;</span>
   <span style="color: #800080;">$Domain</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.ActiveDirectory.Domain<span style="color: #000000;">&#93;</span>::GetDomain<span style="color: #000000;">&#40;</span><span style="color: #800080;">$DomainContext</span><span style="color: #000000;">&#41;</span>
   <span style="color: #800080;">$PDCE</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Domain</span>.PDCRoleOwner
&nbsp;
   <span style="color: #0000FF;">Return</span> <span style="color: #800080;">$PDCE</span>  
  <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">Function</span> get<span style="color: pink;">-</span>RIDMasterRole <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DomainName</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
   <span style="color: #008000;">## Return the RID Master Role Owner for the specified Domain</span>
   <span style="color: #800080;">$DomainContext</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.DirectoryServices.ActiveDirectory.DirectoryContext<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Domain&quot;</span><span style="color: pink;">,</span> <span style="color: #800080;">$DomainName</span><span style="color: #000000;">&#41;</span>
   <span style="color: #800080;">$Domain</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.ActiveDirectory.Domain<span style="color: #000000;">&#93;</span>::GetDomain<span style="color: #000000;">&#40;</span><span style="color: #800080;">$DomainContext</span><span style="color: #000000;">&#41;</span>
   <span style="color: #800080;">$RIDMaster</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Domain</span>.RIDRoleOwner
&nbsp;
   <span style="color: #0000FF;">Return</span> <span style="color: #800080;">$RIDMaster</span>
  <span style="color: #000000;">&#125;</span> 
&nbsp;
<span style="color: #0000FF;">Function</span> Get<span style="color: pink;">-</span>InfMasterRole <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DomainName</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
   <span style="color: #008000;">## Return the Infrastucture Master role owner for the specified Domain</span>
   <span style="color: #800080;">$DomainContext</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.DirectoryServices.ActiveDirectory.DirectoryContext<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Domain&quot;</span><span style="color: pink;">,</span> <span style="color: #800080;">$DomainName</span><span style="color: #000000;">&#41;</span>
   <span style="color: #800080;">$Domain</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.ActiveDirectory.Domain<span style="color: #000000;">&#93;</span>::GetDomain<span style="color: #000000;">&#40;</span><span style="color: #800080;">$DomainContext</span><span style="color: #000000;">&#41;</span>
   <span style="color: #800080;">$InfMaster</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Domain</span>.InfrastructureRoleOwner
&nbsp;
   <span style="color: #0000FF;">Return</span> <span style="color: #800080;">$InfMaster</span>
 <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">Function</span> Get<span style="color: pink;">-</span>DomainMode <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DomainName</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
   <span style="color: #008000;">## Return the Domain Mode for the specified Domain</span>
   <span style="color: #800080;">$DomainContext</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.DirectoryServices.ActiveDirectory.DirectoryContext<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Domain&quot;</span><span style="color: pink;">,</span> <span style="color: #800080;">$DomainName</span><span style="color: #000000;">&#41;</span>
   <span style="color: #800080;">$Domain</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.ActiveDirectory.Domain<span style="color: #000000;">&#93;</span>::GetDomain<span style="color: #000000;">&#40;</span><span style="color: #800080;">$DomainContext</span><span style="color: #000000;">&#41;</span>
   <span style="color: #800080;">$DomainMode</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Domain</span>.DomainMode
&nbsp;
   <span style="color: #0000FF;">Return</span> <span style="color: #800080;">$DomainMode</span>
&nbsp;
  <span style="color: #000000;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/vmweavercom/~4/oNQj_KOglgE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/11/powershell-and-fsmo-roles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://vmweaver.com/index.php/2009/11/powershell-and-fsmo-roles/</feedburner:origLink></item>
		<item>
		<title>Powershell and Unknown User SIDs</title>
		<link>http://feedproxy.google.com/~r/vmweavercom/~3/RWmnu52IGQE/</link>
		<comments>http://vmweaver.com/index.php/2009/10/powershell-and-unknown-user-sids/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 18:01:19 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[AD]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[User Management]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=204</guid>
		<description><![CDATA[Once again, my apologies for my lack of posting..
Anyway&#8230;
An interesting thing came up at work the other day when one of my fellow administrators asked me if I could resolve an unknown SID he was seeing in some logs to see what the heck it belonged to.
Since I had been telling him that Powershell could [...]]]></description>
			<content:encoded><![CDATA[<p>Once again, my apologies for my lack of posting..</p>
<p>Anyway&#8230;</p>
<p>An interesting thing came up at work the other day when one of my fellow administrators asked me if I could resolve an unknown SID he was seeing in some logs to see what the heck it belonged to.</p>
<p>Since I had been telling him that Powershell could do ANYTHING (slight exaggeration, I know)&#8230; that it should be able to do this.</p>
<p>Well, it certainly is an interesting notion.</p>
<p>I know that I have run into this in the past where you have file system ACLs set and there are a bunch of SIDs sitting in there that nobody seems to know who they belong to.</p>
<p>While it isn&#8217;t THAT important to resolve them since the user account is most likely no longer around, it IS an interesting thought exercise.</p>
<p>After perusing the web looking for others who have done something similar, I feel I had enough to throw something together..</p>
<p>Basically when an Active Directory object (like a user) is &#8220;deleted&#8221;, it is really just Tombstoned for a period of time and is moved to the hidden container &#8220;Deleted Objects&#8221; and then removed after like 90 days or so.</p>
<p>Here is my solution to &#8220;finding&#8221; those objects.</p>
<p>First you will have to know what Domain you want to look at for this object AND you have know the SID you are looking for.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Resolve<span style="color: pink;">-</span>DeletedUserSID<span style="color: #000000;">&#40;</span><span style="color: #800080;">$Domain</span><span style="color: pink;">,</span> <span style="color: #800080;">$UserSID</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;">## This is kind of a mashup of a few different scripts I found online in some forums.</span>
	<span style="color: #008000;">## Unfortunately I don't remember who did them.  If it was you, point me to your post and I will</span>
	<span style="color: #008000;">## give you the credit for your piece.</span>
	<span style="color: #008000;">## </span>
	<span style="color: #008000;">## Returns User information for deleted account with the specified SID and User Domain</span>
&nbsp;
	<span style="color: #800080;">$DomainRoot</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;LDAP://&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Domain</span>.trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$DomainDN</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>adsi<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #800080;">$DomainRoot</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.DistinguishedName
	<span style="color: #800080;">$adspath</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;LDAP://&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$DomainDN</span>
	<span style="color: #800080;">$root</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>system.directoryservices.Directoryentry<span style="color: #000000;">&#93;</span> <span style="color: #800080;">$Adspath</span>
	<span style="color: #800080;">$root</span>.psbase.AuthenticationType <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>system.directoryservices.authenticationtypes<span style="color: #000000;">&#93;</span>::Fastbind
	<span style="color: #008000;">## We will be looking in the &quot;Deleted Objects&quot; container which is normally hidden, etc.</span>
	<span style="color: #008000;">## You will need to execute this with an account that has DomainAdmin rights to the domain you are</span>
	<span style="color: #008000;">## querying.</span>
	<span style="color: #800080;">$root</span>.psbase.path <span style="color: pink;">=</span> <span style="color: #800000;">&quot;LDAP://cn=Deleted Objects,&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$DomainDN</span>
	<span style="color: #800080;">$search</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>system.directoryservices.directorysearcher<span style="color: #000000;">&#93;</span> <span style="color: #800080;">$root</span>
	<span style="color: #800080;">$search</span>.<span style="color: #0000FF;">filter</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;(&amp;(isDeleted=TRUE)(!(objectClass=computer))(objectclass=user))&quot;</span>
	<span style="color: #800080;">$search</span>.Tombstone <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>
&nbsp;
	<span style="color: #008000;"># If you have more than 1000 users, you must NOT define SizeLimit (we haven't)</span>
	<span style="color: #008000;"># and PageSize must be less than the default value (of 1000). </span>
	<span style="color: #008000;"># I found this a bit strange...but as long as we understand it, I guess it is okay</span>
	<span style="color: #800080;">$Search</span>.PageSize <span style="color: pink;">=</span> <span style="color: #000000;">500</span>
&nbsp;
	<span style="color: #008000;"># Only look in the top level of the Deleted Objects container.</span>
	<span style="color: #800080;">$search</span>.SearchScope <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>system.directoryservices.searchscope<span style="color: #000000;">&#93;</span>::OneLevel
	<span style="color: #800080;">$result</span> <span style="color: pink;">=</span> <span style="color: #800080;">$search</span>.FindAll<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008000;"># If the SID isn't found, you will get nothing returned.</span>
	<span style="color: #800080;">$result</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select-Object</span> <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span> Name <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Name&quot;</span> ; Expression <span style="color: pink;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #000080;">$_</span>.Properties.Item<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#93;</span>.split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span><span style="color: pink;">,</span> `
	<span style="color: pink;">@</span><span style="color: #000000;">&#123;</span> Name <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SAMAccountName&quot;</span> ; Expression <span style="color: pink;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #000080;">$_</span>.Properties.Item<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;SAMAccountName&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span><span style="color: pink;">,</span> `
	<span style="color: pink;">@</span><span style="color: #000000;">&#123;</span> Name <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SID&quot;</span> ; Expression <span style="color: pink;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Security.Principal.SecurityIdentifier<span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.Properties.Item<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;ObjectSID&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#93;</span><span style="color: pink;">,</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span><span style="color: pink;">,</span> `
	<span style="color: pink;">@</span><span style="color: #000000;">&#123;</span> Name <span style="color: pink;">=</span> <span style="color: #800000;">&quot;WhenChanged&quot;</span> ; Expression <span style="color: pink;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #000080;">$_</span>.Properties.Item<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;WhenChanged&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span> `
	<span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">where-Object</span> <span style="color: #000000;">&#123;</span> <span style="color: #000080;">$_</span>.SID <span style="color: #FF0000;">-ieq</span> <span style="color: #800080;">$UserSid</span>.Trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As always&#8230;</p>
<p>Happy Scripting!!!! and let me know if you have questions or problems.</p>
<p>&#8211; Mark</p>
<img src="http://feeds.feedburner.com/~r/vmweavercom/~4/RWmnu52IGQE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/10/powershell-and-unknown-user-sids/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://vmweaver.com/index.php/2009/10/powershell-and-unknown-user-sids/</feedburner:origLink></item>
		<item>
		<title>Powershell – Recursive Group Membership</title>
		<link>http://feedproxy.google.com/~r/vmweavercom/~3/sQWFXKQdufM/</link>
		<comments>http://vmweaver.com/index.php/2009/08/powershell-recursive-group-membership/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 02:11:57 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Group Membership]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=146</guid>
		<description><![CDATA[Well, I am back for yet another Powershell script. This is one that I found pretty useful actually.
As one of the people really pushing automation in my group at work, I was tasked with getting a list of all users in the &#8220;Domain Admins&#8221; group for all domains in our Active Directory forest.
One of the [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I am back for yet another Powershell script. This is one that I found pretty useful actually.<br />
As one of the people really pushing automation in my group at work, I was tasked with getting a list of all users in the &#8220;Domain Admins&#8221; group for all domains in our Active Directory forest.</p>
<p>One of the challenges in doing this is that you may have a bunch of nested groups and we needed to dump users from all nested groups, etc.  I do realize that there are probably tools and what-not that would this for me, but what fun is that and why spend the bucks if you can script it out.  I like this approach, too, because I can force the output to be whatever I want and in whichever format is best for what I am trying to accomplish.</p>
<p>From my days in college as a computer science kinda guy, I figured we could use recursion to help walk us through all nested groups.</p>
<p>So for those of you unfamilar with this idea of recursion, I will summarize:<br />
It is basically a function that calls itself until a certain condition is met. At this point the function exits. I know all you CS types may take exception to such a simplified definition, so please google for it or hit up Wikipedia for more detailed info on recursion.</p>
<p>How can this possible help us in our quest to enumeration group memberships?  Well let&#8217;s break it down a little.</p>
<ol>
<li>I start with a group I care about. Let&#8217;s say it is &#8220;Domain Admins&#8221; for domain &#8220;office1.contoso.com&#8221;.</li>
<li>I have a function (&#8220;get-groupmembers&#8221;) that I use to enumerate the members of this group and do something with them (output, write to file, etc)</li>
<li>As I am enumerating them, I find a member that is of type &#8220;group&#8221; called something like &#8220;Corp Admins&#8221;</li>
<li>I now call my function (&#8220;get-groupmembers&#8221;) with this nested group (&#8220;Corp Admins&#8221;) to enumerate the members</li>
<li>As I am enumerating them, I find ANOTHER group called &#8220;Help Desk On-Call&#8221; inside of &#8220;Corp Admins&#8221;</li>
<li>I can now call my funciton (&#8220;get-groupmembers&#8221;) ANOTHER time and keep going until I only have users and have walked all of the nested groups</li>
</ol>
<p>I know this may sound a little weird &#8220;that I am calling myself&#8221; over and over again, but it is actually pretty efficient.</p>
<p>Let&#8217;s jump into some code now.</p>
<p>For starters, I need to have some functions that take a Fully Qualified Domain Name (for an Active Directory Domain) and convert it into an LDAP-ish format.  For example, I needed &#8220;office1.contoso.com&#8221; to be transformed into &#8220;DC=office1,DC=contoso,DC=com&#8221;.  I know this isn&#8217;t rocket-surgery, but I just threw some stuff together for it.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Convert<span style="color: pink;">-</span>DNStoDN <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DNSName</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008000;">#  Create an array of each item in the string separated by &quot;.&quot;</span>
   <span style="color: #800080;">$DNSArray</span> <span style="color: pink;">=</span> <span style="color: #800080;">$DNSName</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;.&quot;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #008000;"># Let's go through our new array and do something with each item</span>
   <span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>; <span style="color: #800080;">$x</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$DNSArray</span>.Length ; <span style="color: #800080;">$x</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        <span style="color: #008000;">#I don't want a comma after my last item, so check to see if I am on my last one and set</span>
        <span style="color: #008000;"># $Separator equal to nothing.</span>
        <span style="color: #008000;"># Remember that we need to go to Length-1 because arrays are &quot;0 based indexes&quot;</span>
         <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: #FF0000;">-eq</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DNSArray</span>.Length <span style="color: pink;">-</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$Separator</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #0000FF;">else</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$Separator</span> <span style="color: pink;">=</span><span style="color: #800000;">&quot;,&quot;</span><span style="color: #000000;">&#125;</span>
         <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$DN</span> <span style="color: pink;">+=</span> <span style="color: #800000;">&quot;DC=&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$DNSArray</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$x</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Separator</span>
      <span style="color: #000000;">&#125;</span>
   <span style="color: #0000FF;">return</span> <span style="color: #800080;">$DN</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We will also need to be able to split the FQDN of the DOMAIN out from the DN of a group or user. So, I have something like &#8220;CN=Me,OU=User1,DC=office1,DC=contoso,DC=com&#8221; and want to get the FQDN of this domain.  For this example this would output &#8220;office1.contoso.com&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Convert<span style="color: pink;">-</span>DNtoDNS <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DN</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #800080;">$DNArray</span> <span style="color: pink;">=</span> <span style="color: #800080;">$DN</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;,&quot;</span><span style="color: #000000;">&#41;</span>
     <span style="color: #008000;"># Let's go through our new array and do something with each item</span>
   <span style="color: #0000FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>; <span style="color: #800080;">$x</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$DNArray</span>.Length ; <span style="color: #800080;">$x</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        <span style="color: #008000;">#I don't want a period after my last item, so check to see if I am on my last one and set</span>
        <span style="color: #008000;"># $Separator equal to nothing.</span>
        <span style="color: #008000;"># Remember that we need to go to Length-1 because arrays are &quot;0 based indexes&quot;</span>
         <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: #FF0000;">-eq</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DNArray</span>.Length <span style="color: pink;">-</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$Separator</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #0000FF;">else</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$Separator</span> <span style="color: pink;">=</span><span style="color: #800000;">&quot;.&quot;</span><span style="color: #000000;">&#125;</span>
        <span style="color: #008000;"># Now we have to see if we look like &quot;DC=&quot;. If it does, we will</span>
        <span style="color: #008000;"># start to construct our DNS name.</span>
        <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DNArray</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$x</span><span style="color: #000000;">&#93;</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;=&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #FF0000;">-ilike</span> <span style="color: #800000;">&quot;DC&quot;</span><span style="color: #000000;">&#41;</span>
          <span style="color: #000000;">&#123;</span>
               <span style="color: #008000;"># Let's grab the &quot;contoso&quot; side of the &quot;DC=contoso&quot;</span>
              <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$DNS</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$DNArray</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$x</span><span style="color: #000000;">&#93;</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;=&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Separator</span>
           <span style="color: #000000;">&#125;</span>
      <span style="color: #000000;">&#125;</span>
   <span style="color: #0000FF;">return</span> <span style="color: #800080;">$DNS</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now that we have those little &#8220;cameo&#8221; functions, we will move on to the more of the meat-and-potatoes of the script.<br />
The next function will be to actually enumerate a group in Active Directory without using the Quest Tools for Active Directory (if you don&#8217;t have those yet, you need to them).</p>
<p>We are actually going to use some .NET calls to get the directory objects.  My colleague <a title="Mike Hays' Blog" href="http://blog.mike-hays.net/" target="_blank">Mike Hays</a> actually did a lot of this part of the code.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> get<span style="color: pink;">-</span>groupmember<span style="color: #000000;">&#40;</span><span style="color: #800080;">$domain</span><span style="color: pink;">,</span> <span style="color: #800080;">$groupName</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008000;"># I have passed in the FQDN and Groupname I am interested in</span>
   <span style="color: #008000;"># I just need to convert my FQDN into an LDAP style name using my previous function</span>
   <span style="color: #800080;">$DN</span> <span style="color: pink;">=</span> convert<span style="color: pink;">-</span>DNStoDN<span style="color: #000000;">&#40;</span><span style="color: #800080;">$Domain</span><span style="color: #000000;">&#41;</span>
   <span style="color: #800080;">$domainLDAPUrl</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;LDAP://&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$DN</span>
   <span style="color: #008000;"># Setup my directory connection using .NET call</span>
   <span style="color: #800080;">$ent</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.DirectoryEntry<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #800080;">$domainLDAPUrl</span> <span style="color: #000000;">&#41;</span>
&nbsp;
   <span style="color: #008000;"># Define my &quot;searcher&quot; object to query the directory</span>
   <span style="color: #800080;">$srch</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.DirectorySearcher<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #800080;">$ent</span> <span style="color: #000000;">&#41;</span>
&nbsp;
   <span style="color: #008000;"># Setup my search criteria.. looking for all Groups with CN=GroupName</span>
   <span style="color: #800080;">$groupNameFilter</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;(&amp;(objectClass=group)(CN=&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$groupName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;))&quot;</span>
   <span style="color: #800080;">$srch</span>.<span style="color: #0000FF;">Filter</span> <span style="color: pink;">=</span> <span style="color: #800080;">$groupNameFilter</span>
&nbsp;
   <span style="color: #008000;"># Now go execute my query to and put the results in $coll</span>
   <span style="color: #800080;">$coll</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.SearchResultCollection<span style="color: #000000;">&#93;</span>      <span style="color: #800080;">$srch</span>.FindAll<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
   <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$rs</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$coll</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
       <span style="color: #008000;"># Now get a collection of properties for that object</span>
       <span style="color: #800080;">$resultPropColl</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.ResultPropertyCollection<span style="color: #000000;">&#93;</span> <span style="color: #800080;">$rs</span>.Properties
&nbsp;
       <span style="color: #008000;"># Cycle through all group members</span>
       <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$memberColl</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$resultPropColl</span><span style="color: #000000;">&#91;</span><span style="color: #800000;">&quot;member&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
         <span style="color: #000000;">&#123;</span>
           <span style="color: #008000;"># Build my membership array</span>
           <span style="color: #000000;">&#91;</span><span style="color: #008080;">array</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$gpMemberEntry</span> <span style="color: pink;">+=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.DirectoryEntry<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #800000;">&quot;LDAP://&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$memberColl</span> <span style="color: #000000;">&#41;</span>
          <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
  <span style="color: #008000;"># Send back my group members.</span>
  <span style="color: #0000FF;">return</span> <span style="color: #800080;">$gpMemberEntry</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Okay.. now that we have THAT setup let&#8217;s talk about the next bits of code.<br />
This is where we will have our recursive function &#8220;Get-AllMembers&#8221;.  In it, you will a call to itself.  One of the biggest concerns is that you can end up in an unending or infinite cycle.  I don&#8217;t really do any checking in this little scripty-do-dad, so that may be something for later.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> get<span style="color: pink;">-</span>allmembers<span style="color: #000000;">&#40;</span><span style="color: #800080;">$objectName</span><span style="color: pink;">,</span> <span style="color: #800080;">$OF</span><span style="color: pink;">,</span> <span style="color: #800080;">$GN</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008000;"># Split out my domain name  (should be FQDN) and the group name</span>
    <span style="color: #800080;">$domainName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objectname</span>.split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#93;</span>
    <span style="color: #800080;">$ObjectName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objectname</span>.split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#93;</span>
&nbsp;
    <span style="color: #800080;">$members</span> <span style="color: pink;">=</span> get<span style="color: pink;">-</span>groupmember <span style="color: #800000;">&quot;$DomainName&quot;</span> <span style="color: #800000;">&quot;$ObjectName&quot;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$members</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800080;">$NULL</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
        <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$member</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$members</span><span style="color: #000000;">&#41;</span>
         <span style="color: #000000;">&#123;</span>
            <span style="color: #008000;">#  Grab the domain DNS name out of the object DN</span>
            <span style="color: #800080;">$ObjDomain</span> <span style="color: pink;">=</span> convert<span style="color: pink;">-</span>DNtoDNS <span style="color: #800080;">$Member</span>.DistinguishedName
            <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$member</span>.objectclass <span style="color: #FF0000;">-contains</span> <span style="color: #800000;">&quot;group&quot;</span><span style="color: #000000;">&#41;</span>
              <span style="color: #000000;">&#123;</span>
                 <span style="color: #008000;">#If my group member is, itself, a group We get to do some recursion</span>
                  <span style="color: #800080;">$out</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objDomain</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;\&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$member</span>.name
                  <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$out</span>
                  <span style="color: #008000;"># Call myself with the nested group name</span>
                  get<span style="color: pink;">-</span>allmembers <span style="color: pink;">-</span>ObjectName <span style="color: #800080;">$out</span> <span style="color: pink;">-</span>OF <span style="color: #800080;">$of</span> <span style="color: pink;">-</span>GN <span style="color: #800080;">$GN</span>
               <span style="color: #000000;">&#125;</span>
            <span style="color: #0000FF;">else</span>
               <span style="color: #000000;">&#123;</span>
                  <span style="color: #008000;"># If I get back a user, then see if the user is disabled or not</span>
                  <span style="color: #800080;">$userAndDomain</span> <span style="color: pink;">=</span> <span style="color: #800080;">$objDomain</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;\&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$member</span>.name
                  <span style="color: #008000;"># The  UserAccountControl property contains several &quot;flags&quot;</span>
                  <span style="color: #008000;"># that we can interrogate.  By doing a Binary AND we are seeing if the 2nd flag is set.</span>
                  <span style="color: #000000;">&#91;</span><span style="color: #008080;">bool</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$accountIsDisabled</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$member</span>.userAccountControl.ToSTring<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-band</span> <span style="color: #000000;">2</span>
&nbsp;
                  <span style="color: #008000;"># Setup our output (I am choosing to construct a comma-delimited type of output</span>
                  <span style="color: #800080;">$OutText</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;'&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$GN</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$objDomain</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Member</span>.Samaccountname <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Member</span>.displayName <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$member</span>.distinguishedName <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$objectname</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;','&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$accountIsDisabled</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;'&quot;</span>
&nbsp;
                  <span style="color: #008080; font-weight: bold;">Out-File</span> <span style="color: #008080; font-style: italic;">-FilePath</span> <span style="color: #800080;">$OF</span> <span style="color: #008080; font-style: italic;">-inputobject</span> <span style="color: #800080;">$Outtext</span> <span style="color: #008080; font-style: italic;">-append</span> <span style="color: #008080; font-style: italic;">-Encoding</span> <span style="color: #800000;">&quot;ASCII&quot;</span>
&nbsp;
                  <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$OutText</span>
                <span style="color: #000000;">&#125;</span>
         <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">else</span>
     <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;No Members or no Group:&quot;</span> <span style="color: #800080;">$ObjectName</span> <span style="color: #800000;">&quot;in Domain:&quot;</span> <span style="color: #800080;">$DomainName</span> <span style="color: pink;">-</span>Foreground RED
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">###########################</span>
<span style="color: #008000;">## Main</span>
<span style="color: #008000;">###########################</span>
<span style="color: #800080;">$GroupName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Domain Admins&quot;</span>
<span style="color: #800080;">$Today</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span> <span style="color: #008080; font-style: italic;">-format</span> <span style="color: #800000;">&quot;yyyyMMddhh&quot;</span>
<span style="color: #800080;">$OutputFolder</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Temp\&quot;</span>
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Test-Path</span> <span style="color: #800080;">$outputFolder</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$False</span><span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-weight: bold;">New-Item</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$OutputFolder</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">Type</span> Directory <span style="color: pink;">&gt;</span> <span style="color: #800080;">$NULL</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Grab my forest info</span>
<span style="color: #800080;">$forest</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.DirectoryServices.ActiveDirectory.Forest<span style="color: #000000;">&#93;</span>::GetCurrentForest<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;">#Setup my output file</span>
<span style="color: #800080;">$OutHeader</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;'Group','UserDomain','SAMAccount','DisplayName','DN','MemberofGroup','IsDisabled'&quot;</span>
&nbsp;
<span style="color: #008000;"># Define output file name</span>
<span style="color: #800080;">$of</span> <span style="color: pink;">=</span> <span style="color: #800080;">$OutputFolder</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Today</span> <span style="color: pink;">+</span><span style="color: #800000;">&quot;_&quot;</span><span style="color: pink;">+</span> <span style="color: #800080;">$GroupName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;-AuditReport.txt&quot;</span>
&nbsp;
<span style="color: #008080; font-weight: bold;">Out-File</span> <span style="color: #008080; font-style: italic;">-FilePath</span> <span style="color: #800080;">$OF</span> <span style="color: #008080; font-style: italic;">-inputobject</span> <span style="color: #800080;">$OutHeader</span> <span style="color: #008080; font-style: italic;">-Encoding</span> <span style="color: #800000;">&quot;ASCII&quot;</span>
&nbsp;
<span style="color: #008000;"># Cycle through all the child domains in the forest root to query for Group.</span>
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$domain</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$forest</span>.Domains<span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
    <span style="color: #800080;">$FullGroupName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$domain</span>.name <span style="color: pink;">+</span> <span style="color: #800000;">&quot;\&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$GroupName</span>
    get<span style="color: pink;">-</span>allmembers <span style="color: pink;">-</span>ObjectName <span style="color: #800080;">$FullGroupName</span> <span style="color: pink;">-</span>OF <span style="color: #800080;">$of</span> <span style="color: pink;">-</span>GN <span style="color: #800080;">$FullGroupName</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Just take all of the script blocks from above and paste them into your script.  I am trying to keep these posts a bit shorter, so you may see upcoming posts broken out into parts.</p>
<p>Well, I think I am done here with this one.  Please let me know if you have questions, concerns, or comments.</p>
<p>Please keep in mind that this script will attempt to enumerate the Group in  ALL child domains in your current AD Forest.  If you have a large Forest with lots of child domains&#8230;..this could take a while.</p>
<p>I will be happy to help out with requested changes if they seem like they would be beneficial overall, but I am also a STRONG advocate of doing-it-yourself.<br />
Every bit of Powershell and scripting I have learned by grabbing it and going with it.</p>
<p>Anyway, as always&#8230;thanks for stopping by and happy scripting!!!</p>
<p>&#8211; Mark</p>
<img src="http://feeds.feedburner.com/~r/vmweavercom/~4/sQWFXKQdufM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/08/powershell-recursive-group-membership/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://vmweaver.com/index.php/2009/08/powershell-recursive-group-membership/</feedburner:origLink></item>
		<item>
		<title>Binary Search and Powershell</title>
		<link>http://feedproxy.google.com/~r/vmweavercom/~3/XjX567h718Q/</link>
		<comments>http://vmweaver.com/index.php/2009/07/binary-search-and-powershell/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 03:53:52 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[EventLog]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=142</guid>
		<description><![CDATA[Time for another Powershell Post.
So, what is a binary search and why should you care about it?
Well, it provides us a mechanism to very quickly search through an ordered list of &#8220;things&#8221; (like event log entries). It is especially useful when looking through LARGE amounts of data.
I know, I know.. There are all sorts of [...]]]></description>
			<content:encoded><![CDATA[<p>Time for another Powershell Post.<br />
So, what is a binary search and why should you care about it?</p>
<p>Well, it provides us a mechanism to very quickly search through an ordered list of &#8220;things&#8221; (like event log entries). It is especially useful when looking through LARGE amounts of data.</p>
<p>I know, I know.. There are all sorts of ways to look through/filter/search event logs and everyone has probably written about this already, but I guess I feel compelled to put in my 2 cents.</p>
<p>We ran into the need to be able to pull event log entries from a specific period of time on several servers.<br />
There aren&#8217;t really any &#8220;native&#8221; Powershell methods to query Eventlogs from REMOTE servers (unless you are on CTP and hitting Vista/2K8 systems).</p>
<p>So a little info about a binary search:</p>
<p>Lets say we have a list of things as follows:</p>
<table border="1" cellspacing="0" cellpadding="4" width="198" bordercolor="#000000">
<colgroup span="1">
<col span="1" width="48"></col>
<col span="1" width="47"></col>
<col span="1" width="77"></col>
</colgroup>
<tbody>
<tr valign="top">
<td width="48" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>Index</strong></span></p>
</td>
<td width="47" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>Name</strong></span></p>
</td>
<td width="77" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>DOB</strong></span></p>
</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">0</p>
</td>
<td width="47">Adam</td>
<td width="77">01/20/80</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">1</p>
</td>
<td width="47">Cheryl</td>
<td width="77">09/16/77</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">2</p>
</td>
<td width="47">Frank</td>
<td width="77">04/01/82</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">3</p>
</td>
<td width="47">Ivan</td>
<td width="77">10/26/70</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">4</p>
</td>
<td width="47">Suzy</td>
<td width="77">04/10/90</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">5</p>
</td>
<td width="47">Tim</td>
<td width="77">11/21/62</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">6</p>
</td>
<td width="47">Wendy</td>
<td width="77">02/02/71</td>
</tr>
</tbody>
</table>
<p>And we are wanting to find which index “Suzy” is in the list.</p>
<p>One way would be to start at the top of the list and see if the Name matches. On a short list this may make sense since it is really only 5 compares before we find her.</p>
<p>But if I was looking for “Suzy” in a list the size of a phone book this can take a long time and a lot of horsepower to find her and may take 300,000 compares or more.</p>
<p>A binary search algorithm helps us by breaking the list down by using something we already know about the list: that it is sorted by name.</p>
<p>With a single compare, though, we can cut the list of viable options in half.</p>
<p>All we need to do is take the index of our smallest value [0] and we add it to the index of our largest element [6] and divide by 2, we get the index of our middle element [3]. Now lets compare what we are looking for (“Suzy”) to the value of element [3].</p>
<p>LowerBound Index = 0<br />
UpperBound Index = 6<br />
Mid Index = (6+0)\2 = 3</p>
<p>Lets take a look at this:</p>
<table border="1" cellspacing="0" cellpadding="4" width="198" bordercolor="#000000">
<colgroup span="1">
<col span="1" width="48"></col>
<col span="1" width="47"></col>
<col span="1" width="77"></col>
</colgroup>
<tbody>
<tr valign="top">
<td width="48" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>Index</strong></span></p>
</td>
<td width="47" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>Name</strong></span></p>
</td>
<td width="77" bgcolor="#000000">
<p align="center"><span style="color: #ffffff;"><strong>DOB</strong></span></p>
</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">0</p>
</td>
<td width="47">Adam</td>
<td width="77">01/20/80</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">1</p>
</td>
<td width="47">Cheryl</td>
<td width="77">09/16/77</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">2</p>
</td>
<td width="47">Frank</td>
<td width="77">04/01/82</td>
</tr>
<tr valign="top">
<td width="48" bgcolor="#e6e6ff">
<p align="center">3</p>
</td>
<td width="47" bgcolor="#e6e6ff">Ivan</td>
<td width="77" bgcolor="#e6e6ff">10/26/70</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">4</p>
</td>
<td width="47">Suzy</td>
<td width="77">04/10/90</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">5</p>
</td>
<td width="47">Tim</td>
<td width="77">11/21/62</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">6</p>
</td>
<td width="47">Wendy</td>
<td width="77">02/02/71</td>
</tr>
</tbody>
</table>
<p>Obviously “Suzy” is greater than our value at index [3] “Ivan”. Since we know this, we can throw out all indexes that are [3] and lower.</p>
<p>Our list of viable options now becomes this:</p>
<table border="1" cellspacing="0" cellpadding="4" width="198" bordercolor="#000000">
<colgroup span="1">
<col span="1" width="48"></col>
<col span="1" width="47"></col>
<col span="1" width="77"></col>
</colgroup>
<tbody>
<tr valign="top">
<td width="48">
<p align="center">4</p>
</td>
<td width="47">Suzy</td>
<td width="77">04/10/90</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">5</p>
</td>
<td width="47">Tim</td>
<td width="77">11/21/62</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">6</p>
</td>
<td width="47">Wendy</td>
<td width="77">02/02/71</td>
</tr>
</tbody>
</table>
<p style="MARGIN-BOTTOM: 0in">Let&#8217;s repeat this exercise on our new list:</p>
<p style="MARGIN-BOTTOM: 0in">Lowest index = 4</p>
<p style="MARGIN-BOTTOM: 0in">Largest index = 6</p>
<p style="MARGIN-BOTTOM: 0in">Middle index = (6+4) /2 = 5</p>
<p style="MARGIN-BOTTOM: 0in"> </p>
<table border="1" cellspacing="0" cellpadding="4" width="198" bordercolor="#000000">
<colgroup span="1">
<col span="1" width="48"></col>
<col span="1" width="47"></col>
<col span="1" width="77"></col>
</colgroup>
<tbody>
<tr valign="top">
<td width="48">
<p align="center">4</p>
</td>
<td width="47">Suzy</td>
<td width="77">04/10/90</td>
</tr>
<tr valign="top">
<td width="48" bgcolor="#e6e6e6">
<p align="center">5</p>
</td>
<td width="47" bgcolor="#e6e6e6">Tim</td>
<td width="77" bgcolor="#e6e6e6">11/21/62</td>
</tr>
<tr valign="top">
<td width="48">
<p align="center">6</p>
</td>
<td width="47">Wendy</td>
<td width="77">02/02/71</td>
</tr>
</tbody>
</table>
<p style="MARGIN-BOTTOM: 0in"> So now we compare what we are looking for “Suzy” to our element [5] (“Tim”). Well, “Suzy is less than “Tim”, so that means we can throw out indexes that are [5] and higher.</p>
<p style="MARGIN-BOTTOM: 0in"> </p>
<table border="1" cellspacing="0" cellpadding="4" width="198" bordercolor="#000000">
<colgroup span="1">
<col span="1" width="48"></col>
<col span="1" width="47"></col>
<col span="1" width="77"></col>
</colgroup>
<tbody>
<tr valign="top">
<td width="48" bgcolor="#e6e6e6">
<p align="center">4</p>
</td>
<td width="47" bgcolor="#e6e6e6">Suzy</td>
<td width="77" bgcolor="#e6e6e6">04/10/90</td>
</tr>
</tbody>
</table>
<p style="MARGIN-BOTTOM: 0in"> Let&#8217;s go one more time&#8230;. oh wait.. .there is only one thing left&#8230;.it&#8217;s “Suzy”. We have found her.</p>
<p style="MARGIN-BOTTOM: 0in"> </p>
<p style="MARGIN-BOTTOM: 0in">There ARE .NET methods (of [System.Array]) that enable us to do binary searches of Arrays for Strings. The only problem is that it will be looking for EXACT matches for the string.</p>
<p style="MARGIN-BOTTOM: 0in"> </p>
<p style="MARGIN-BOTTOM: 0in">This poses a problem for us if we are looking for something that is a little less exact.</p>
<p style="MARGIN-BOTTOM: 0in"> </p>
<p style="MARGIN-BOTTOM: 0in"><span style="font-size: large;"><em>Mark&#8217;s Modified Binary Search</em></span></p>
<p>Okay, leave it to me to mess with a good thing.</p>
<p>My problem is that I want to grab all events from the System event log that occurred between 8pm and 9pm last night.</p>
<p>This is a little more “fuzzy” than exact, so let me show you what I did&#8230;.heh</p>
<p> I know you all are getting a little antsy for some code, but we will get there soon.. Stay with me.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>DatedLogEntries<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ServerName</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$EventLogName</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>datetime<span style="color: #000000;">&#93;</span><span style="color: #800080;">$OldestTime</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>datetime<span style="color: #000000;">&#93;</span><span style="color: #800080;">$NewestTime</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #008000;">#Grabbing my Eventlog Entries</span>
	<span style="color: #800080;">$EventLog</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Diagnostics.EventLog<span style="color: #000000;">&#40;</span><span style="color: #800080;">$EventlogName</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$EventLog</span>.MachineName <span style="color: pink;">=</span> <span style="color: #800080;">$ServerName</span>
	<span style="color: #800080;">$Entries</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Eventlog</span>.Entries</pre></div></div>

<p>There are some cmdlets designed to return eventlog entries, but they are only really effective if you are querying the local server or if you are leveraging the “new” Powershell Remoting, but you have to be running against Vista or Server 2008.</p>
<p>Fortunately, there is a way to grab remote event log entries via .NET (as shown in the code above)</p>
<p>When done, $Entries will contain all of the events in the logfile sorted by Time.</p>
<p>Next, we will setup our “bound”ing values for our array of entries and the times we are looking for.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">#Defining my starting boundaries of my array</span>
<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span>.count <span style="color: pink;">-</span> <span style="color: #000000;">1</span>
<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
&nbsp;
<span style="color: #008000;">#Setting up my dates</span>
<span style="color: #800080;">$StartTime</span> <span style="color: pink;">=</span> <span style="color: #800080;">$OldestTime</span>
<span style="color: #800080;">$EndTime</span> <span style="color: pink;">=</span> $NewestTime</pre></div></div>

<p>Now we are ready for the real work. Because I want to grab a range of events, I will run through the binary search two times. The first time will tell me what the UpperBound of my list is, and the second will tell me what the LowerBound of my list is as array indexes.</p>
<p>Before we start checking anything, we can make sure our search isn&#8217;t for naught.</p>
<p>Many systems have event logs that can roll pretty quickly, so we can just compare the oldest event to our start time. If the oldest event is newer than my start time, then my logs have rolled and I won&#8217;t have any data.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">-</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-gt</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #000000;">2</span> <span style="color: #000000;">&#41;</span> <span style="color: #008000;">#Calculate my midpoint</span>
  <span style="color: #008000;">#Compare my midpoint to my StartTime</span>
  <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
  <span style="color: #008000;">#If my midpoint is less than my Start time, then throw out all events</span>
  <span style="color: #008000;">#below and including my Midpoint</span>
        <span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span> <span style="color: pink;">+</span> <span style="color: #000000;">1</span>
     <span style="color: #000000;">&#125;</span>
   <span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
     <span style="color: #008000;">#If my midpoint is greater than my Start time, then throw out all events</span>
      <span style="color: #008000;">#above and including my Midpoint</span>
      <span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span><span style="color: pink;">-</span><span style="color: #000000;">1</span>
     <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">else</span>
     <span style="color: #000000;">&#123;</span>
     <span style="color: #008000;">#If my midpoint is equal to my Start time, then I got lucky and found my time.</span>
     <span style="color: #008000;">#I just realized that I may need to do something else with this. May tackle that</span>
     <span style="color: #008000;"># later though...</span>
      <span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
      <span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
     <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
 <span style="color: #008000;"># Now I know that the array index of our oldest item is here</span>
 <span style="color: #800080;">$Oldest</span> <span style="color: pink;">=</span> $LBound</pre></div></div>

<p>Now we will repeat the process to find out LowerBound. I won&#8217;t document it too much since it is nearly identical code to above. If you have questions, please let me know.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span>.count <span style="color: pink;">-</span> <span style="color: #000000;">1</span>
<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
&nbsp;
<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">-</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-gt</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #000000;">2</span> <span style="color: #000000;">&#41;</span>
   <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
      <span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span> <span style="color: pink;">+</span> <span style="color: #000000;">1</span>
     <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
      <span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span><span style="color: pink;">-</span><span style="color: #000000;">1</span>
     <span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">else</span>
    <span style="color: #000000;">&#123;</span>
     <span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
     <span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
    <span style="color: #000000;">&#125;</span>
 <span style="color: #000000;">&#125;</span>
<span style="color: #800080;">$Newest</span> <span style="color: pink;">=</span> $LBound</pre></div></div>

<p>So now we have the LowerBound index for the range I am looking for.<br />
Now to finish it up&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Sometimes we can end up with endpoints that don't meet our time range</span>
<span style="color: #008000;"># We fix that by going through each side and adjusting them until they</span>
<span style="color: #008000;"># are correct</span>
 <span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$Endtime</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$Newest</span><span style="color: pink;">--</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
 <span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$Oldest</span><span style="color: pink;">++</span>
  <span style="color: #000000;">&#125;</span>	
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
    <span style="color: #008000;">#Insert Code here if you want to do something when</span>
    <span style="color: #008000;">#no events found during the period requested..</span>
    <span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NULL</span>
 <span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">else</span>
 <span style="color: #000000;">&#123;</span>
    <span style="color: #008000;">#Create a new array and assign it the $Entries indexes ranging from our 'oldest' to our 'newest'</span>
    <span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span>..<span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">else</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;">#Insert Code here if you want to do something when</span>
	<span style="color: #008000;"># Logs have rolled... and none were in the specified range.</span>
	<span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NULL</span>
 <span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">return</span> <span style="color: #800080;">$EntriesByDate</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I hope I was able to articulate this effectively. As always, let me know if you have questions, comments, or suggestions.<br />
Thanks for Reading and happy shelling.<br />
So, that is basically it. Here is the code in one big block:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">## Get-DatedLogEntries Function</span>
<span style="color: #008000;">## Written by: Mark A. Weaver</span>
<span style="color: #008000;">## Website: www.vmweaver.com</span>
<span style="color: #008000;">## Version: 1.0</span>
<span style="color: #008000;">## Date: 7/23/2009</span>
<span style="color: #008000;">## Purpose: This Function will get event log entries from the specified server using currently logged in</span>
<span style="color: #008000;">##          credentials and return an array of Events that occurred between the 2 times.</span>
<span style="color: #008000;">##          Not much error checking or validation is done, so you please edit to your liking.</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##        Input:</span>
<span style="color: #008000;">##				-ServerName &quot;ServerName&quot;</span>
<span style="color: #008000;">##				-EventLogName &quot;EventLogName&quot;</span>
<span style="color: #008000;">##          -OldestTime [DateTime]OldestTime</span>
<span style="color: #008000;">##				-NewestTime [Datetime]NewestTime</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##        Output:</span>
<span style="color: #008000;">##				Array of Event log entries or Null if none found</span>
<span style="color: #008000;">#############################</span>
<span style="color: #008000;">## Updates:</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">######################################################################</span>
<span style="color: #008000;">######################################################################</span>
&nbsp;
<span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>DatedLogEntries<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ServerName</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$EventLogName</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>datetime<span style="color: #000000;">&#93;</span><span style="color: #800080;">$OldestTime</span><span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>datetime<span style="color: #000000;">&#93;</span><span style="color: #800080;">$NewestTime</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #008000;">#Grabbing my Eventlog Entries</span>
	<span style="color: #800080;">$EventLog</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Diagnostics.EventLog<span style="color: #000000;">&#40;</span><span style="color: #800080;">$EventlogName</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$EventLog</span>.MachineName <span style="color: pink;">=</span> <span style="color: #800080;">$ServerName</span>
	<span style="color: #800080;">$Entries</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Eventlog</span>.Entries
&nbsp;
	<span style="color: #008000;">#Defining my starting boundaries of my array</span>
	<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span>.count <span style="color: pink;">-</span> <span style="color: #000000;">1</span>
	<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
	<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
&nbsp;
	<span style="color: #008000;">#Setting up my dates</span>
	<span style="color: #800080;">$StartTime</span> <span style="color: pink;">=</span> <span style="color: #800080;">$OldestTime</span>
	<span style="color: #800080;">$EndTime</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NewestTime</span> 
&nbsp;
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">0</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">-</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-gt</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #000000;">2</span> <span style="color: #000000;">&#41;</span> <span style="color: #008000;">#Calculate my midpoint</span>
			<span style="color: #008000;">#Compare my midpoint to my StartTime</span>
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008000;">#If my midpoint is less than my Start time, then throw out all events</span>
				<span style="color: #008000;">#below and including my Midpoint</span>
				<span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span> <span style="color: pink;">+</span> <span style="color: #000000;">1</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008000;">#If my midpoint is greater than my Start time, then throw out all events</span>
				<span style="color: #008000;">#above and including my Midpoint</span>
				<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span><span style="color: pink;">-</span><span style="color: #000000;">1</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">else</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008000;">#If my midpoint is equal to my Start time, then I got lucky and found my time.</span>
				<span style="color: #008000;">#I just realized that I may need to do something else with this. May tackle that</span>
				<span style="color: #008000;"># later though...</span>
				<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
				<span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #008000;"># Now I know that the array index of our oldest item is here</span>
		<span style="color: #800080;">$Oldest</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LBound</span> 
&nbsp;
		<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span>.count <span style="color: pink;">-</span> <span style="color: #000000;">1</span>
		<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
		<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
&nbsp;
		<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">-</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-gt</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Ubound</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Lbound</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #000000;">2</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span> <span style="color: pink;">+</span> <span style="color: #000000;">1</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Mid</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span><span style="color: pink;">-</span><span style="color: #000000;">1</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">else</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$Ubound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
				<span style="color: #800080;">$LBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Mid</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #800080;">$Newest</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LBound</span>		
&nbsp;
		<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$Endtime</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$Newest</span><span style="color: pink;">--</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$Oldest</span><span style="color: pink;">++</span>
		<span style="color: #000000;">&#125;</span>	
&nbsp;
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$StartTime</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span><span style="color: #000000;">&#93;</span>.TimeGenerated <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$EndTime</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008000;">#No events found during the period requested..</span>
			<span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NULL</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">else</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008000;">#Create a new array and assign it the $Entries indexes ranging from our 'oldest' to our 'newest'</span>
			<span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Entries</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$Oldest</span>..<span style="color: #800080;">$Newest</span><span style="color: #000000;">&#93;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># Logs have rolled... and none were in the specified range.</span>
		<span style="color: #800080;">$EntriesByDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NULL</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">return</span> <span style="color: #800080;">$EntriesByDate</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/vmweavercom/~4/XjX567h718Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/07/binary-search-and-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://vmweaver.com/index.php/2009/07/binary-search-and-powershell/</feedburner:origLink></item>
		<item>
		<title>Powershell and DFSR</title>
		<link>http://feedproxy.google.com/~r/vmweavercom/~3/iD552okVibY/</link>
		<comments>http://vmweaver.com/index.php/2009/04/powershell-and-dfsr/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 16:22:56 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[dfsr]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=126</guid>
		<description><![CDATA[Sorry for the long delay between posts, but work has been absolutely crazy.
Anyway, one of the recent tasks I have been working on is to find a way to check DFSR to make sure that our remote sites are properly replicating data back to our corporate datacenter.  Part of this new infrastructure relies heavily on [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry for the long delay between posts, but work has been absolutely crazy.</p>
<p>Anyway, one of the recent tasks I have been working on is to find a way to check DFSR to make sure that our remote sites are properly replicating data back to our corporate datacenter.  Part of this new infrastructure relies heavily on Microsoft DFSR and all the cool stuff it brings (in 2003 R2).</p>
<p>Our support teams have been asking for ways to ensure that data has completely synchronized to our corporate datacenter every night.  Unfortunately there isn&#8217;t an easy way to determine this scriptomatically.  Well leave it to me to try some different things and attempt to put SOMETHING in place to do this.</p>
<p>Basically we have remote sites replicating during non-business-hours back to a central &#8220;hub&#8221; DFSR server.  We would then backup this &#8220;hub&#8221; server with our corporate backup infrastructure.  This is a WHOLE lot easier than getting users in remote sites to swap tapes or whatever and send them offsite, etc. </p>
<p>The only way I have been able to determine the state of replication is to query the &#8220;backlog&#8221; of the remote site DFSR servers.  This should tell us how many files are sitting there awaiting replication. DFSRDIAG is a tool that can help us enumerate these files, but then we have to parse out the data.  We also need to know which replication partner, which replicated folder, and which replication group these remote sites belong to.</p>
<p>One way to enumerate that info is through a WMI query.  From the DFSR &#8220;hub&#8221; server you can enumerate all DFSR connections, groups, folders, etc. by running some queries against the &#8220;MicrosoftDFS&#8221; namespace.  This is different from standard WMI queries because the default namespace (cimv2) does not contain any DFSR configuruations.</p>
<p>Once we connect to this namespace, it is a fairly trivial task to cycle through all the connection partners, replication groups, and replicated folders.</p>
<p>We can then run the &#8220;DFSRDIAG&#8221; tool to see how many files are in the backlog.</p>
<p>Once we have determine how many files are out there for each replicated folder, we then write a custom event log entry and have our monitoring tools pick those up.</p>
<p>For this script I have set a threshold of 10 files before writing an &#8220;error&#8221; event log.  This can easily be changed based on your specific needs, though.  </p>
<p>You should also be able to easily customize the eventIDs and source information by modifying the values assigned to those variables.</p>
<p>For actually writing to the event log, I am &#8220;borrowing&#8221; some code my colleauge <a title="Mike Hays' blog site" href="http://blog.mike-hays.net" target="_blank">Mike </a>put together.</p>
<p>Anyway, I think the script is fairly self explanitory.  If you need additionaly info or have questions, please let me know.</p>
<p>Thanks and happy scripting&#8230;</p>
<p>&#8211; Mark</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">## Check-DFSR.ps1 script</span>
<span style="color: #008000;">## Written by: Mark A. Weaver</span>
<span style="color: #008000;">## Site: http://www.vmweaver.com</span>
<span style="color: #008000;">## Version: 2.0</span>
<span style="color: #008000;">## Date: 5/7/2009</span>
<span style="color: #008000;">## Purpose: This script will query the local WMI root for DFS replication groups and folders.  </span>
<span style="color: #008000;">##				It will then run DFS utilities to determine the number of files in the backlog on the</span>
<span style="color: #008000;">##          destination partners in the replication group.</span>
<span style="color: #008000;">##          </span>
<span style="color: #008000;">##          This script was written for the spefic use of being run on a centralized DFSR server</span>
<span style="color: #008000;">##          which acts as the HUB for remote office backups.</span>
<span style="color: #008000;">##         </span>
<span style="color: #008000;">##        </span>
<span style="color: #008000;">##          Monitoring Rules can be setup to collect and report on the events being generated.</span>
<span style="color: #008000;">## </span>
<span style="color: #008000;">##          Event information is written to the Application log using the EventIDs at the bottom.</span>
<span style="color: #008000;">## Input: None</span>
<span style="color: #008000;">#############################</span>
<span style="color: #008000;">## Updates:</span>
<span style="color: #008000;">##  20090408 Weaver: Fixed issue where multiple events are generated throughout the execution</span>
<span style="color: #008000;">##  20090408 Weaver: Added BacklogFileCount to event message</span>
<span style="color: #008000;">##  20090409 Weaver: Fixed list of replication connections issue due to change in replication topology</span>
<span style="color: #008000;">##  20090507 Weaver: Added functionality to return results from all partners in the replication</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">######################################################################</span>
<span style="color: #008000;">######################################################################</span>
<span style="color: #008000;"># Write-Event powershell function</span>
<span style="color: #008000;"># Written by Mike Hays</span>
<span style="color: #008000;"># http://blog.mike-hays.net</span>
<span style="color: #008000;">#</span>
<span style="color: #008000;">#</span>
&nbsp;
<span style="color: #0000FF;">function</span> Write<span style="color: pink;">-</span>Event<span style="color: #000000;">&#40;</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Source</span> <span style="color: pink;">=</span> $<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">throw</span> <span style="color: #800000;">&quot;An event Source must be specified.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$EventId</span> <span style="color: pink;">=</span> $<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">throw</span> <span style="color: #800000;">&quot;An Event ID must be specified.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span>System.Diagnostics.EventLogEntryType<span style="color: #000000;">&#93;</span> <span style="color: #800080;">$EventType</span> <span style="color: pink;">=</span> $<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">throw</span> <span style="color: #800000;">&quot;Event EventType must be specified. (Error, Warning, Information, SuccessAudit, FailureAudit)&quot;</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span>
	<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Message</span> <span style="color: pink;">=</span> $<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">throw</span> <span style="color: #800000;">&quot;An event Message must be specified.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span>
	<span style="color: #800080;">$EventLog</span>
<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;">#Uncommon event logs can be specified (even custom ones), but since that isn't generally</span>
	<span style="color: #008000;">#the desired result, I prevent that here</span>
	<span style="color: #800080;">$acceptedEventLogs</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Application&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;System&quot;</span>
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$eventEventLog</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$eventEventLog</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Application&quot;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: pink;">!</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$acceptedEventLogs</span> <span style="color: #FF0000;">-icontains</span> <span style="color: #800080;">$eventEventLog</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;This function supports writing to the following event logs:&quot;</span> <span style="color: #800080;">$acceptedEventLogs</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Defaulting to Application Eventlog&quot;</span>
		<span style="color: #800080;">$eventEventLog</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Application&quot;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008000;">#Create a .NET object that is connected to the Eventlog</span>
	<span style="color: #800080;">$event</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> System.Diagnostics.Eventlog <span style="color: #008080; font-style: italic;">-argumentlist</span> <span style="color: #800080;">$EventLog</span>
	<span style="color: #008000;">#Define the Source property</span>
	<span style="color: #800080;">$event</span>.Source <span style="color: pink;">=</span> <span style="color: #800080;">$Source</span>
	<span style="color: #008000;">#Write the event to the log</span>
	<span style="color: #800080;">$event</span>.WriteEntry<span style="color: #000000;">&#40;</span><span style="color: #800080;">$Message</span><span style="color: pink;">,</span> <span style="color: #800080;">$EventType</span><span style="color: pink;">,</span> <span style="color: #800080;">$EventId</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">######################################################################</span>
<span style="color: #008000;">######################################################################</span>
<span style="color: #008000;">## Main </span>
<span style="color: #008000;">## Errors written:</span>
<span style="color: #008000;">##   Log File: Application</span>
<span style="color: #008000;">##   Source: Check-DFSR Script</span>
<span style="color: #008000;">##   ID: 9500 - Lists fully replicated replication folders</span>
<span style="color: #008000;">##   ID: 9501 - Lists replication folders with less than the $BacklogErrorLevel files waiting </span>
<span style="color: #008000;">##   ID: 9502 - Lists replication folders with more than the $BacklogErrorLevel files waiting</span>
<span style="color: #008000;">##   ID: 9503 - If a connection is not pingable, this event is written.</span>
&nbsp;
<span style="color: #800080;">$BacklogErrorLevel</span> <span style="color: pink;">=</span> <span style="color: #000000;">10</span> 
&nbsp;
<span style="color: #800080;">$ComputerName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$env</span>:ComputerName
<span style="color: #008000;">## Query DFSR groups from the local MicrosftDFS WMI namespace.</span>
<span style="color: #800080;">$DFSRGroupWMIQuery</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT * FROM DfsrReplicationGroupConfig&quot;</span>
<span style="color: #800080;">$RGroups</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-Namespace</span> <span style="color: #800000;">&quot;root\MicrosoftDFS&quot;</span> <span style="color: #008080; font-style: italic;">-Query</span> <span style="color: #800080;">$DFSRGroupWMIQuery</span>
&nbsp;
&nbsp;
<span style="color: #008000;">## Setup my variables</span>
<span style="color: #800080;">$ping</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Net.NetworkInformation.Ping
<span style="color: #800080;">$SuccessAudit</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Null</span>
<span style="color: #800080;">$WarningAudit</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Null</span>
<span style="color: #800080;">$ErrorAudit</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Null</span>
<span style="color: #800080;">$EventSource</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Check-DFSR Script&quot;</span>
<span style="color: #800080;">$SuccessEventID</span> <span style="color: pink;">=</span> <span style="color: #000000;">9500</span>
<span style="color: #800080;">$WarningEventID</span> <span style="color: pink;">=</span> <span style="color: #000000;">9501</span>
<span style="color: #800080;">$ErrorEventID</span> <span style="color: pink;">=</span> <span style="color: #000000;">9502</span>
<span style="color: #800080;">$NoPingEventID</span> <span style="color: pink;">=</span> <span style="color: #000000;">9503</span>
&nbsp;
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Group</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$RGroups</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008000;">## Cycle through all Replication groups found</span>
	<span style="color: #800080;">$DFSRGFoldersWMIQuery</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT * FROM DfsrReplicatedFolderConfig WHERE ReplicationGroupGUID='&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Group</span>.ReplicationGroupGUID <span style="color: pink;">+</span> <span style="color: #800000;">&quot;'&quot;</span>
	<span style="color: #800080;">$RGFolders</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-Namespace</span> <span style="color: #800000;">&quot;root\MicrosoftDFS&quot;</span> <span style="color: #008080; font-style: italic;">-Query</span> <span style="color: #800080;">$DFSRGFoldersWMIQuery</span>
&nbsp;
	<span style="color: #008000;">## Grab all connections associated with a Replication Group</span>
	<span style="color: #800080;">$DFSRConnectionWMIQuery</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$Group</span>.ReplicationGroupGUID <span style="color: pink;">+</span> <span style="color: #800000;">&quot;'&quot;</span>
	<span style="color: #800080;">$RGConnections</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-Namespace</span> <span style="color: #800000;">&quot;root\MicrosoftDFS&quot;</span> <span style="color: #008080; font-style: italic;">-Query</span> <span style="color: #800080;">$DFSRConnectionWMIQuery</span>	
	<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Connection</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$RGConnections</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #800080;">$ConnectionName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Connection</span>.PartnerName.Trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$IsInBound</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Connection</span>.Inbound
		<span style="color: #800080;">$IsEnabled</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Connection</span>.Enabled
&nbsp;
		<span style="color: #008000;">## Do not attempt to look at connections that are Disabled</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$IsEnabled</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$True</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>  
			<span style="color: #008000;">## If the connection is not ping-able, do not attempt to query it for Backlog info</span>
			<span style="color: #800080;">$Reply</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ping</span>.send<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;$ConnectionName&quot;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$reply</span>.Status <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;Success&quot;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
&nbsp;
&nbsp;
				<span style="color: #008000;">## Cycle through the Replication Folders that are part of the replication group and run DFSRDIAG tool to determine the backlog on the connection partners.</span>
				<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Folder</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$RGFolders</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #800080;">$RGName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Group</span>.ReplicationGroupName
					<span style="color: #800080;">$RFName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Folder</span>.ReplicatedFolderName
&nbsp;
					<span style="color: #008000;">## Determine if current connect is an inbound connection or not, set send/receive members accordingly</span>
					<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$IsInBound</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$True</span><span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						<span style="color: #800080;">$SendingMember</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ConnectionName</span>
						<span style="color: #800080;">$ReceivingMember</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ComputerName</span>
					<span style="color: #000000;">&#125;</span>
					<span style="color: #0000FF;">else</span>
					<span style="color: #000000;">&#123;</span>
						<span style="color: #800080;">$SendingMember</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ComputerName</span>
						<span style="color: #800080;">$ReceivingMember</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ConnectionName</span>
					<span style="color: #000000;">&#125;</span>
					   <span style="color: #800080;">$Out</span> <span style="color: pink;">=</span> <span style="color: #800080;">$RGName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;:&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$RFName</span> <span style="color: pink;">+</span>  <span style="color: #800000;">&quot; - S:&quot;</span><span style="color: pink;">+</span><span style="color: #800080;">$SendingMember</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; R:&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ReceivingMember</span> 
					   <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$Out</span>
						<span style="color: #008000;">## Execute the dfsrdiag command and get results back in the $Backlog variable</span>
						<span style="color: #800080;">$BLCommand</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;dfsrdiag Backlog /RGName:'&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$RGName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;' /RFName:'&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$RFName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;' /SendingMember:&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$SendingMember</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; /ReceivingMember:&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ReceivingMember</span>
						<span style="color: #800080;">$Backlog</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Invoke-Expression</span> <span style="color: #008080; font-style: italic;">-Command</span> <span style="color: #800080;">$BLCommand</span>
&nbsp;
						<span style="color: #800080;">$BackLogFilecount</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
						<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$item</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$Backlog</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
							<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$item</span> <span style="color: #FF0000;">-ilike</span> <span style="color: #800000;">&quot;*Backlog File count*&quot;</span><span style="color: #000000;">&#41;</span>
							<span style="color: #000000;">&#123;</span>
								<span style="color: #800080;">$BacklogFileCount</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Item</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;:&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#93;</span>.Trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
							<span style="color: #000000;">&#125;</span>
&nbsp;
						<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
						<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$BacklogFileCount</span> <span style="color: #FF0000;">-eq</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
							<span style="color: #008000;">#Update Success Audit </span>
							<span style="color: #800080;">$SuccessAudit</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$RGName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;:&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$RFName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; is in sync with 0 files in the backlog from &quot;</span><span style="color: pink;">+</span> <span style="color: #800080;">$SendingMember</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; to &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ReceivingMember</span> <span style="color: pink;">+</span><span style="color: #800000;">&quot;.<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span>					
&nbsp;
						<span style="color: #000000;">&#125;</span>
						<span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$BacklogFilecount</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$BacklogErrorLevel</span><span style="color: #000000;">&#41;</span>
						<span style="color: #000000;">&#123;</span>
							<span style="color: #008000;">#Update Warning Audit</span>
							<span style="color: #800080;">$WarningAudit</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$RGName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;:&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$RFName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; has &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$BacklogFileCount</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; files in the backlog from &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$SendingMember</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; to &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ReceivingMember</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;.<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span>
						<span style="color: #000000;">&#125;</span>
						<span style="color: #0000FF;">else</span>
						<span style="color: #000000;">&#123;</span>
							<span style="color: #008000;">#Update Error Audit</span>
							<span style="color: #800080;">$ErrorAudit</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$RGName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;:&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$RFName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; has &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$BacklogFilecount</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; files in the backlog from &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$SendingMember</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; to &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ReceivingMember</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;.<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span>
						<span style="color: #000000;">&#125;</span>
						<span style="color: #008000;">#Write-Host + $Folder.ReplicatedFolderName &quot;- &quot; $BackLogFilecount -foregroundcolor $FGColor</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0000FF;">else</span>
				<span style="color: #000000;">&#123;</span> 
				<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$ConnectionName</span> <span style="color: #800000;">&quot;is not pingable&quot;</span> 
				<span style="color: #800080;">$NoPingMessage</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Server &quot;</span><span style="color: #800000;">&quot;&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$ConnectionName</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot;&quot;</span><span style="color: #800000;">&quot; could not be reached.<span style="color: #008080; font-weight: bold;">`n</span>Please verify it is on the network and pingable.&quot;</span>
				Write<span style="color: pink;">-</span>Event <span style="color: #800080;">$EventSource</span> <span style="color: #800080;">$NoPingEventID</span> <span style="color: #800000;">&quot;Warning&quot;</span> <span style="color: #800080;">$NoPingMessage</span> <span style="color: #800000;">&quot;Application&quot;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
<span style="color: #008000;">## Write my events to the local Application log.</span>
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$SuccessAudit</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800080;">$Null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Write<span style="color: pink;">-</span>Event <span style="color: #800080;">$EventSource</span> <span style="color: #800080;">$SuccessEventID</span> <span style="color: #800000;">&quot;Information&quot;</span> <span style="color: #800080;">$SuccessAudit</span> <span style="color: #800000;">&quot;Application&quot;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$WarningAudit</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800080;">$Null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Write<span style="color: pink;">-</span>Event <span style="color: #800080;">$EventSource</span> <span style="color: #800080;">$WarningEventID</span> <span style="color: #800000;">&quot;Warning&quot;</span> <span style="color: #800080;">$WarningAudit</span> <span style="color: #800000;">&quot;Application&quot;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$ErrorAudit</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800080;">$Null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Write<span style="color: pink;">-</span>Event <span style="color: #800080;">$EventSource</span> <span style="color: #800080;">$ErrorEventID</span> <span style="color: #800000;">&quot;Error&quot;</span> <span style="color: #800080;">$ErrorAudit</span> <span style="color: #800000;">&quot;Application&quot;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/vmweavercom/~4/iD552okVibY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/04/powershell-and-dfsr/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		<feedburner:origLink>http://vmweaver.com/index.php/2009/04/powershell-and-dfsr/</feedburner:origLink></item>
		<item>
		<title>Zen and the Mystical Art of Candidacy Analysis</title>
		<link>http://feedproxy.google.com/~r/vmweavercom/~3/U11Q_Y_-Wa0/</link>
		<comments>http://vmweaver.com/index.php/2009/03/zen-and-the-mystical-art-of-candidacy-analysis/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 03:54:34 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Virtualization]]></category>
		<category><![CDATA[candidacy analysis]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=116</guid>
		<description><![CDATA[Okay so maybe there is no Zen, nor is it really &#8220;mystical&#8221;. 
One of the MANY challenges we faced while trying to implement a brand-spanking-new virtualization infrastructure was to put together some type of guidance for our server admin teams to help them determine if existing systems would &#8220;live&#8221; happily in our new environment.
After reading recommendations from VMware, digging [...]]]></description>
			<content:encoded><![CDATA[<p>Okay so maybe there is no Zen, nor is it really &#8220;mystical&#8221;. </p>
<p>One of the MANY challenges we faced while trying to implement a brand-spanking-new virtualization infrastructure was to put together some type of guidance for our server admin teams to help them determine if existing systems would &#8220;live&#8221; happily in our new environment.</p>
<p>After reading recommendations from VMware, digging through Microsoft Management Packs for Operations Manager, etc., it didn&#8217;t seem that any of those really fit our needs nor did they make much sense.</p>
<p>So we happen to use Operations Manager to collect performance data on our Windows-based servers, but there is a TON of data.</p>
<p>We also had to make sure that the tools we were using (Operations Manager) didn&#8217;t aggregate our data.  We NEEDED to have granular data for our entire 3 month look-back period.  We just didn&#8217;t like the idea of averaging averages, so we have data points every 5 or 15 minutes (depending on the counter) for 90 days.  One of the early reports we developed let admins specify the peak usage times so that the performance analysis could be tailored for specific systems.</p>
<p>Now we have all of this data but how do we look at it?  How do we know what&#8217;s important?  What recommendations from VMware and MS do we use and which ones do we throw away?</p>
<p>Several of the deficiencies we saw with most tools are:</p>
<ol>
<li>They average entire periods of time (a day, a week, a month) with no regard for peak usage times</li>
<li>They use aggregated data (kind of like above)</li>
<li>They assume that &#8220;CPU %&#8221; means the same thing for all systems</li>
</ol>
<p>So now we have to look only at peak times for specific servers.  Wow..that can be a VERY daunting task unless you make SOME assumptions.  That&#8217;s exactly what we did.</p>
<p>We know that not ALL systems will have the same usage patterns, but generally our systems will be the busiest during normal business hours at our corporate data center.  So, that would be Monday &#8211; Friday, 8am-5pm CST.  Well, we also have people using our systems on the east and west coasts, so if we move our &#8220;peak&#8221; time to 7am-7pm CST, we should cover most of the usage we wish to capture.</p>
<p>You  may be asking why not just take all data points and be done with it&#8230; I know *WE* did.</p>
<p>It would certainly have been easier, but far less accurate.  To demonstrate this, think about this:</p>
<ul>
<li>A system has a CPU utilization of 75% all the time during peak usage times</li>
<li>This same system has a CPU utilization of 10% during off-peak times</li>
<li>This system has a CPU utilization of 10% during the ENTIRE weekend</li>
</ul>
<p>Okay, so if we take the mean average (the normal practice) we get something in the range of&#8230;well.. I am not gonna do the math, but it is WAY less than 75%.  We THEN thought, well&#8230; why not just take the highest or maximum measurement for the entire period and plan for that?  That doesn&#8217;t work out so well either if you have systems that don&#8217;t do ANYTHING, then a virus scan kicks off and pegs the CPU for 3o minutes.  This greatly throws our performance footprint off.  So now we have to find a happy sweet-spot for evaluating performance.</p>
<p>After mulling through all of our options and thinking about what types of data were REALLY helpful in this process, we decided to do a &#8220;Top 20% Average&#8221;.  I know it sounds a little strange, but it is pretty straight-forward. How did we come up with this number?  Well, we had to pick a number and threw some others around and this one seems to work, so we stuck with it.  The calculation is pretty simple:</p>
<ol>
<li>Take all of your data points</li>
<li>Sort them High to Low</li>
<li>Take the top 20% of your values and average them</li>
</ol>
<p>This gives us a nice look at what our systems do when they are operating at or near their highest loads.  The benefit of having a Top 20% Average and its normal average is that the greater the difference between the two, the &#8220;more spiky&#8221; the performance is.  As the numbers get closer and closer we can see that our systems are more consistently busy during our peak usage time.</p>
<p>The two most important things we need to be looking at are CPU and Memory utilization since those are often the most limiting resources in our environment.  Of less importance to us were Disk IO and Network IO because we were really looking at &#8220;low hanging fruit&#8221; types of systems and NORMALLY if a system is disk or network heavy, CPU utilization will also shoot up.</p>
<p>So now we have this notion of Top 20% Average (T20 Average) and Average, but what are we actually analyzing?</p>
<h2><span style="text-decoration: underline;">Not So Normal CPU Usage</span></h2>
<p>Well, we can&#8217;t just look at CPU Utilization Percent!  Why, you may ask? Well CLEARLY the following are not equivalent:</p>
<ul>
<li>1 CPU &#8211; 700MHz system running 75% utilization</li>
<li>4 CPU &#8211; 2.2 GHz system running 20% utilization</li>
</ul>
<p>Many tools used to evaluate this type of data look at Total CPU Percent Utilization.  This would mean our 1 CPU system has a larger processor footprint than our 4 CPU system.  This doesn&#8217;t really make sense.</p>
<p>One of the interesting strategies VMware presents in some of their documentation is this idea of a &#8220;normalized&#8221; CPU.   Basically this is kind of calculating how many megahertz  a system uses instead of using a percentage (which is relative to how many horses are under the hood).  While this isn&#8217;t entirely accurate, it does help us create high-water marks for a normalized calculation.</p>
<p>In the above example, our 1CPU system would have a normalized CPU usage of :</p>
<ul>
<li>1 CPU x 700MHz  x .75 = <strong>525MHz</strong></li>
</ul>
<p>Our 4CPU system would normalize to :</p>
<ul>
<li>4 CPU x 2200MHz x .20 = <strong>1760MHz</strong></li>
</ul>
<p>It is MUCH easier to compare the 2 of them now and to see which one will probably have a larger processor footprint.  I do realize that it doesn&#8217;t really take into account performance of multi-threaded applications and such, but it does a pretty good job.</p>
<p>So, how do we get all that info to calculate it? Well, we also happen to have SMS or SCCM available to collect configuration information about our systems.  This enables us to create some custom database views that contained system information regarding CPU Speeds, number of CPUs,  Hyperthreading configuration, etc.  We needed to have info on Hyper-Threading enabled hardware because Windows actually reports those &#8220;HT&#8221; processors as physical processors.  Unfortunately a  HT processor doesn&#8217;t really have the same performance as a TRUE physical processor.</p>
<p>To get around this, we figure a HT Processor is about a half of a physical processor.  So if our 4CPU system above is really a 2CPU system with HT, it would look like this:</p>
<ul>
<li>2 CPU x 2200MHz x .20 = <strong>880MHz</strong></li>
<li>2 CPU x .5 (because they are HT)  x 2200MHz x .20 = <strong>440MHz</strong></li>
<li>Total Normalized CPU usage is <strong>1320MHz</strong></li>
</ul>
<p>I hope I did an okay job of explaining it.  If not, please let me know.</p>
<h2><span style="text-decoration: underline;">Memory and Commitment</span></h2>
<p>Memory is VERY easy to evaluate compared to CPU numbers as it is already normalized (kinda).  We will use the same methodologies for memory as we did for CPU with respect to Average Values and peak times.  When looking at memory counters, we are interested in finding out how much memory our systems are using, not how much is free, or the percent free or anything like that.</p>
<p>As Microsoft calls it, we need the &#8220;<strong>Committed Bytes in Use</strong>&#8221; counter.   I don&#8217;t think Operations Manager collects this info by default, so you may need to start collecting it to do this type of analysis.</p>
<p>Other than using the right counter it is pretty much the same as CPU.</p>
<h2><span style="text-decoration: underline;">Disk and Network Stuff</span></h2>
<p>While we DO care about network and disk throughput, they typically will be weighted much less than CPU and memory utilization when looking at the overall analysis.  If you are looking at virtualizing some beefy SQL or Exchange systems, then these will become MUCH more important, but probably still following memory and cpu.</p>
<p>We use the same Average calculations as we do for CPU and Memory, but we will look at the disk counter &#8220;Disk Bytes per Second&#8221;  for this analysis and<br />
&#8220;Total Bytes per second&#8221;(I think) for the network counter.  It MAY be important to eliminate loopback adapters in the query for network data, but most of the time our data comes back with numbers that are WAY below our thresholds for candidacy on even relatively busy systems.</p>
<h2><span style="text-decoration: underline;">Whew!!!</span></h2>
<p>So, I made it through this in one fell swoop.  I am SURE that it isn&#8217;t written perfectly, but I wanted to get my thoughts down on this topic.  If you have any questions about it or recommendations&#8230;.. PLEASE comment and open a dialogue.</p>
<p>Thanks for making it through this&#8230; and good luck.</p>
<p>&#8211; Mark</p>
<img src="http://feeds.feedburner.com/~r/vmweavercom/~4/U11Q_Y_-Wa0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/03/zen-and-the-mystical-art-of-candidacy-analysis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://vmweaver.com/index.php/2009/03/zen-and-the-mystical-art-of-candidacy-analysis/</feedburner:origLink></item>
		<item>
		<title>Powershell Power! (Part Deux)</title>
		<link>http://feedproxy.google.com/~r/vmweavercom/~3/ozz0lBoVgWU/</link>
		<comments>http://vmweaver.com/index.php/2009/02/powershell-power-part-deux/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 00:54:39 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=85</guid>
		<description><![CDATA[Okay, so this is the second installment of my little tutorial-thingy for Powershell.  After much thought on where I want this to go, I figured the next logical step would be to talk about setting up your Powershell (&#8220;PoSh&#8221;) environment.
So, let&#8217;s put together a little laundry-list of tools you may want to procure.  Just download [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, so this is the second installment of my little tutorial-thingy for Powershell.  After much thought on where I want this to go, I figured the next logical step would be to talk about setting up your Powershell (&#8220;PoSh&#8221;) environment.</p>
<p>So, let&#8217;s put together a little laundry-list of tools you may want to procure.  Just download the bits and save them for later.  We will go through some of these in more detail.</p>
<ol>
<li><a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx" target="_blank">Powershell installation bits </a>(requires at least .NET 2.0 Framework)</li>
<li><a href="http://thepowershellguy.com/blogs/posh/pages/powertab.aspx" target="_blank">PowerTab from ThePowerShellGuy</a> (Most EXCELLENT Site for all things PoSh)</li>
<li><a href="http://www.powergui.org/downloads.jspa" target="_blank">Quest PowerGUI </a>(A powerful FREE GUI and IDE for PoSh) I won&#8217;t cover this installation here, but it is pretty straight forward.</li>
</ol>
<p>That will probably be enough to get moving.</p>
<p>The Powershell install should be fairly straight forward.  Just take all the default options and let-her-role.</p>
<p>NOTE: If you want to install a newer version of Powershell, you will need to Uninstall the previous version.  This is kinda sucky, but not too bad.  To Uninstall it, you can go to Add/Remove Programs and look for it, but you may not see it if you have &#8220;Show Updates&#8221; option off.  It probably will show up as &#8220;Window Powershell&#8221;.</p>
<p>To install PowerTab, unzip the downloaded file to a folder you want it to live permanently.  I normally pick something like &#8220;C:\Program Files\PowerTab&#8221; so it is where all my other programs live.</p>
<p>Now open a command shell and start Powershell by typing in &#8220;powershell&#8221; and hitting Enter.</p>
<p>This may take a few seconds to launch, but if successful you should see your new and shiny Powershell prompt.   The first thing to do before anything else make it so we can actually run scripts.</p>
<p>Out-of-the-box, Powershell is delivered secure.  SO secure, in fact, that you can NOT run ANY scripts without making it a little less secure.  I will probably discuss this in more detail later or pressure my buddy <a title="http://blog.mike-hays.net/powershell-execution-policy-remotesigned/" href="http://blog.mike-hays.net/powershell-execution-policy-remotesigned/" target="_blank">Mike to write about it on his blog</a> (this is more up his alley than mine).</p>
<p>Anyway&#8230;at your PoSh prompt type in the cmdlet:</p>
<p><strong>Set-ExecutionPolicy RemoteSigned</strong></p>
<p>If successful, you should be kicked back to your prompt.  Now we can move forward with the PowerTab install.</p>
<p>CD to the directory you install PowerTab to and run the &#8220;Setup.CMD&#8221; file to start the installer.  I normally just take all the defaults and let it go.  This will probably take a few minutes to finish up.</p>
<p>Now to show you a little about what PowerTab does for you&#8230;.</p>
<p>If you type &#8220;Get-&#8221; and then hit the TAB key you should see a popup window with all sorts of fun things.  Basically PowerTab is like tab completion on STEROIDS.  It is very helpful for discovering what is available and such.  I hope you will take this opportunity to explore Powershell a bit.</p>
<p>You can get a list of all commands (called Commandlets) by typing in &#8220;Get-Command&#8221;.</p>
<p>I guess that is a fairly good place to break and talk about the &#8220;Cmdlets&#8221; (said CommandLets).</p>
<p>Cmdlets are the meat-and-potatoes of Powershell.  They are to Powershell what &#8220;cd&#8221; and &#8220;dir&#8221; are to the normal Windows command shell (cmd.exe).  Cmdlet names are constructed of 2 parts: a VERB and a NOUN.  If you look at the output of the &#8220;Get-Command&#8221; cmdlet, you will see lots of verbs on the left side of the &#8220;dash&#8221; in the cmdlet name: &#8220;Get&#8221;, &#8220;Out&#8221;, &#8220;Write&#8221;, etc.  The right side of the cmdlet name is the noun, or the thing that the verb acts on.</p>
<p>As you can see there are common Verbs and Nouns.  You can, in fact, do a &#8220;get-command -verb get&#8221; to list all commands that have the verb &#8220;Get&#8221;.  You can do the same for Nouns.  Go ahead and try that out on a few.</p>
<p>There are several cmdlets you should be using as you are learning the ins and outs of Powershell.  Probably the MOST helpful is, well &#8220;Get-Help&#8221;.</p>
<p>Get-Help is one I use on a VERY regular basis.  The general format for this one is:</p>
<ul>
<li>&#8220;Get-Help &lt;Cmdlet&gt;&#8221;    : Gives basic info on the cmdlet</li>
<li>&#8220;Get-Help &lt;Cmdlet&gt; -Detailed&#8221;  : Gives  MUCH more info about the Cmdlet including some examples on using the cmdlet</li>
<li>&#8220;Get-Help &lt;Cmdlet&gt; -Full&#8221; : I think this gives ALL the info about the Cmdlet</li>
<li>&#8220;Get-Help &lt;Cmdlet&gt; -Examples&#8221; : Just gives you the usage examples.</li>
</ul>
<p>Go ahead and try it now.  Do a &#8220;get-command&#8221;,  pick one that looks interesting to you and &#8220;get-help&#8221; on it.  This was crucial for me in learning the Powershell.</p>
<p>While I bring this part to a close I will challenge you to write something&#8230;well ANYTHING really.  The best way for me in learning was to pick a task and do it in Powershell.  The thought process for me was &#8220;Well, I could do this in vbScript in like 5 minutes, or I could take 30 minutes and do it in Powershell and learn a ton.&#8221;  If I have the time to do it in Powershell, I am doing it.</p>
<p>Some simple tasks would be to start using Powershell as your &#8220;normal&#8221; command shell.  This will get you used to using the cmdlets to navigate the environment and doing things.  Try NOT to use some of the default aliases that are &#8220;replacements&#8221; for the standard &#8220;cmd.exe&#8221; commands. These would include &#8220;type&#8221;, &#8220;dir&#8221;, etc.  I have been using the Powershell equivilant aliases&#8230;  &#8220;dir&#8221; becomes &#8220;Get-ChildItem&#8221; or simply &#8220;gci&#8221;.</p>
<p>The other thing I will challenge you to look at is the cmdlet &#8220;Get-Member&#8221;.  This will help you in this worthwhile venture into the wonderful world of Powershell.</p>
<p>I think the next session we will cover the notion that Powershell is &#8220;Object-Based&#8221;, which will be a good lead in from your &#8220;homework&#8221; on the &#8220;Get-Member&#8221; cmdlet.</p>
<p>Anyway, until next time&#8230;. happy Scripting!</p>
<p>&#8211; Mark</p>
<img src="http://feeds.feedburner.com/~r/vmweavercom/~4/ozz0lBoVgWU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/02/powershell-power-part-deux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://vmweaver.com/index.php/2009/02/powershell-power-part-deux/</feedburner:origLink></item>
		<item>
		<title>Powershell Power! (Part the First)</title>
		<link>http://feedproxy.google.com/~r/vmweavercom/~3/C6FYPuOZp48/</link>
		<comments>http://vmweaver.com/index.php/2009/02/powershell-power-part-the-first/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 04:57:37 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=57</guid>
		<description><![CDATA[This will (hopefully) be the first of several &#8216;tutorials&#8217; on Powershell.
Many of you have probably seen code in the forums or tutorials online that show some exerpts of code that are just CRAZY!  Many containing aliases like &#8220;%&#8221; and &#8220;?&#8221; and lots of braces and parentheses of all shapes and sizes.
Well, one of the things [...]]]></description>
			<content:encoded><![CDATA[<p>This will (hopefully) be the first of several &#8216;tutorials&#8217; on Powershell.</p>
<p>Many of you have probably seen code in the forums or tutorials online that show some exerpts of code that are just CRAZY!  Many containing aliases like &#8220;%&#8221; and &#8220;?&#8221; and lots of braces and parentheses of all shapes and sizes.</p>
<p>Well, one of the things I love about Powershell is that you can do some VERY powerful, yet hard-to-read things at the command line.  While this may seem super and efficient for someone who knows Powershell, it SUCKS if you are trying to learn the language.</p>
<p>When I was trying to pick it up, this is all you could really find and it wasn&#8217;t until I found (what I believe to be the Powershell BIBLE) <a href="http://www.amazon.com/gp/product/1932394907?ie=UTF8&#038;tag=vmweavercom-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=1932394907">Windows PowerShell in Action by Bruce Payette.</a><img src="http://www.assoc-amazon.com/e/ir?t=vmweavercom-20&#038;l=as2&#038;o=1&#038;a=1932394907" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>
<p>In my mind there are ideally two different styles to Powershell(ing).</p>
<ul>
<li>The free-form, Wild-Wild-West,  I-need-to-pound-this-out  command line style</li>
<li>The structured, methodical, EXPLICIT scripting style</li>
</ul>
<p>From a practical sense, though, I think most people end up somewhere in the middle.  What absolutely drives me bonkers is obscure-one-liners that veteran Powershell scripters spew to  the newbie scripters asking relatively simple questions like:</p>
<p>&#8220;How  do I get a list of all files in a directory that have been modified in the last 3 weeks?&#8221;</p>
<p>One may be inclined to say &#8220;Oh, that&#8217;s easy!  Here it is&#8230;&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">gci</span> <span style="color: pink;">|</span>sort<span style="color: pink;">|</span> <span style="color: pink;">%</span><span style="color: #000000;">&#123;</span><span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000080;">$_</span>.LastWriteTime <span style="color: #FF0000;">-GT</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">get-date</span><span style="color: #000000;">&#41;</span>.Adddays<span style="color: #000000;">&#40;</span><span style="color: pink;">-</span><span style="color: #000000;">21</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> `
<span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">write-host</span> <span style="color: #000080;">$_</span>.name <span style="color: #800000;">&quot;--&quot;</span>  <span style="color: #000080;">$_</span>.LastWriteTime <span style="color: pink;">-</span>foreground Red<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span></pre></div></div>

<p>But for someone unfamiliar with Powershell, it looks like a bunch of gibberish.  Now something like THIS may be more helpful for them:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$AllFiles</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Sort-Object</span>
<span style="color: #800080;">$Now</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span>
<span style="color: #800080;">$CutoffDate</span> <span style="color: pink;">=</span> <span style="color: #800080;">$Now</span>.AddDays<span style="color: #000000;">&#40;</span><span style="color: pink;">-</span><span style="color: #000000;">21</span><span style="color: #000000;">&#41;</span>
<span style="color: #0000FF;">Foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$File</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$AllFiles</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     <span style="color: #0000FF;">If</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$File</span>.LastWriteTime <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$CutoffDate</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$File</span>.Name <span style="color: #800000;">&quot;--&quot;</span> <span style="color: #800080;">$File</span>.LastWriteTime <span style="color: pink;">-</span>Foreground Red
      <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Okay, if someone is asking this question, they are certainly new to the language and so an obscure response will probably only confuse them and, hopefully, not taint their desire to learn this robust and relatively simple tool.</p>
<p>ANYWAY&#8230; sorry about that half rant/half informational bit&#8230;Let&#8217;s talk a little bit about Powershell&#8230;</p>
<p>What *IS* Powershell?</p>
<p>Well the quick-and-dirty is that it is a &#8220;new&#8221; scripting language and command shell geared for IT administrators with a focus on simplicity, power, and automation.</p>
<p>The &#8220;official&#8221; description can be found here: <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx">http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx</a></p>
<p>Anyway, now that you have an idea of <em>what</em> Powershell is, we will take a peak at some of the easy stuff you can do with it.  I will probably start with some of the fundamentals of scripting and move forward from that.</p>
<p>That&#8217;s all for now, but I will write more shortly.  I still haven&#8217;t figured out what format I want to use, but we will figure it out together.</p>
<p>Thanks for reading, sorry for the mild rant, and happy shelling!</p>
<p>&#8211; Mark</p>
<img src="http://feeds.feedburner.com/~r/vmweavercom/~4/C6FYPuOZp48" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/02/powershell-power-part-the-first/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://vmweaver.com/index.php/2009/02/powershell-power-part-the-first/</feedburner:origLink></item>
		<item>
		<title>Get-HACapacity</title>
		<link>http://feedproxy.google.com/~r/vmweavercom/~3/x4WmoSr7OBA/</link>
		<comments>http://vmweaver.com/index.php/2009/02/get-hacapacity/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 02:26:51 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[capacity]]></category>
		<category><![CDATA[cluster]]></category>
		<category><![CDATA[ha]]></category>
		<category><![CDATA[vmware]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=46</guid>
		<description><![CDATA[This script will calculate the HighAvailability capacity of a VMware ESX Cluster.

### Written by Mark A. Weaver
##  Date: 7/27/2008
##  Version: 1.0
##  Website: www.vmweaver.com
##
##  Call this function and pass in -ServerName  -ClusterName
##  Output should be an object containing the information
##
##  Feel free to modify as needed to suit your [...]]]></description>
			<content:encoded><![CDATA[<p>This script will calculate the HighAvailability capacity of a VMware ESX Cluster.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">### Written by Mark A. Weaver</span>
<span style="color: #008000;">##  Date: 7/27/2008</span>
<span style="color: #008000;">##  Version: 1.0</span>
<span style="color: #008000;">##  Website: www.vmweaver.com</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##  Call this function and pass in -ServerName  -ClusterName</span>
<span style="color: #008000;">##  Output should be an object containing the information</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##  Feel free to modify as needed to suit your needs, but please keep this header</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##  Thanks  -- Mark</span>
&nbsp;
<span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>HACapacity<span style="color: #000000;">&#40;</span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ServerName</span><span style="color: pink;">,</span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ClusterName</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$ServerName</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$ClusterName</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># These booleans tell me if I am using the VMware default memory and cpu reservations for the cluster.</span>
			<span style="color: #800080;">$DASMemDefault</span> <span style="color: pink;">=</span> <span style="color: #800080;">$True</span>
			<span style="color: #800080;">$DASCPUDefault</span> <span style="color: pink;">=</span> <span style="color: #800080;">$True</span>
&nbsp;
		<span style="color: #008000;"># The following numbers are derived from VMware published numbers for memory overhead.</span>
		<span style="color: #008000;"># I have dropped them into arrays using the number of vCPUs as an index to get the correct constant.</span>
		<span style="color: #008000;"># This is why you will notice only [1], [2], and [4] have non-zero values</span>
		<span style="color: #008000;"># These constants are used later on when calculating Memory Reserve.</span>
			<span style="color: #800080;">$MemConst32</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span><span style="color: pink;">,</span> <span style="color: #000000;">3.262</span><span style="color: pink;">,</span> <span style="color: #000000;">5.769059</span><span style="color: pink;">,</span> <span style="color: #000000;">0</span><span style="color: pink;">,</span> <span style="color: #000000;">6.77933</span>
			<span style="color: #800080;">$MemConst64</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span><span style="color: pink;">,</span> <span style="color: #000000;">3.2678</span><span style="color: pink;">,</span> <span style="color: #000000;">5.79251</span><span style="color: pink;">,</span> <span style="color: #000000;">0</span><span style="color: pink;">,</span> <span style="color: #000000;">6.82622</span>
			<span style="color: #800080;">$MemBase32</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span><span style="color: pink;">,</span> <span style="color: #000000;">87.56</span><span style="color: pink;">,</span> <span style="color: #000000;">108.73</span><span style="color: pink;">,</span> <span style="color: #000000;">0</span><span style="color: pink;">,</span> <span style="color: #000000;">146.75</span>
			<span style="color: #800080;">$MemBase64</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span><span style="color: pink;">,</span> <span style="color: #000000;">107.54</span><span style="color: pink;">,</span> <span style="color: #000000;">146.41</span><span style="color: pink;">,</span> <span style="color: #000000;">0</span><span style="color: pink;">,</span> <span style="color: #000000;">219.82</span>
&nbsp;
		<span style="color: #008000;"># Initialize some Variables</span>
			<span style="color: #800080;">$MaxMemRes</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
			<span style="color: #800080;">$MacNumCPU</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
			<span style="color: #800080;">$MaxCPUResVM</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>
			<span style="color: #800080;">$VMCount</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
&nbsp;
		<span style="color: #008000;"># define default memory and cpu reservation</span>
			<span style="color: #800080;">$DASMinMHz</span> <span style="color: pink;">=</span> <span style="color: #000000;">256</span>
			<span style="color: #800080;">$DASMinMemory</span> <span style="color: pink;">=</span> <span style="color: #000000;">256</span>
&nbsp;
			<span style="color: #800080;">$viServerName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ServerName</span>
			<span style="color: #800080;">$viClusterName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ClusterName</span>
&nbsp;
		<span style="color: #008000;"># Connect to the VirtualCenter Server and get some info</span>
			<span style="color: #800080;">$viServer</span> <span style="color: pink;">=</span> Connect<span style="color: pink;">-</span>VIServer <span style="color: #800080;">$viServerName</span>
			<span style="color: #800080;">$viCluster</span> <span style="color: pink;">=</span> get<span style="color: pink;">-</span>cluster <span style="color: #800080;">$viClusterName</span>
			<span style="color: #800080;">$viHosts</span> <span style="color: pink;">=</span> get<span style="color: pink;">-</span>vmhost <span style="color: pink;">-</span>location <span style="color: #800080;">$viCluster</span>
			<span style="color: #800080;">$viClusterV</span> <span style="color: pink;">=</span> get<span style="color: pink;">-</span>view <span style="color: #800080;">$viCluster</span>.ID
&nbsp;
		<span style="color: #008000;"># Get the &quot;Resources&quot; Resource Pool from the cluster.</span>
		<span style="color: #008000;"># This gives us the Reservation Pools for Memory and CPU</span>
			<span style="color: #800080;">$viResGroup</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>ResourcePool <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800000;">&quot;Resources&quot;</span> <span style="color: pink;">-</span>Location <span style="color: #800080;">$viCluster</span>
			<span style="color: #800080;">$viCPURes</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viResGroup</span>.CpuReservationMHz
			<span style="color: #800080;">$viMemRes</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viResGroup</span>.MemReservationMB
			<span style="color: #800080;">$viHostCount</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viClusterV</span>.Summary.NumHosts
&nbsp;
		<span style="color: #008000;"># Get HA cluster configuration information</span>
			<span style="color: #800080;">$viHostFailures</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viClusterV</span>.Configuration.DasConfig.FailoverLevel
&nbsp;
		<span style="color: #008000;"># Get a list of options that may be configured at the clusters level</span>
		<span style="color: #008000;"># We are looking for whether or not the default memory and cpu</span>
		<span style="color: #008000;">#  reservations have been overridden</span>
			<span style="color: #800080;">$viDASOptions</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viClusterV</span>.Configuration.DASConfig.Option
			<span style="color: #800080;">$viVMs</span> <span style="color: pink;">=</span> get<span style="color: pink;">-</span>vm <span style="color: pink;">-</span>Location <span style="color: #800080;">$viCluster</span>
&nbsp;
		<span style="color: #008000;"># Is Adminisssion Control enabled on the cluster?</span>
			<span style="color: #800080;">$viClusterControl</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viClusterV</span>.Configuration.DASConfig.AdmissionControlEnabled	
&nbsp;
		<span style="color: #008000;"># See if das.vmMemoryMinMB key is defined and grab its value</span>
		<span style="color: #008000;"># See if das.vmCpuMinMHZ key is defined and grab its value</span>
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$viDASoptions</span>.Count <span style="color: #FF0000;">-ne</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$viDASOption</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$viDASOptions</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$viDASOption</span>.Key <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;das.vmMemoryMinMB&quot;</span><span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						<span style="color: #800080;">$DASMemDefault</span> <span style="color: pink;">=</span> <span style="color: #800080;">$False</span>
					<span style="color: #800080;">$DASMinMemory</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viDASOption</span>.Value <span style="color: #000000;">&#125;</span>
&nbsp;
					<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$viDASOption</span>.Key <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;das.vmCpuMinMHz&quot;</span><span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						<span style="color: #800080;">$DASCPUDefault</span> <span style="color: pink;">=</span> <span style="color: #800080;">$False</span>
					<span style="color: #800080;">$DASMinMHz</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viDASOption</span>.Value <span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #008000;"># Let's go through every VM and see what the maximum CPU and Memory reservation is.</span>
		<span style="color: #008000;"># We will also get a count of powered on VMs.</span>
		<span style="color: #008000;"># When we hit a maximum reservation, save the machine name that set that maximum</span>
			<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$viVM</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$viVMs</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$NumCPU</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viVm</span>.NumCPU
				<span style="color: #800080;">$VMMem</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viVm</span>.MemoryMB
				<span style="color: #800080;">$MemRes</span> <span style="color: pink;">=</span> <span style="color: #000000;">0</span>
&nbsp;
				<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$viVM</span>.PowerState <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;PoweredOn&quot;</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #800080;">$VMCount</span> <span style="color: pink;">+=</span> <span style="color: #000000;">1</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #008000;"># Get the VM-view and determine if the current guest CPU or memory reservations configured</span>
			<span style="color: #800080;">$vmView</span> <span style="color: pink;">=</span> get<span style="color: pink;">-</span>view <span style="color: #800080;">$viVM</span>.ID
			<span style="color: #800080;">$vmViewCPURes</span> <span style="color: pink;">=</span> <span style="color: #800080;">$vmView</span>.ResourceConfig.CpuAllocation.Reservation
			<span style="color: #800080;">$vmViewMemRes</span> <span style="color: pink;">=</span> <span style="color: #800080;">$vmView</span>.ResourceConfig.MemoryAllocation.Reservation
&nbsp;
			<span style="color: #008000;"># If no reservations are set at the VM level, calculate the memory reservation.</span>
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$vmViewMemRes</span> <span style="color: #FF0000;">-eq</span> <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$VMMem</span> <span style="color: #FF0000;">-le</span> <span style="color: #000000;">256</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #800080;">$MemRes</span> <span style="color: pink;">=</span> <span style="color: #800080;">$MemConst64</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$NumCpu</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$MemBase64</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$NumCPU</span><span style="color: #000000;">&#93;</span>
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0000FF;">else</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$viVM</span>.Guest.OSFullName <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select-String</span> <span style="color: #800000;">&quot;64-bit&quot;</span><span style="color: #000000;">&#41;</span>.Matches.Count<span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-ge</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>
					<span style="color: #000000;">&#123;</span>
						<span style="color: #800080;">$MemRes</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$VMMem</span> <span style="color: pink;">/</span> <span style="color: #000000;">256</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #800080;">$MemConst64</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$NumCPU</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$MemBase64</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$NumCPU</span><span style="color: #000000;">&#93;</span>
					<span style="color: #000000;">&#125;</span>
					<span style="color: #0000FF;">else</span>
					<span style="color: #000000;">&#123;</span>
						<span style="color: #800080;">$MemRes</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$VMMem</span> <span style="color: pink;">/</span> <span style="color: #000000;">256</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #800080;">$MemConst32</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$NumCPU</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$MemBase32</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$NumCPU</span><span style="color: #000000;">&#93;</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
&nbsp;
				<span style="color: #800080;">$MemRes</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$DASMinMemory</span>
			<span style="color: #000000;">&#125;</span>																			
&nbsp;
			<span style="color: #0000FF;">else</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$MemRes</span> <span style="color: pink;">=</span> <span style="color: #800080;">$vmViewMemRes</span> 									
&nbsp;
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #008000;">#Figure out if the current VM holds the highest reservation so far</span>
&nbsp;
				<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$vmViewCPURes</span> <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$DASMinMHz</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #800080;">$DASMinMHz</span> <span style="color: pink;">=</span> <span style="color: #800080;">$vmViewCPURes</span>
					<span style="color: #800080;">$MaxCPUResVM</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viVM</span>.Name
				<span style="color: #000000;">&#125;</span>			
&nbsp;
				<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$MemRes</span> <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$MaxMemRes</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #800080;">$MaxMemRes</span> <span style="color: pink;">=</span> <span style="color: #800080;">$MemRes</span>
					<span style="color: #800080;">$MaxMemResVM</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viVM</span>.Name
				<span style="color: #000000;">&#125;</span>
&nbsp;
				<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$NumCPU</span> <span style="color: #FF0000;">-gt</span> <span style="color: #800080;">$MaxNumCPU</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					<span style="color: #800080;">$MaxNumCPU</span> <span style="color: pink;">=</span> <span style="color: #800080;">$NumCPU</span>
					<span style="color: #800080;">$MaxCPUNumVM</span> <span style="color: pink;">=</span> <span style="color: #800080;">$viVM</span>.Name
				<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$MaxCPUResVM</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$MaxCPUResVM</span> <span style="color: pink;">=</span> <span style="color: #800080;">$MaxCPUNumVM</span> <span style="color: #000000;">&#125;</span>			
&nbsp;
		<span style="color: #800080;">$MaxCPURes</span> <span style="color: pink;">=</span> <span style="color: #800080;">$MaxNumCPU</span> <span style="color: pink;">*</span> <span style="color: #800080;">$DASMinMHz</span>
&nbsp;
		<span style="color: #008000;"># Calculate the VM Capacity for the cluster based on memory and cpu reservations.</span>
			<span style="color: #800080;">$ClusterVMCapacityMEM</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Truncate<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$viMemRes</span> <span style="color: pink;">/</span> <span style="color: #800080;">$MaxMemRes</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #000000;">&#40;</span> <span style="color: #800080;">$viHostCount</span> <span style="color: pink;">-</span> <span style="color: #800080;">$viHostFailures</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #800080;">$viHostCount</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #800080;">$ClusterVMCapacityCPU</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Truncate<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$viCPURes</span> <span style="color: pink;">/</span> <span style="color: #800080;">$MaxCPURes</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #000000;">&#40;</span> <span style="color: #800080;">$viHostCount</span> <span style="color: pink;">-</span> <span style="color: #800080;">$viHostFailures</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #800080;">$viHostCount</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$ClusterVMCapacityMEM</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$ClusterVMCapacityCPU</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$ClusterVMCapacity</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ClusterVMCapacityMEM</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">else</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$ClusterVMCapacity</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ClusterVMCapacityCPU</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #008000;"># Create an object to return</span>
			<span style="color: #800080;">$CPUObj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Object
			<span style="color: #800080;">$CPUObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> ClusterCPURes <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$viCPURes</span>
			<span style="color: #800080;">$CPUObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> DefaultCPURes <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$DASCPUDefault</span>
			<span style="color: #800080;">$CPUObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> MinCPURes <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$DASMinMHz</span>
		   <span style="color: #800080;">$CPUObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> MaxCPUNumVM <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$MaxCPUNumVM</span>
			<span style="color: #800080;">$CPUObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> MaxCPURes <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$MaxCPURes</span>
			<span style="color: #800080;">$CPUObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> MaxCPUResVM <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$MaxCPUResVM</span>
			<span style="color: #800080;">$CPUObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> MaxCPUs <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$MaxNumCPU</span>
			<span style="color: #800080;">$CPUObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> VMCapacityCPU <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$ClusterVMCapacityCPU</span>
&nbsp;
			<span style="color: #800080;">$MemObj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Object
			<span style="color: #800080;">$MemObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> ClusterMemRes <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$viMemRes</span>
			<span style="color: #800080;">$MemObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> DefaultMemRes <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$DASMemDefault</span>
			<span style="color: #800080;">$MemObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> MinMemRes <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$DASMinMemory</span>
			<span style="color: #800080;">$MemObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> MaxMemRes <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$MaxMemRes</span>
			<span style="color: #800080;">$MemObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> MaxMemResVM <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$MaxMemResVM</span>
			<span style="color: #800080;">$MemObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> VMCapacityMem <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$ClusterVMCapacityMEM</span>
&nbsp;
			<span style="color: #800080;">$OutObj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Object
			<span style="color: #800080;">$OutObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> AdmissionControl <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$viClusterControl</span>
			<span style="color: #800080;">$OutObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> CPU <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$CPUObj</span>
			<span style="color: #800080;">$OutObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> FailoverHosts <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$viHostFailures</span>
			<span style="color: #800080;">$OutObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> HostCount <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$viHostCount</span>
			<span style="color: #800080;">$OutObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> Memory <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$MemObj</span>
			<span style="color: #800080;">$OutObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> RunningVMs <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$VMCount</span>
			<span style="color: #800080;">$OutObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> VIServer <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$viServerName</span>
			<span style="color: #800080;">$OutObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> VICluster <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$viClusterName</span>
			<span style="color: #800080;">$OutObj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">type</span> NoteProperty <span style="color: #008080; font-style: italic;">-name</span> VMCapacity <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$ClusterVMCapacity</span>	
&nbsp;
			<span style="color: #0000FF;">return</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$outObj</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># Write usage info</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;-------------------------------------&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Get-HACapacity.ps1 Usage:&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span><span style="color: #000000;">&#40;</span> <span style="color: #800000;">&quot;You must specify the following parameters: &quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;     '-ServerName '  where  is the name of the VirtualCenter Server&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;     '-ClusterName '  where  is the name of the cluster to query&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Let me know if you have any problems with it or would like more info..</p>
<p>&#8211; Mark</p>
<img src="http://feeds.feedburner.com/~r/vmweavercom/~4/x4WmoSr7OBA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/02/get-hacapacity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://vmweaver.com/index.php/2009/02/get-hacapacity/</feedburner:origLink></item>
	</channel>
</rss>
