<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vmweaver.com &#187; Powershell</title>
	<atom:link href="http://vmweaver.com/index.php/tag/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>http://vmweaver.com</link>
	<description>Mindless ramblings of a geek...</description>
	<lastBuildDate>Thu, 06 Oct 2011 20:42:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Powershell 3.0 &#8211; drool</title>
		<link>http://vmweaver.com/index.php/2011/09/powershell-3-0-drool/</link>
		<comments>http://vmweaver.com/index.php/2011/09/powershell-3-0-drool/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 03:09:20 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=272</guid>
		<description><![CDATA[Okay, so if you live under a rock and haven&#8217;t heard, the CTP for the Windows Management Framework 3.0 has been released by Microsoft (http://www.microsoft.com/download/en/details.aspx?id=27548). I know, you are probably thinking &#8220;Big Deal!!&#8221; another Community Tech Preview. Not so fast there&#8230; THIS CTP includes Powershell 3.0! Let me restate: &#8220;THIS CTP INCLUDES POWERSHELL 3.0!&#8221; What [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, so if you live under a rock and haven&#8217;t heard, the CTP for the Windows Management Framework 3.0 has been released by Microsoft (<a href="http://www.microsoft.com/download/en/details.aspx?id=27548">http://www.microsoft.com/download/en/details.aspx?id=27548</a>).</p>
<p>I know, you are probably thinking &#8220;Big Deal!!&#8221; another Community Tech Preview.  Not so fast there&#8230; THIS CTP includes Powershell 3.0!</p>
<p>Let me restate: &#8220;<strong>THIS CTP INCLUDES POWERSHELL 3.0</strong>!&#8221;</p>
<p>What I hope to do is post more as I begin to learn about what is new, what is cool, what is fresh, etc.</p>
<p>A little story about me and Powershell 3.0 &#8230;.</p>
<p>When I found out about the Windows 8 &#8211; Developer Preview, I wasn&#8217;t excited about the new interface, the new look of familiar applications&#8230; no.  After I found out how to actually get to a command prompt, my first thing was to run Powershell and do a:</p>
<p>                        $host.version</p>
<p>To my UTTER DELIGHT, I saw the number &#8220;3&#8243; in the output.  My colleagues, of course, we interested in why I was ooo-ing and ahhh-ing.. then they saw that I was sitting at a Powershell prompt (of course) and the &#8220;geek&#8221;, &#8220;nerd&#8221;, and &#8220;dork&#8221; comments began.</p>
<p>Anyway, needless to say, I soon discovered I could install Powershell 3 on my Windows 7, SP1 laptop at home so I prompty did so upon arriving home from work.</p>
<p>Wow&#8230;that was a longer little story than I was expecting.</p>
<p>The bottom line is that I finally have it installed and have begun playing with it and I am a TOTAL GEEK.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>One of the things I was afraid of was losing my Powershell 2.0 installation, yet I had read that they could co-exist.  Well, seeing is believing, and I am a believer now.</p>
<p>Launching &#8220;powershell.exe&#8221; from a command line, by default, will open up version 3.</p>
<p>Launching &#8220;powershell.exe -version 2&#8243; from a command line will bring up version 2.</p>
<p>Yes, it really is that simple.</p>
<p>Upon a cursory glance at the cmdlets available I notice that there are many more geared toward command line management of the system and features.</p>
<p><em></p>
<p>       get-controlpanelitem</p>
<p>              show-controlpanelitem &#8220;Display&#8221;</p>
<p>              get-counter</p>
<p>	new-webbinding</p>
<p></em></p>
<p>Also there is a cmdlet that helps you explore all of the cmdlets available via a GUI:</p>
<p>	<em>show-command</em></p>
<p>I have, literally, been playing with the new powershell for about an hour, so stay tuned for more updates on this.</p>
<p>Please, everyone, go grab it, kick the tires, run it through its paces, and poke it with a stick.</p>
<p>As always, happy scripting and have a good time discovering Powershell 3.0.</p>
<p> &#8212; Mark</p>
]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2011/09/powershell-3-0-drool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Give me some DATA! (with Powershell)</title>
		<link>http://vmweaver.com/index.php/2011/03/sql-data-with-powershell/</link>
		<comments>http://vmweaver.com/index.php/2011/03/sql-data-with-powershell/#comments</comments>
		<pubDate>Fri, 18 Mar 2011 02:44:30 +0000</pubDate>
		<dc:creator>Mark A. Weaver</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://vmweaver.com/?p=252</guid>
		<description><![CDATA[Ah&#8230;wow.. has it really been over a YEAR since I last post? Sorry about that, but it has been an absolutely nutty year. Anyway.. In my new job and new role, I get to have fun showing people the power of automation with Powershell. One of the recent tasks I was helping with is to [...]]]></description>
			<content:encoded><![CDATA[<p>Ah&#8230;wow.. has it really been over a YEAR since I last post?  Sorry about that, but it has been an absolutely nutty year.</p>
<p>Anyway..<br />
In my new job and new role, I get to have fun showing people the power of automation with Powershell.<br />
One of the recent tasks I was helping with is to take a list of users (from AD) and query a database to get some info about these users.</p>
<p>While I had done some work with SQL queries with Powershell, I hadn&#8217;t ever &#8220;functionalized&#8221; it, so I decided to take this opportunity to do so.</p>
<p>While I am sure there are a bazillion examples of this out there, this is what I came up with and here is what it looks like.<br />
Sorry there aren&#8217;t many (any) in-line comments like I normally do, but if you have questions, please let me know!<br />
Thanks and hope you find this helpful.</p>
<p>As always&#8230; Happy Scripting!<br />
&#8211; Mark</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">##-------------------------------------------------------------------</span>
<span style="color: #008000;">## Script: Get-SQLData.ps1</span>
<span style="color: #008000;">## Written by: Mark A. Weaver </span>
<span style="color: #008000;">## Website: vmweaver.com</span>
<span style="color: #008000;">## </span>
<span style="color: #008000;">## Version: 1.0</span>
<span style="color: #008000;">## Creation Date: 3/16/2011</span>
<span style="color: #008000;">## Purpose: This is really a function that you pass 3 parameters to. It will connect to the </span>
<span style="color: #008000;">##          SQL Server you specify, to the Database you specify, and execute the</span>
<span style="color: #008000;">##          query you specify.</span>
<span style="color: #008000;">##          This connects with you existing credentials (integrated).</span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##    </span>
<span style="color: #008000;">##   Input: </span>
<span style="color: #008000;">##                      -SQLServer &quot;Name of your sql server&quot;</span>
<span style="color: #008000;">##                      -Database &quot;Name of your database&quot;</span>
<span style="color: #008000;">##                      -Query &quot;your query&quot;</span>
<span style="color: #008000;">##     *Note if you want to have a query that spans multiple lines, etc, you can pass in a </span>
<span style="color: #008000;">##      'here-string' or construct another multi-line (not array) string.</span>
<span style="color: #008000;">##          </span>
<span style="color: #008000;">##</span>
<span style="color: #008000;">##  Output: </span>
<span style="color: #008000;">##      An array is passed back containing the data from your query.</span>
<span style="color: #008000;">##      If no data is returned, the array will be $null</span>
<span style="color: #008000;">##                 </span>
<span style="color: #008000;">##                              </span>
<span style="color: #008000;">##  Assumptions: </span>
<span style="color: #008000;">##               </span>
<span style="color: #008000;">##               </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: #0000FF;">Function</span> Get<span style="color: pink;">-</span>SQLData
<span style="color: #000000;">&#123;</span>
   <span style="color: #0000FF;">param</span> <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;">$SQLServer</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;">$Database</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;">$Query</span><span style="color: #000000;">&#41;</span>    
&nbsp;
        <span style="color: #800080;">$SQLData</span><span style="color: pink;">=@</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>     
        <span style="color: #800080;">$SqlConnection</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SqlConnection
        <span style="color: #800080;">$SqlConnection</span>.ConnectionString <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Server = $SqlServer; Database = $Database; Integrated Security = True&quot;</span>
        <span style="color: #800080;">$SqlCmd</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SqlCommand
        <span style="color: #800080;">$SqlCmd</span>.CommandText <span style="color: pink;">=</span> <span style="color: #800080;">$Query</span>
        <span style="color: #800080;">$SqlCmd</span>.Connection <span style="color: pink;">=</span> <span style="color: #800080;">$SqlConnection</span>
        <span style="color: #800080;">$SqlAdapter</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.SqlClient.SqlDataAdapter
        <span style="color: #800080;">$SqlAdapter</span>.SelectCommand <span style="color: pink;">=</span> <span style="color: #800080;">$SqlCmd</span>
        <span style="color: #800080;">$DataSet</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Data.DataSet
        <span style="color: #800080;">$SqlAdapter</span>.Fill<span style="color: #000000;">&#40;</span><span style="color: #800080;">$DataSet</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>
        <span style="color: #800080;">$SqlConnection</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
        <span style="color: #800080;">$RecordCount</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$DataSet</span>.tables<span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Measure-Object</span><span style="color: #000000;">&#41;</span>.count
        <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$RecordCount</span> <span style="color: #FF0000;">-eq</span> <span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #800080;">$SQLData</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: #800080;">$SQLData</span> <span style="color: pink;">=</span> <span style="color: #800080;">$DataSet</span>.tables<span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span> 
        <span style="color: #000000;">&#125;</span>
  <span style="color: #0000FF;">return</span> <span style="color: #800080;">$SQLData</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2011/03/sql-data-with-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powershell and Unknown User SIDs</title>
		<link>http://vmweaver.com/index.php/2009/10/powershell-and-unknown-user-sids/</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 [...]]]></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: #804000;">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: #804000;">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: #804000;">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: #804000;">0</span><span style="color: #000000;">&#93;</span><span style="color: pink;">,</span> <span style="color: #804000;">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>
]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/10/powershell-and-unknown-user-sids/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powershell &#8211; Recursive Group Membership</title>
		<link>http://vmweaver.com/index.php/2009/08/powershell-recursive-group-membership/</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 [...]]]></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: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</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: #804000;">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: #804000;">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: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</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: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$domain</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;">$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: #804000;">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: #804000;">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: #804000;">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<span style="color: #FF0000;">-File</span></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<span style="color: #FF0000;">-File</span></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>
]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/08/powershell-recursive-group-membership/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Binary Search and Powershell</title>
		<link>http://vmweaver.com/index.php/2009/07/binary-search-and-powershell/</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 [...]]]></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: #804000;">1</span>
<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">1</span>
<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">1</span>
	<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
	<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">1</span>
		<span style="color: #800080;">$Lbound</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
		<span style="color: #800080;">$Mid</span> <span style="color: pink;">=</span> <span style="color: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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>

]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/07/binary-search-and-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powershell and DFSR</title>
		<link>http://vmweaver.com/index.php/2009/04/powershell-and-dfsr/</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 [...]]]></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: #804000;">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: #804000;">9500</span>
<span style="color: #800080;">$WarningEventID</span> <span style="color: pink;">=</span> <span style="color: #804000;">9501</span>
<span style="color: #800080;">$ErrorEventID</span> <span style="color: pink;">=</span> <span style="color: #804000;">9502</span>
<span style="color: #800080;">$NoPingEventID</span> <span style="color: pink;">=</span> <span style="color: #804000;">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: #804000;">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: #804000;">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: #804000;">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>

]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/04/powershell-and-dfsr/feed/</wfw:commentRss>
		<slash:comments>81</slash:comments>
		</item>
		<item>
		<title>Powershell Power! (Part Deux)</title>
		<link>http://vmweaver.com/index.php/2009/02/powershell-power-part-deux/</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>
		<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 [...]]]></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>
]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/02/powershell-power-part-deux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powershell Power! (Part the First)</title>
		<link>http://vmweaver.com/index.php/2009/02/powershell-power-part-the-first/</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>
		<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 [...]]]></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: #804000;">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: #804000;">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>
]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/02/powershell-power-part-the-first/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get-HACapacity</title>
		<link>http://vmweaver.com/index.php/2009/02/get-hacapacity/</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[Virtualization]]></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 [...]]]></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: #804000;">0</span><span style="color: pink;">,</span> <span style="color: #804000;">3.262</span><span style="color: pink;">,</span> <span style="color: #804000;">5.769059</span><span style="color: pink;">,</span> <span style="color: #804000;">0</span><span style="color: pink;">,</span> <span style="color: #804000;">6.77933</span>
			<span style="color: #800080;">$MemConst64</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span><span style="color: pink;">,</span> <span style="color: #804000;">3.2678</span><span style="color: pink;">,</span> <span style="color: #804000;">5.79251</span><span style="color: pink;">,</span> <span style="color: #804000;">0</span><span style="color: pink;">,</span> <span style="color: #804000;">6.82622</span>
			<span style="color: #800080;">$MemBase32</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span><span style="color: pink;">,</span> <span style="color: #804000;">87.56</span><span style="color: pink;">,</span> <span style="color: #804000;">108.73</span><span style="color: pink;">,</span> <span style="color: #804000;">0</span><span style="color: pink;">,</span> <span style="color: #804000;">146.75</span>
			<span style="color: #800080;">$MemBase64</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span><span style="color: pink;">,</span> <span style="color: #804000;">107.54</span><span style="color: pink;">,</span> <span style="color: #804000;">146.41</span><span style="color: pink;">,</span> <span style="color: #804000;">0</span><span style="color: pink;">,</span> <span style="color: #804000;">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: #804000;">0</span>
			<span style="color: #800080;">$MacNumCPU</span> <span style="color: pink;">=</span> <span style="color: #804000;">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: #804000;">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: #804000;">256</span>
			<span style="color: #800080;">$DASMinMemory</span> <span style="color: pink;">=</span> <span style="color: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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: #804000;">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>
]]></content:encoded>
			<wfw:commentRss>http://vmweaver.com/index.php/2009/02/get-hacapacity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

