<?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>Write-Host</title>
	<atom:link href="http://www.write-host.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.write-host.com</link>
	<description>Kind of a PowerShell blog</description>
	<lastBuildDate>Fri, 16 Sep 2011 15:48:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Common solution to zipping files in PoSh</title>
		<link>http://www.write-host.com/2011/09/common-solution-to-zipping-files-in-posh/</link>
		<comments>http://www.write-host.com/2011/09/common-solution-to-zipping-files-in-posh/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 15:48:27 +0000</pubDate>
		<dc:creator>Ben Baird</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.write-host.com/?p=30</guid>
		<description><![CDATA[The code below, or very similar variations, seem to be a very widespread recommendation to adding files to a zip archive in PowerShell: $fileName = 'C:\Temp\test.zip' $source = 'C:\Temp\reallylargefile.txt' Set-Content $fileName &#40;'PK' + &#91;char&#93;5 + &#91;char&#93;6 + &#40;&#34;$([char]0)&#34; * 18&#41;&#41;; &#8230;<p class="read-more"><a href="http://www.write-host.com/2011/09/common-solution-to-zipping-files-in-posh/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>The code below, or very similar variations, seem to be a very widespread recommendation to adding files to a zip archive in PowerShell:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$fileName</span> <span style="color: pink;">=</span> <span style="color: #800000;">'C:\Temp\test.zip'</span>
<span style="color: #800080;">$source</span> <span style="color: pink;">=</span> <span style="color: #800000;">'C:\Temp\reallylargefile.txt'</span>
<span style="color: #008080; font-weight: bold;">Set-Content</span> <span style="color: #800080;">$fileName</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">'PK'</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">char</span><span style="color: #000000;">&#93;</span><span style="color: #804000;">5</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">char</span><span style="color: #000000;">&#93;</span><span style="color: #804000;">6</span> <span style="color: pink;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;$([char]0)&quot;</span> <span style="color: pink;">*</span> <span style="color: #804000;">18</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$oShell</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-ComObject</span> Shell.Application;
<span style="color: #800080;">$zipPackage</span> <span style="color: pink;">=</span> <span style="color: #800080;">$oShell</span>.NameSpace<span style="color: #000000;">&#40;</span><span style="color: #800080;">$fileName</span><span style="color: #000000;">&#41;</span>;
<span style="color: #800080;">$zipPackage</span>.CopyHere<span style="color: #000000;">&#40;</span><span style="color: #800080;">$source</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>There&#8217;s nothing particularly wrong with this code &#8212; it&#8217;s easy, it&#8217;s short, and it&#8217;s effective. However, a number of scripters out there have noticed that there&#8217;s a small problem: The <strong>CopyHere</strong> method returns immediately, rather than waiting for the zip process to complete. If your script wants to do something with that zip file, it&#8217;s got to find a way to know when the zip is free to be moved, renamed, copied, and so on.</p>
<p>Fortunately, there&#8217;s a way to do that. Here&#8217;s the solution I use:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Test<span style="color: pink;">-</span>FileLock <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0000FF;">param</span> <span style="color: #000000;">&#40;</span>
		<span style="color: #000000;">&#91;</span>parameter<span style="color: #000000;">&#40;</span>Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: pink;">,</span>ValueFromPipeline<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: pink;">,</span>ValueFromPipelineByPropertyName<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
		<span style="color: #000000;">&#91;</span>Alias<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;FullName&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
		<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$Path</span><span style="color: pink;">,</span>
		<span style="color: #000000;">&#91;</span><span style="color: #0000FF;">switch</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$PassThru</span>
	<span style="color: #000000;">&#41;</span>
&nbsp;
	begin <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
&nbsp;
	Process
	<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: #008080; font-weight: bold;">Test-Path</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$Path</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: #0000FF;">throw</span> <span style="color: #000000;">&#91;</span>System.IO.FileNotFoundException<span style="color: #000000;">&#93;</span> 
	    	<span style="color: #0000FF;">return</span>
	    <span style="color: #000000;">&#125;</span>
&nbsp;
	    <span style="color: #800080;">$oFile</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Item</span> <span style="color: #800080;">$Path</span>
&nbsp;
	    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$oFile</span>.PSIsContainer<span style="color: #000000;">&#41;</span>
	    <span style="color: #000000;">&#123;</span>
    		<span style="color: #0000FF;">return</span>
	    <span style="color: #000000;">&#125;</span>
&nbsp;
	    try
	    <span style="color: #000000;">&#123;</span>
		    <span style="color: #800080;">$oStream</span> <span style="color: pink;">=</span> <span style="color: #800080;">$oFile</span>.Open<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>System.IO.FileMode<span style="color: #000000;">&#93;</span>::Open<span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>System.IO.FileAccess<span style="color: #000000;">&#93;</span>::ReadWrite<span style="color: pink;">,</span> <span style="color: #000000;">&#91;</span>System.IO.FileShare<span style="color: #000000;">&#93;</span>::None<span style="color: #000000;">&#41;</span>
		    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$oStream</span><span style="color: #000000;">&#41;</span>
		    <span style="color: #000000;">&#123;</span>
		        <span style="color: #800080;">$oStream</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		    <span style="color: #000000;">&#125;</span>
		    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$PassThru</span>.IsPresent <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: #800080;">$false</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;">Get-Item</span> <span style="color: #800080;">$Path</span>
		    <span style="color: #000000;">&#125;</span>
	    <span style="color: #000000;">&#125;</span>
	    catch
	    <span style="color: #000000;">&#123;</span>
	    	<span style="color: #008000;"># file is locked by a process.</span>
		    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$PassThru</span>.IsPresent <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: #800080;">$true</span>
		    <span style="color: #000000;">&#125;</span>
	    <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	end <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The function above can be handy for any instance in which you need to know whether a file is being held open by another process. It just so happens that one of those instances is when waiting for Explorer to finish dumping files into a zip archive! Armed with the example above, you can easily wait for the zip file to become available:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$zipPackage</span>.CopyHere<span style="color: #000000;">&#40;</span><span style="color: #800080;">$source</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Seconds</span> <span style="color: #804000;">2</span>
<span style="color: #0000FF;">while</span> <span style="color: #000000;">&#40;</span>Test<span style="color: pink;">-</span>FileLock <span style="color: #800080;">$fileName</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Seconds</span> <span style="color: #804000;">2</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note that while the default for the <strong>Test-FileLock</strong> function is to return $true or $false, I added a -PassThru parameter that changes the output to either a FileInfo object or $null. This alternate behavior allows you pass unlocked files down the pipeline to be used by other cmdlets.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.write-host.com/2011/09/common-solution-to-zipping-files-in-posh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Helper function: Set-TimeZone</title>
		<link>http://www.write-host.com/2011/09/helper-function-set-timezone/</link>
		<comments>http://www.write-host.com/2011/09/helper-function-set-timezone/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 14:38:59 +0000</pubDate>
		<dc:creator>Ben Baird</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.write-host.com/?p=27</guid>
		<description><![CDATA[Getting time zone information in PowerShell is an exercise in simplicity. This single command: &#91;TimeZoneInfo&#93;::Local &#8230; yields about as much information as you&#8217;d generally care to know about the current system time zone. Sample results: Id : Mountain Standard Time &#8230;<p class="read-more"><a href="http://www.write-host.com/2011/09/helper-function-set-timezone/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Getting time zone information in PowerShell is an exercise in simplicity. This single command:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>TimeZoneInfo<span style="color: #000000;">&#93;</span>::Local</pre></div></div>

<p>&#8230; yields about as much information as you&#8217;d generally care to know about the current system time zone.</p>
<p>Sample results:</p>
<pre>Id                         : Mountain Standard Time
DisplayName                : (UTC-07:00) Mountain Time (US &#038; Canada)
StandardName               : Mountain Standard Time
DaylightName               : Mountain Daylight Time
BaseUtcOffset              : -07:00:00
SupportsDaylightSavingTime : True</pre>
<p><em>Setting</em> the current time zone, unfortunately, isn&#8217;t quite as easy or intuitive. This is something I needed to do a while back for my desktop imaging process. Although the runtime portion of the Microsoft Deployment Tools (MDT) are largely built on a VBScript foundation, most of my deployment scripts are PowerShell-based, so I came up with a simple Posh function for setting the system time zone that works for both Windows XP and Windows 7.</p>
<p>There are a number of crazy ways to set time zone information, but rather than delving into methods that directly modify the registry and such, I decided upon documented, supported ways to change the current time zone:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Set<span style="color: pink;">-</span>TimeZone <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span>
		<span style="color: #000000;">&#91;</span>parameter<span style="color: #000000;">&#40;</span>Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
		<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$TimeZone</span>
	<span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$osVersion</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Item</span> <span style="color: #800000;">&quot;HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion&quot;</span><span style="color: #000000;">&#41;</span>.GetValue<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;CurrentVersion&quot;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$proc</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Diagnostics.Process
	<span style="color: #800080;">$proc</span>.StartInfo.WindowStyle <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Hidden&quot;</span>
&nbsp;
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$osVersion</span> <span style="color: #FF0000;">-ge</span> <span style="color: #804000;">6.0</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># OS is newer than XP</span>
		<span style="color: #800080;">$proc</span>.StartInfo.FileName <span style="color: pink;">=</span> <span style="color: #800000;">&quot;tzutil.exe&quot;</span>
		<span style="color: #800080;">$proc</span>.StartInfo.Arguments <span style="color: pink;">=</span> <span style="color: #800000;">&quot;/s <span style="color: #008080; font-weight: bold;">`&quot;</span>$TimeZone<span style="color: #008080; font-weight: bold;">`&quot;</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;"># XP or earlier</span>
		<span style="color: #800080;">$proc</span>.StartInfo.FileName <span style="color: pink;">=</span> <span style="color: #800080;">$env</span>:comspec
		<span style="color: #800080;">$proc</span>.StartInfo.Arguments <span style="color: pink;">=</span> <span style="color: #800000;">&quot;/c start /min control.exe TIMEDATE.CPL,,/z $TimeZone&quot;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #800080;">$proc</span>.Start<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You can call this function using the friendly time zone name associated with the zone you want. For example,</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;">Set<span style="color: pink;">-</span>TimeZone <span style="color: #800000;">&quot;Pacific Standard Time&quot;</span>
Set<span style="color: pink;">-</span>TimeZone <span style="color: #800000;">&quot;North Asia East Standard Time&quot;</span>
Set<span style="color: pink;">-</span>TimeZone <span style="color: #800000;">&quot;GMT Standard Time&quot;</span></pre></div></div>

<p>&#8230; and so on.</p>
<p>Note that the function uses the .NET Process class to execute the necessary commands. The Windows 7 tzutil method doesn&#8217;t need this, but the control.exe command used for XP and earlier won&#8217;t work with the Start-Process cmdlet, so I went with the lowest common denominator.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.write-host.com/2011/09/helper-function-set-timezone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>List GPOs that apply to a specific Active Directory group</title>
		<link>http://www.write-host.com/2011/08/list-gpos-that-apply-to-a-specific-active-directory-group/</link>
		<comments>http://www.write-host.com/2011/08/list-gpos-that-apply-to-a-specific-active-directory-group/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 15:42:24 +0000</pubDate>
		<dc:creator>Ben Baird</dc:creator>
				<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[Group Policy]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.write-host.com/?p=23</guid>
		<description><![CDATA[I manage an Active Directory environment that&#8217;s over ten years old and has undergone a number of upgrades and transitions. Yesterday I found myself trying to clean up some groups that didn&#8217;t appear to be used for anything anymore, but &#8230;<p class="read-more"><a href="http://www.write-host.com/2011/08/list-gpos-that-apply-to-a-specific-active-directory-group/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I manage an Active Directory environment that&#8217;s over ten years old and has undergone a number of upgrades and transitions. Yesterday I found myself trying to clean up some groups that didn&#8217;t appear to be used for anything anymore, but I wanted to make sure I wasn&#8217;t missing anything. One task I needed to accomplish for this was to make sure there were no active GPOs applying to those AD groups.</p>
<p>The cool thing is, PowerShell makes that task pretty darn easy! I whipped up the function below (which relies on the Microsoft GroupPolicy cmdlets) and was able to find exactly the information I was looking for.</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>GroupPoliciesByGroup <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span>
		<span style="color: #000000;">&#91;</span>Parameter<span style="color: #000000;">&#40;</span>Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
		<span style="color: #000000;">&#91;</span>Alias<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</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: pink;">,</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: #000000;">&#41;</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;">Get-Command</span> Get<span style="color: pink;">-</span>GPO <span style="color: #008080; font-style: italic;">-ErrorAction</span> SilentlyContinue<span style="color: #000000;">&#41;</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>
		Import<span style="color: pink;">-</span>Module GroupPolicy
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Domain</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$null</span> <span style="color: #FF0000;">-or</span> <span style="color: #800080;">$Domain</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">''</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$Domain</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>System.DirectoryServices.ActiveDirectory.Domain<span style="color: #000000;">&#93;</span>::GetCurrentDomain<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.Name<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #800080;">$gpos</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>GPO <span style="color: pink;">-</span>All <span style="color: pink;">-</span>Domain <span style="color: #800080;">$Domain</span>
&nbsp;
	<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$gpo</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$gpos</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$secinfo</span> <span style="color: pink;">=</span> <span style="color: #800080;">$gpo</span>.GetSecurityInfo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span> <span style="color: #000080;">$_</span>.Permission <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;GpoApply&quot;</span> <span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$sec</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$secinfo</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;">$sec</span>.Trustee.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$GroupName</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008080; font-weight: bold;">Out-Default</span> <span style="color: #008080; font-style: italic;">-InputObject</span> <span style="color: #800080;">$gpo</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you can see, it&#8217;s possible to pass this function a specific domain if desired. There are plenty of other ways to extend the function&#8217;s behavior as well, but the code above was enough to get me what I needed to know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.write-host.com/2011/08/list-gpos-that-apply-to-a-specific-active-directory-group/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>List local group members on a remote computer using WMI and PowerShell</title>
		<link>http://www.write-host.com/2011/08/list-local-group-members-on-a-remote-computer-using-wmi-and-powershell/</link>
		<comments>http://www.write-host.com/2011/08/list-local-group-members-on-a-remote-computer-using-wmi-and-powershell/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 18:22:39 +0000</pubDate>
		<dc:creator>Ben Baird</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[WMI]]></category>

		<guid isPermaLink="false">http://www.write-host.com/?p=17</guid>
		<description><![CDATA[Today I needed to sort through the computers on a domain and check the machines&#8217; local Administrators group for non-standard entries. I found quite a few examples of listing local group members using ADSI, but I tend to avoid ADSI &#8230;<p class="read-more"><a href="http://www.write-host.com/2011/08/list-local-group-members-on-a-remote-computer-using-wmi-and-powershell/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Today I needed to sort through the computers on a domain and check the machines&#8217; local Administrators group for non-standard entries. I found quite a few examples of listing local group members using ADSI, but I tend to avoid ADSI when possible, as its use has been falling out of favor for some time.</p>
<p>That meant using WMI &#8212; but the only WMI-based examples I found were written in VBScript. So I made a quick and dirty function that would do what I needed:</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>LocalGroupMembers 
<span style="color: #000000;">&#123;</span> 
    <span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span> 
        <span style="color: #000000;">&#91;</span>parameter<span style="color: #000000;">&#40;</span>Mandatory<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: pink;">,</span>ValueFromPipeline<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: pink;">,</span>ValueFromPipelineByPropertyName<span style="color: pink;">=</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
        <span style="color: #000000;">&#91;</span>Alias<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> 
        <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$ComputerName</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: pink;">=</span> <span style="color: #800000;">&quot;Administrators&quot;</span> 
    <span style="color: #000000;">&#41;</span> 
&nbsp;
    begin <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span> 
&nbsp;
    process 
    <span style="color: #000000;">&#123;</span> 
        <span style="color: #008000;"># If the account name of the computer object was passed in, it will </span>
        <span style="color: #008000;"># end with a $. Get rid of it so it doesn't screw up the WMI query. </span>
        <span style="color: #800080;">$ComputerName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ComputerName</span>.Replace<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`$</span>&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">''</span><span style="color: #000000;">&#41;</span> 
&nbsp;
        <span style="color: #008000;"># Initialize an array to hold the results of our query. </span>
        <span style="color: #800080;">$arr</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
&nbsp;
        <span style="color: #800080;">$wmi</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$ComputerName</span> <span style="color: #008080; font-style: italic;">-Query</span> ` 
            <span style="color: #800000;">&quot;SELECT * FROM Win32_GroupUser WHERE GroupComponent=<span style="color: #008080; font-weight: bold;">`&quot;</span>Win32_Group.Domain='$ComputerName',Name='$GroupName'<span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span> 
&nbsp;
        <span style="color: #008000;"># Parse out the username from each result and append it to the array. </span>
        <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$wmi</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;">$item</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$wmi</span><span style="color: #000000;">&#41;</span> 
            <span style="color: #000000;">&#123;</span> 
                <span style="color: #800080;">$arr</span> <span style="color: pink;">+=</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$item</span>.PartComponent.Substring<span style="color: #000000;">&#40;</span><span style="color: #800080;">$item</span>.PartComponent.IndexOf<span style="color: #000000;">&#40;</span><span style="color: #800000;">','</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">+</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>.Replace<span style="color: #000000;">&#40;</span><span style="color: #800000;">'Name='</span><span style="color: pink;">,</span> <span style="color: #800000;">''</span><span style="color: #000000;">&#41;</span>.Replace<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">''</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> 
            <span style="color: #000000;">&#125;</span> 
        <span style="color: #000000;">&#125;</span> 
&nbsp;
        <span style="color: #800080;">$hash</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>ComputerName<span style="color: pink;">=</span><span style="color: #800080;">$ComputerName</span>;Members<span style="color: pink;">=</span><span style="color: #800080;">$arr</span><span style="color: #000000;">&#125;</span> 
        <span style="color: #0000FF;">return</span> <span style="color: #800080;">$hash</span> 
    <span style="color: #000000;">&#125;</span> 
&nbsp;
    end<span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span> 
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This function turns out to be a cinch to use with my favorite Active Directory tools from Quest:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$AdminList</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>QADComputer <span style="color: pink;">-</span>SizeLimit <span style="color: #804000;">0</span> <span style="color: pink;">|</span> Get<span style="color: pink;">-</span>LocalGroupMembers <span style="color: pink;">-</span>GroupName <span style="color: #800000;">&quot;Administrators&quot;</span></pre></div></div>

<p>Output resembles something like this in the console, but you can do any number of things with the data to format it and send it to a CSV or text file:</p>
<pre>ComputerName                   PRINT-TST1
Members                        {NTAdmin, ServerLocalAdmin_DL_IA_F, DesktopServerLocalAdmin_DL_IA_F}
ComputerName                   CTX105
Members                        {NTAdmin, ServerLocalAdmin_DL_IA_F, DesktopServerLocalAdmin_DL_IA_F}
ComputerName                   CTX106
Members                        {NTAdmin, ServerLocalAdmin_DL_IA_F, DesktopServerLocalAdmin_DL_IA_F}
ComputerName                   CTX107
Members                        {NTAdmin, ServerLocalAdmin_DL_IA_F, DesktopServerLocalAdmin_DL_IA_F}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.write-host.com/2011/08/list-local-group-members-on-a-remote-computer-using-wmi-and-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Are two dates equal?</title>
		<link>http://www.write-host.com/2011/08/are-two-dates-equal/</link>
		<comments>http://www.write-host.com/2011/08/are-two-dates-equal/#comments</comments>
		<pubDate>Wed, 10 Aug 2011 14:51:03 +0000</pubDate>
		<dc:creator>Ben Baird</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.write-host.com/?p=12</guid>
		<description><![CDATA[Let&#8217;s say you&#8217;ve got a scheduled PowerShell task running to pull a list of files created on a certain date. You might try accomplishing such a feat with this code: $datePreserveWeek = &#40;Get-Date&#41;.AddDays&#40;-7&#41; &#160; Get-ChildItem -Path &#34;C:\Backups&#34; -Filter *.bak &#124; &#8230;<p class="read-more"><a href="http://www.write-host.com/2011/08/are-two-dates-equal/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you&#8217;ve got a scheduled PowerShell task running to pull a list of files created on a certain date. You might try accomplishing such a feat with this code:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$datePreserveWeek</span> <span style="color: pink;">=</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;">7</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;C:\Backups&quot;</span> <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: pink;">*</span>.bak <span style="color: pink;">|</span>
        <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span> <span style="color: #000080;">$_</span>.CreationTime <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$datePreserveWeek</span> <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Unfortunately, even if you have files that were created exactly 7 days ago, you will most likely get nothing back from this call. That&#8217;s because CreationTime and $datePreserveWeek are DateTime objects, which store both date (year, month, day) <em>and</em> time (hour, minute, second) data. When you compare them with the -eq operator, unless the objects also happen to match exactly the specific time, you&#8217;ll get a $false result.</p>
<p>In the past, I&#8217;ve solved this problem with the following solution:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;C:\Backups&quot;</span> <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: pink;">*</span>.bak <span style="color: pink;">|</span>
    <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span> <span style="color: #000080;">$_</span>.LastWriteTime.Year <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$datePreserveWeek</span>.Year <span style="color: #FF0000;">-and</span>
        <span style="color: #000080;">$_</span>.LastWriteTime.Month <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$datePreserveWeek</span>.Month <span style="color: #FF0000;">-and</span>
        <span style="color: #000080;">$_</span>.LastWriteTime.Day <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$datePreserveWeek</span>.Day <span style="color: #000000;">&#125;</span></pre></div></div>

<p>However, this method, while successful, apparently exposed my lack of knowledge about .NET&#8217;s DateTime class. MVP Shay Levi <a href="http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/f9ddafdf-a792-4876-bcfb-d0d98dab82af" target="_blank">pointed out</a> that the same thing can be accomplished with a bit less code!</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;C:\Backups&quot;</span> <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: pink;">*</span>.bak <span style="color: pink;">|</span>
        <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span> <span style="color: #000080;">$_</span>.CreationTime.Date <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$datePreserveWeek</span>.Date <span style="color: #000000;">&#125;</span></pre></div></div>

<p>The more you know&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.write-host.com/2011/08/are-two-dates-equal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Safely check for a variable&#8217;s existence in PowerShell</title>
		<link>http://www.write-host.com/2011/08/safely-check-for-a-variables-existence-in-powershell/</link>
		<comments>http://www.write-host.com/2011/08/safely-check-for-a-variables-existence-in-powershell/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 19:33:19 +0000</pubDate>
		<dc:creator>Ben Baird</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.write-host.com/?p=7</guid>
		<description><![CDATA[If you&#8217;re needing to access a PowerShell variable, but aren&#8217;t guaranteed that the variable has already been declared, you might be looking for a surefire way of finding out. Luckily, the PSProvider that manages the variable store can easily tell &#8230;<p class="read-more"><a href="http://www.write-host.com/2011/08/safely-check-for-a-variables-existence-in-powershell/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re needing to access a PowerShell variable, but aren&#8217;t guaranteed that the variable has already been declared, you might be looking for a surefire way of finding out. Luckily, the PSProvider that manages the variable store can easily tell you. Simply use the Test-Path cmdlet like so:</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: #008080; font-weight: bold;">Test-Path</span> <span style="color: #800000;">&quot;variable:\VarName&quot;</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;The variable VarName exists. Its value is $VarName&quot;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note that this can come in handy whether or not you use Strict Mode; Test-Path will return $false for a variable that hasn&#8217;t been assigned a value at all, but will report $true if the variable has been explicitly assigned a value of $null. If you were trying to dump the return value of a cmdlet into a variable, for instance, this distinction can sometimes tell you whether the cmdlet ended its execution abnormally.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.write-host.com/2011/08/safely-check-for-a-variables-existence-in-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

