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

<channel>
	<title>The PoSH Student</title>
	<atom:link href="http://poshstudent.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://poshstudent.wordpress.com</link>
	<description>Confessions of a Powershell Beginner</description>
	<lastBuildDate>Wed, 27 Apr 2011 21:05:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='poshstudent.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The PoSH Student</title>
		<link>http://poshstudent.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://poshstudent.wordpress.com/osd.xml" title="The PoSH Student" />
	<atom:link rel='hub' href='http://poshstudent.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Finding un-linked GPOs</title>
		<link>http://poshstudent.wordpress.com/2011/04/25/finding-un-linked-gpos-2/</link>
		<comments>http://poshstudent.wordpress.com/2011/04/25/finding-un-linked-gpos-2/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 17:08:27 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[GPO]]></category>
		<category><![CDATA[Group Policy]]></category>

		<guid isPermaLink="false">http://poshstudent.wordpress.com/?p=122</guid>
		<description><![CDATA[At my new job, I needed to do some GPO cleanup. There are a LOT of them, and many are not even linked to anything. I need to remove them, but didn&#8217;t want to go through them one-by-one to do it. PowerShell to the rescue! For the impatient, here is the quick script to show [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=122&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>At my new job, I needed to do some GPO cleanup. There are a LOT of them, and many are not even linked to anything. I need to remove them, but didn&#8217;t want to go through them one-by-one to do it. PowerShell to the rescue!</p>
<p>For the impatient, here is the quick script to show the un-linked GPOs. The explanation of how to get there is below.</p>
<pre class="brush: powershell;">
Import-Module grouppolicy
$allGPOs = Get-GPO -All | sort DisplayName
ForEach ($gpo in $allGPOs) {
	$xml = [xml](Get-GPOReport $gpo.Id xml)
	If (!$xml.GPO.LinksTo) {
		$gpo.DisplayName
		}
	}
</pre>
<p>First off, load the Group Policy module with <strong>Import-Module grouppolicy</strong>, and find what commands are available with <strong>Get-Command -Module grouppolicy</strong>:</p>
<pre style="color:#6D381F;font-size:10px;">CommandType     Name                                                Definition
-----------     ----                                                ----------
Cmdlet          Backup-GPO                                          Backup-GPO -Guid  -Path  [-Comment...
Cmdlet          Copy-GPO                                            Copy-GPO -SourceGuid  -TargetName ...
Cmdlet          Get-GPInheritance                                   Get-GPInheritance [-Target]  [-Domain &lt;S...
Cmdlet          Get-GPO                                             Get-GPO [-Guid]  [[-Domain] ] [[-S...
Cmdlet          Get-GPOReport                                       Get-GPOReport [-Guid]  [-ReportType] &lt;Repo...
Cmdlet          Get-GPPermissions                                   Get-GPPermissions -Guid  [-TargetName &lt;Str...
Cmdlet          Get-GPPrefRegistryValue                             Get-GPPrefRegistryValue -Guid  -Context &lt;G...
Cmdlet          Get-GPRegistryValue                                 Get-GPRegistryValue -Guid  -Key  [...
Cmdlet          Get-GPResultantSetOfPolicy                          Get-GPResultantSetOfPolicy [-Computer ] ...
Cmdlet          Get-GPStarterGPO                                    Get-GPStarterGPO -Guid  [-Domain ]...
Cmdlet          Import-GPO                                          Import-GPO -BackupId  -Path  [-Tar...
Cmdlet          New-GPLink                                          New-GPLink -Guid  -Target  [-LinkE...
Cmdlet          New-GPO                                             New-GPO [-Name]  [-Comment ] [-D...
Cmdlet          New-GPStarterGPO                                    New-GPStarterGPO [-Name]  [-Comment &lt;Str...
Cmdlet          Remove-GPLink                                       Remove-GPLink -Guid  -Target  [-Do...
Cmdlet          Remove-GPO                                          Remove-GPO -Guid  [-Domain ] [-Ser...
Cmdlet          Remove-GPPrefRegistryValue                          Remove-GPPrefRegistryValue [[-Server] ] ...
Cmdlet          Remove-GPRegistryValue                              Remove-GPRegistryValue [-Guid]  [-Key] &lt;St...
Cmdlet          Rename-GPO                                          Rename-GPO -Guid  -TargetName  [-D...
Cmdlet          Restore-GPO                                         Restore-GPO -BackupId  -Path  [-Do...
Cmdlet          Set-GPInheritance                                   Set-GPInheritance [-Target]  -IsBlocked ...
Cmdlet          Set-GPLink                                          Set-GPLink -Guid  -Target  [-LinkE...
Cmdlet          Set-GPPermissions                                   Set-GPPermissions -Guid  -PermissionLevel ...
Cmdlet          Set-GPPrefRegistryValue                             Set-GPPrefRegistryValue -Guid  -Context &lt;G...
Cmdlet          Set-GPRegistryValue                                 Set-GPRegistryValue -Guid  -Key  [...
</pre>
<p>So, I apparently can create a new Link, or set or remove one, but not simply get one. Nice. And <strong>Get-GPO</strong> doesn’t include links either:</p>
<pre style="color:#6D381F;font-size:10px;">DisplayName      : test
DomainName       : mydomain.com
Owner            : MYDOMAIN\Domain Admins
Id               : 1be29c7a-6cdb-48f8-aaef-18db7ab79b25
GpoStatus        : AllSettingsEnabled
Description      :
CreationTime     : 9/16/2004 11:10:04 AM
ModificationTime : 1/25/2011 5:39:44 PM
UserVersion      : AD Version: 6, SysVol Version: 6
ComputerVersion  : AD Version: 2, SysVol Version: 2
WmiFilter        :
</pre>
<p>So I need more detail. <strong>Get-GPOReport</strong> has lots of detail, but it's in HTML or XML format. Since PowerShell can read XML, I went that way:</p>
<pre style="color:#6D381F;font-size:10px;">PS&gt; $test = [xml](Get-GPOReport test xml)
PS&gt; $test

xml                                                         GPO
---                                                         ---
version="1.0" encoding="utf-16"                             GPO

PS&gt; $test.GPO

xsi                 : http://www.w3.org/2001/XMLSchema-instance
xsd                 : http://www.w3.org/2001/XMLSchema
xmlns               : http://www.microsoft.com/GroupPolicy/Settings
Identifier          : Identifier
Name                : test
IncludeComments     : true
CreatedTime         : 2004-09-16T15:10:04
ModifiedTime        : 2011-01-25T21:39:45
ReadTime            : 2011-04-25T14:43:30.3150573Z
SecurityDescriptor  : SecurityDescriptor FilterDataAvailable : true
Computer            : Computer
User                : User
LinksTo             : LinksTo

PS&gt; $test.GPO.LinksTo

SOMName                       SOMPath                 Enabled                       NoOverride
-------                       -------                 -------                       ----------
TestLab                       mydomain.com/SomeOU/... true                          false
</pre>
<p>So, all I need to do is find the GPOs where the XML doesn&#8217;t have a <strong>GPO.LinksTo</strong> property, or said property is blank.</p>
<pre class="brush: powershell;">
Import-Module grouppolicy
$allGPOs = Get-GPO -All | sort DisplayName
ForEach ($gpo in $allGPOs) {
	$xml = [xml](Get-GPOReport $gpo.Id xml)
	If (!$xml.GPO.LinksTo) {
		$gpo.DisplayName
		}
	}
</pre>
<p>Bam.</p>
<p><strong>Edit 4/25/2011 3:22 PM:</strong><br />
I realized I can also search for the GPOs which are linked, but have no settings at all:</p>
<pre class="brush: powershell;">
$allGPOs = Get-GPO -All | sort DisplayName
ForEach ($gpo in $allGPOs) {
    $xml = [xml](Get-GPOReport $gpo.Id xml)
    If ($xml.GPO.LinksTo) {
        If (!$xml.GPO.Computer.ExtensionData -and !$xml.GPO.User.ExtensionData) {
            $gpo.DisplayName
            }
        }
    }
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poshstudent.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poshstudent.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poshstudent.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poshstudent.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poshstudent.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poshstudent.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poshstudent.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poshstudent.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poshstudent.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poshstudent.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poshstudent.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poshstudent.wordpress.com/122/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poshstudent.wordpress.com/122/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poshstudent.wordpress.com/122/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=122&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poshstudent.wordpress.com/2011/04/25/finding-un-linked-gpos-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05bf0587857458ed131f3b8ad9f5dd6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sysiphus</media:title>
		</media:content>
	</item>
		<item>
		<title>How to hide a password in a script</title>
		<link>http://poshstudent.wordpress.com/2010/10/12/how-to-hide-a-password-in-a-script/</link>
		<comments>http://poshstudent.wordpress.com/2010/10/12/how-to-hide-a-password-in-a-script/#comments</comments>
		<pubDate>Tue, 12 Oct 2010 21:14:51 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[illegal characters]]></category>
		<category><![CDATA[obfuscation]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[scripts]]></category>

		<guid isPermaLink="false">http://poshstudent.wordpress.com/?p=112</guid>
		<description><![CDATA[Disclaimer: this is not secure. If you want secure, you need to encode the password as a secure string in a supporting file. This is for when you have to have the password in the script, but you want to hide it in plain sight. The trick: obfuscation. First off, go to http://home2.paulschou.net/tools/xlate/, put your [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=112&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: this is not secure. If you want secure, you need to encode the password as a secure string in a supporting file. This is for when you have to have the password in the script, but you want to hide it in plain sight.</p>
<p>The trick: obfuscation.</p>
<p>First off, go to <a title="TRANSLATOR, BINARY" href="http://home2.paulschou.net/tools/xlate/">http://home2.paulschou.net/tools/xlate/</a>, put your password in the TEXT field, and get the HEX values. If your password is &#8220;Password&#8221;, the HEX will be <strong>50 61 73 73 77 6f 72 64</strong></p>
<p>Next, generate a very long string of random characters to use. I use my <a href="http://poshstudent.wordpress.com/2010/10/12/generate-password/">Generate-Password function</a> to make a 48 character password of every printable character (with the Consolas font, anyway) in the standard ASCII set:</p>
<pre class="brush: powershell; toolbar: false;">
$charSet = [string]::Join(&amp;amp;quot;&amp;amp;quot;, (((33..126)+(161..255)) | %{[char]$_}))
Generate-Password -Length 48 -Chars $charSet
</pre>
<p>This leaves me with something like <strong>Ý×ôg&#8217;ÐÚ®ÉFS)ÉAÓ&lt;É¨c¦TÊiÝØ¨ü*T&amp;½Â{íÏÄüïN¾m!bhm0¦</strong></p>
<p>Next, we need to combine these together. In your random string, find a place where there are no <em>lower-case</em> hexadecimal characters for the length of your password +1. In this case, it&#8217;s the string <strong>®ÉFS)ÉA</strong>. It can be anywhere in your string; it just has to be clean. After the first character of the selected substring, enter your first Hex character pair (<strong>50</strong> in this case.) Skip a character, enter the next one, and so on. When you are done, you should have a combined string like <em>Ý×ôg&#8217;ÐÚ<strong>50</strong>®<strong>61</strong>É<strong>73</strong>F<strong>73</strong>S<strong>77</strong>)<strong>6f</strong>É<strong>72</strong>A<strong>64</strong>Ó&lt;É¨c¦TÊiÝØ¨ü*T&amp;½Â{íÏÄüïN¾m!bhm0¦</em></p>
<p>Note: I picked 48 characters because my 8 char password produces 16 hex chars, making a nice 64-digit block of seemingly-meaningless text. The whole point is to add as many layers of obfuscation as possible; even someone counting characters could be led nicely off-track for a while.</p>
<p>Now that you have this nice ridiculous string, assign it to a variable with a misdirecting name:</p>
<pre class="brush: powershell; toolbar: false;">
$Temp2 = &amp;amp;quot;Ý×ôg'ÐÚ50®61É73F73S77)6fÉ72A64Ó&amp;amp;lt;É¨c¦TÊiÝØ¨ü*T&amp;amp;amp;½Â{íÏÄüïN¾m!bhm0¦&amp;amp;quot;
</pre>
<p>It helps to also do the same for the username called <strong>$temp3</strong>, and maybe one or two that won&#8217;t be used.</p>
<p>Now that you have a nicely encoded, obfuscated password, you just need the script to be able to decode it. Run:</p>
<pre class="brush: powershell; toolbar: false;">
($Temp2 -creplace &amp;amp;quot;[^0-9a-f]&amp;amp;quot;,&amp;amp;quot;#&amp;amp;quot;).Split(&amp;amp;quot;#&amp;amp;quot;)
</pre>
<p>Note the use of <strong>-creplace</strong> instead of <strong>-replace</strong>; case-sensitive regex really matters here. You should get a whole bunch of blank lines, with perhaps a few single Hex characters, and somewhere in the middle, your Hex string doubles. What we did was find all the characters that did not match 0..9 or a..f and replace them with a character, then split the string into an array on that character. I picked the Hash because it has meaning in PoSH, but we are not using that meaning in this context, thus providing further obfuscation.</p>
<p>Now count your way to the first pair of your set (remember to start at 0) and the last pair. In my example case, it&#8217;s 8 and 15. Now, add a few lines to the end of your previous string:</p>
<pre class="brush: powershell; toolbar: false;">
($Temp2 -creplace &amp;amp;quot;[^0-9a-f]&amp;amp;quot;,&amp;amp;quot;#&amp;amp;quot;).Split(&amp;amp;quot;#&amp;amp;quot;)[8..15]
</pre>
<p>If you did it right, you just got your original Hex string, only as an array instead of a string separated by spaces. We&#8217;re just narrowing to only those members of the array.</p>
<p>We&#8217;re almost there. To get the password back, we need to convert each of these hex pairs into an ASCII character, and then change this array of ASCII characters into a single string.</p>
<pre class="brush: powershell; toolbar: false;">
[string]::join(&amp;amp;quot;&amp;amp;quot;, (($Temp2 -creplace &amp;amp;quot;[^0-9a-f]&amp;amp;quot;,&amp;amp;quot;#&amp;amp;quot;).Split(&amp;amp;quot;#&amp;amp;quot;)[8..15] | %{[convert]::ToInt32($_, 16)} | %{[char]$_}))
</pre>
<p>The <strong>[convert]</strong> portion of the pipeline converts the Hex (aka Base16) characters to Int32. The <strong>[char]</strong> portion converts the integers to their ASCII equivalents. And the whole pipeline is contained in parentheses within the <strong>[string]::Join</strong> command, which joins the array as a string. The output is the password we originally encoded.</p>
<p>Can this be decoded? Yes. Easily? No. Few have the knowledge, nor the patience to track backwards through this morass to figure out what the password is. When paired with an account with permissions only to the very narrowest possible resources needed to run the script, this will work as a &#8220;secure enough, good enough&#8221; sort of free solution.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poshstudent.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poshstudent.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poshstudent.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poshstudent.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poshstudent.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poshstudent.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poshstudent.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poshstudent.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poshstudent.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poshstudent.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poshstudent.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poshstudent.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poshstudent.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poshstudent.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=112&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poshstudent.wordpress.com/2010/10/12/how-to-hide-a-password-in-a-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05bf0587857458ed131f3b8ad9f5dd6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sysiphus</media:title>
		</media:content>
	</item>
		<item>
		<title>Generate-Password</title>
		<link>http://poshstudent.wordpress.com/2010/10/12/generate-password/</link>
		<comments>http://poshstudent.wordpress.com/2010/10/12/generate-password/#comments</comments>
		<pubDate>Tue, 12 Oct 2010 20:12:16 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[odd characters]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://poshstudent.wordpress.com/?p=109</guid>
		<description><![CDATA[I generate random passwords a lot. Anytime I need a service account, or when creating new user accounts, I need a random password to use. But I don&#8217;t want it to be too random, lest I confuse 1, l, and I because of some font variant somewhere. The natural result: a function! function Generate-Password { [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=109&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I generate random passwords a lot. Anytime I need a service account, or when creating new user accounts, I need a random password to use. But I don&#8217;t want it to be <em>too</em> random, lest I confuse 1, l, and I because of some font variant somewhere. The natural result: a function!</p>
<pre class="brush: powershell;">
function Generate-Password {
	Param (
		[int]$Length = 8,
		[string]$Chars = &quot;ABCDEFGHJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789&quot;,
		[switch]$Help
		)

	If ($Help) {
		Write-Host -f yellow @&quot;
PURPOSE:
  Generate random password of a specific type
USAGE:
  Generate-Password [[-Length] &lt;int&gt;] [-Chars &lt;string&gt;]
  Defaults to 8 characters long. Specify [-Length] characters if needed.
  Defaults to all upper &amp; lowercase letters and numbers, except confusable ones like
    0,O,1,l, etc. Specify your own [-Chars] as needed.
EXAMPLE:
  Generate-Password 25 &quot;§©ª«À¿¾æñ¶¤¨«®±µð÷&quot;
  Make a 25 character password of untypeable characters.
&quot;@
		break
		}

	$random = New-Object random
	$result = &quot;&quot;
	for ($i=0; $i -lt $length; $i++) {
		$result += $chars[$random.Next(0,$chars.Length)]
		}

$result
	}
</pre>
<p>It&#8217;s actually a very simple thing, but I use it almost daily, and sometimes for unexpected things. Need a large string to play with for testing? Generate a 1024 character password, or 128 eight-character ones. Want a huge password of untypeable characters? Here&#8217;s one for every character from <strong>[CHAR]0</strong> to <strong>[CHAR]255</strong> that prints using the Consolas font:</p>
<pre class="brush: powershell; toolbar: false;">
Generate-Password -Length 50 -Chars [string]::Join(&quot;&quot;, (((33..126)+(161..255)) | %{[char]$_}))
</pre>
<p>I keep this in a Functions.ps1 file that can be called by all my scripts, so it is available any time I need randomized characters.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poshstudent.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poshstudent.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poshstudent.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poshstudent.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poshstudent.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poshstudent.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poshstudent.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poshstudent.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poshstudent.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poshstudent.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poshstudent.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poshstudent.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poshstudent.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poshstudent.wordpress.com/109/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=109&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poshstudent.wordpress.com/2010/10/12/generate-password/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<georss:point>42.105874 -84.248282</georss:point>
		<geo:lat>42.105874</geo:lat>
		<geo:long>-84.248282</geo:long>
		<media:content url="http://0.gravatar.com/avatar/05bf0587857458ed131f3b8ad9f5dd6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sysiphus</media:title>
		</media:content>
	</item>
		<item>
		<title>Why I don&#8217;t try for one-liners</title>
		<link>http://poshstudent.wordpress.com/2010/08/23/why-i-dont-try-for-one-liners/</link>
		<comments>http://poshstudent.wordpress.com/2010/08/23/why-i-dont-try-for-one-liners/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 21:41:05 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ActiveSync]]></category>
		<category><![CDATA[ActiveSync Devices]]></category>
		<category><![CDATA[Exchange]]></category>
		<category><![CDATA[one-liners]]></category>
		<category><![CDATA[pipelining]]></category>
		<category><![CDATA[scripts]]></category>

		<guid isPermaLink="false">http://poshstudent.wordpress.com/?p=92</guid>
		<description><![CDATA[All I wanted to do was report on all the ActiveSync devices that have connected to our Exchange server in the last month: Get-CASMailbox -ResultSize Unlimited -Filter {HasActiveSyncDevicePartnership -eq $true -and -not DisplayName -like "CAS_{*"} &#124; Get-Mailbox &#124; %{Get-ActiveSyncDeviceStatistics -Mailbox $_} &#124; ?{$_.LastSuccessSync -ge (get-date).AddDays(-30)} &#124; Select @{Name="FirstSyncTime";e={$_.FirstSyncTime.AddHours(-4)}}, @{Name="LastPolicyUpdateTime";e={$_.LastPolicyUpdateTime.AddHours(-4)}}, @{Name="LastSyncAttemptTime";e={$_.LastSyncAttemptTime.AddHours(-4)}}, @{Name="LastSuccessSync";e={$_.LastSuccessSync.AddHours(-4)}}, DeviceType, DeviceID, DeviceUserAgent, DeviceWipeSentTime, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=92&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>All I wanted to do was report on all the ActiveSync devices that have connected to our Exchange server in the last month:</p>
<pre style="white-space:pre-wrap;"><span style="color:#5f9ea0;font-weight:bold;">Get-CASMailbox</span><span style="color:#000000;"> </span><span style="color:#5f9ea0;font-style:italic;">-ResultSize</span><span style="color:#000000;"> </span><span style="color:#800000;">Unlimited</span><span style="color:#000000;"> </span><span style="color:#5f9ea0;font-style:italic;">-Filter</span><span style="color:#000000;"> {HasActiveSyncDevicePartnership </span><span style="color:#ff0000;">-eq</span><span style="color:#000000;"> </span><span style="color:#800080;">$true</span><span style="color:#000000;"> </span><span style="color:#ff0000;">-and</span><span style="color:#000000;"> </span><span style="color:#ff0000;">-not</span><span style="color:#000000;"> DisplayName </span><span style="color:#ff0000;">-like</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">CAS_{*</span><span style="color:#800000;">"</span><span style="color:#000000;">} | </span><span style="color:#5f9ea0;font-weight:bold;">Get-Mailbox</span><span style="color:#000000;"> | </span><span style="color:#5f9ea0;font-weight:bold;">%</span><span style="color:#000000;">{</span><span style="color:#5f9ea0;font-weight:bold;">Get-ActiveSyncDeviceStatistics</span><span style="color:#000000;"> </span><span style="color:#5f9ea0;font-style:italic;">-Mailbox</span><span style="color:#000000;"> </span><span style="color:#800080;">$_</span><span style="color:#000000;">} | ?{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.LastSuccessSync </span><span style="color:#ff0000;">-ge</span><span style="color:#000000;"> (</span><span style="color:#5f9ea0;font-weight:bold;">get-date</span><span style="color:#000000;">).AddDays(</span><span style="color:#ff0000;">-</span><span style="color:#000000;">30</span><span style="color:#000000;">)} | </span><span style="color:#5f9ea0;font-weight:bold;">Select</span><span style="color:#000000;"> @{Name</span><span style="color:#ff0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">FirstSyncTime</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#ff0000;">=</span><span style="color:#000000;">{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.FirstSyncTime.AddHours(</span><span style="color:#ff0000;">-</span><span style="color:#000000;">4</span><span style="color:#000000;">)}}, @{Name</span><span style="color:#ff0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">LastPolicyUpdateTime</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#ff0000;">=</span><span style="color:#000000;">{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.LastPolicyUpdateTime.AddHours(</span><span style="color:#ff0000;">-</span><span style="color:#000000;">4</span><span style="color:#000000;">)}}, @{Name</span><span style="color:#ff0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">LastSyncAttemptTime</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#ff0000;">=</span><span style="color:#000000;">{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.LastSyncAttemptTime.AddHours(</span><span style="color:#ff0000;">-</span><span style="color:#000000;">4</span><span style="color:#000000;">)}}, @{Name</span><span style="color:#ff0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">LastSuccessSync</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#ff0000;">=</span><span style="color:#000000;">{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.LastSuccessSync.AddHours(</span><span style="color:#ff0000;">-</span><span style="color:#000000;">4</span><span style="color:#000000;">)}}, DeviceType, DeviceID, DeviceUserAgent, DeviceWipeSentTime, DeviceWipeRequestTime, DeviceWipeAckTime, LastPingHeartbeat, DeviceModel, DeviceIMEI, DeviceFriendlyName, DeviceOS, DeviceOSLanguage, DevicePhoneNumber, Identity, @{Name</span><span style="color:#ff0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">User</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#ff0000;">=</span><span style="color:#000000;">{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.Identity.SmtpAddress}}, @{Name</span><span style="color:#ff0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">OU</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#ff0000;">=</span><span style="color:#000000;">{(</span><span style="color:#5f9ea0;font-weight:bold;">Get-User</span><span style="color:#000000;"> </span><span style="color:#800080;">$_</span><span style="color:#000000;">.Identity.SmtpAddress).Identity.Parent.Name}} | </span><span style="color:#5f9ea0;font-weight:bold;">Sort</span><span style="color:#000000;"> DeviceID,LastSyncAttemptTime | </span><span style="color:#5f9ea0;font-weight:bold;">Export-Csv</span><span style="color:#000000;"> </span><span style="color:#800000;">'</span><span style="color:#800000;">C:\ActiveSyncDevices.csv</span><span style="color:#800000;">'</span><span style="color:#000000;"> </span><span style="color:#5f9ea0;font-style:italic;">-NoTypeInformation</span><span style="color:#000000;">
</span></pre>
<p>Yes, this really will work to get a report. But this is a perfect example of why I don&#8217;t try for one-liners. Let&#8217;s break it down by doing this the least one-liner-ish that is reasonable:</p>
<pre style="white-space:pre-wrap;"><span style="color:#800080;">$CasMailboxes</span><span style="color:#000000;"> </span><span style="color:#ff0000;">=</span><span style="color:#000000;"> </span><span style="color:#5f9ea0;font-weight:bold;">Get-CASMailbox</span><span style="color:#000000;"> </span><span style="color:#5f9ea0;font-style:italic;">-ResultSize</span><span style="color:#000000;"> </span><span style="color:#800000;">Unlimited</span><span style="color:#000000;"> </span><span style="color:#5f9ea0;font-style:italic;">-Filter</span><span style="color:#000000;"> {HasActiveSyncDevicePartnership </span><span style="color:#ff0000;">-eq</span><span style="color:#000000;"> </span><span style="color:#800080;">$true</span><span style="color:#000000;"> </span><span style="color:#ff0000;">-and</span><span style="color:#000000;"> </span><span style="color:#ff0000;">-not</span><span style="color:#000000;"> DisplayName </span><span style="color:#ff0000;">-like</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">CAS_{*</span><span style="color:#800000;">"</span><span style="color:#000000;">}</span></pre>
<p>Since <span style="color:#5f9ea0;font-weight:bold;">Get-Mailbox</span> does not return a <strong>HasActiveSyncDevicePartnership</strong> attribute, I had to use Get-CASMailbox. This retrieves all of them that have the attribute. Total run time for my 18,000 CAS mailboxes: 2:09.</p>
<pre style="white-space:pre-wrap;"><span style="color:#800080;">$Mailboxes</span><span style="color:#000000;"> </span><span style="color:#ff0000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">$CasMailboxes</span><span style="color:#000000;"> | </span><span style="color:#5f9ea0;font-weight:bold;">Get-Mailbox</span></pre>
<p>In the next step, I&#8217;ll be running <span style="color:#5f9ea0;font-weight:bold;">Get-ActiveSyncDeviceStatistics</span>, but that command requires a <strong><em>Mailbox</em></strong> ID Parameter, not a <strong><em>CasMailbox</em></strong> ID Parameter. So I have to get the mailboxes for each CasMailbox I obtained earlier. Total run time: 3:37.</p>
<pre style="white-space:pre-wrap;"><span style="color:#800080;">$ASDevices</span><span style="color:#000000;"> </span><span style="color:#ff0000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">$Mailboxes</span><span style="color:#000000;"> | </span><span style="color:#5f9ea0;font-weight:bold;">%</span><span style="color:#000000;">{</span><span style="color:#5f9ea0;font-weight:bold;">Get-ActiveSyncDeviceStatistics</span><span style="color:#000000;"> </span><span style="color:#5f9ea0;font-style:italic;">-Mailbox</span><span style="color:#000000;"> </span><span style="color:#800080;">$_</span><span style="color:#000000;">}</span></pre>
<p>Finally getting around to getting the devices. This gets all of the <strong>ActiveSyncDeviceStatistics</strong> for each mailbox. It gives me some handy stats for each one, and handily each object represents a device so I now have the devices themselves. An interesting note: even though the documentation states that <span style="color:#5f9ea0;font-weight:bold;">Get-ActiveSyncDeviceStatistics</span> will accept pipeline input on the <span style="color:#5f9ea0;font-style:italic;">-Mailbox</span> parameter, it doesn&#8217;t. Thus I have to do a <span style="color:#5f9ea0;font-weight:bold;">%</span> (aka <span style="color:#5f9ea0;font-weight:bold;">ForEach-Object</span>) loop to make it work. Total run time: 15:23.</p>
<pre style="white-space:pre-wrap;"><span style="color:#800080;">$RecentDevices</span><span style="color:#000000;"> </span><span style="color:#ff0000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">$ASDevices</span><span style="color:#000000;"> | ?{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.LastSuccessSync </span><span style="color:#ff0000;">-ge</span><span style="color:#000000;"> (</span><span style="color:#5f9ea0;font-weight:bold;">get-date</span><span style="color:#000000;">).AddDays(</span><span style="color:#ff0000;">-</span><span style="color:#000000;">30</span><span style="color:#000000;">)}</span></pre>
<p>I only want the ones that have connected in the last 30 days, so I use<br />
<span style="color:#5f9ea0;font-weight:bold;">?</span> (aka <span style="color:#5f9ea0;font-weight:bold;">Where-Object</span>) to filter it. Total run time: 0:04.</p>
<pre style="white-space:pre-wrap;"><span style="color:#800080;">$Devices</span><span style="color:#000000;"> </span><span style="color:#FF0000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">$RecentDevices</span><span style="color:#000000;"> | </span><span style="color:#5F9EA0;font-weight:bold;">select</span><span style="color:#000000;"> @{Name</span><span style="color:#FF0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">FirstSyncTime</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#FF0000;">=</span><span style="color:#000000;">{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.FirstSyncTime.AddHours(</span><span style="color:#FF0000;">-</span><span style="color:#000000;">4</span><span style="color:#000000;">)}}, @{Name</span><span style="color:#FF0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">LastPolicyUpdateTime</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#FF0000;">=</span><span style="color:#000000;">{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.LastPolicyUpdateTime.AddHours(</span><span style="color:#FF0000;">-</span><span style="color:#000000;">4</span><span style="color:#000000;">)}}, @{Name</span><span style="color:#FF0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">LastSyncAttemptTime</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#FF0000;">=</span><span style="color:#000000;">{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.LastSyncAttemptTime.AddHours(</span><span style="color:#FF0000;">-</span><span style="color:#000000;">4</span><span style="color:#000000;">)}}, @{Name</span><span style="color:#FF0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">LastSuccessSync</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#FF0000;">=</span><span style="color:#000000;">{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.LastSuccessSync.AddHours(</span><span style="color:#FF0000;">-</span><span style="color:#000000;">4</span><span style="color:#000000;">)}}, DeviceType, DeviceID, DeviceUserAgent, DeviceWipeSentTime, DeviceWipeRequestTime, DeviceWipeAckTime, LastPingHeartbeat, DeviceModel, DeviceIMEI, DeviceFriendlyName, DeviceOS, DeviceOSLanguage, DevicePhoneNumber, Identity, @{Name</span><span style="color:#FF0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">User</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#FF0000;">=</span><span style="color:#000000;">{</span><span style="color:#800080;">$_</span><span style="color:#000000;">.Identity.SmtpAddress}}, @{Name</span><span style="color:#FF0000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">OU</span><span style="color:#800000;">"</span><span style="color:#000000;">;e</span><span style="color:#FF0000;">=</span><span style="color:#000000;">{(</span><span style="color:#5F9EA0;font-weight:bold;">Get-User</span><span style="color:#000000;"> </span><span style="color:#800080;">$_</span><span style="color:#000000;">.Identity.SmtpAddress).Identity.Parent.Name}}</span></pre>
<p>Now we&#8217;re starting to get convoluted. Now that I had all the devices, I wanted to filter the properties I had for each. Seven of the properties are time-properties, but I needed them in local time, not GMT. So for each of them, I created a custom column with the same original name, but with &#8220;-4&#8243; hours added. I simply included the other columns/properties as-is, except I left out the <strong>RecoveryPassword</strong> password.</p>
<p>Then I added two more columns. The first is <strong>User</strong>, which is really just the first part of <strong>Identity</strong> before the backslash. I included it because I need to check for uniqueness; it can be helpful to know who is using both an iPad and an iPhone. I also included <strong>OU</strong>, so that I can get accurate counts of how many devices are used by faculty, staff, and students. (I keep them in separate OUs, even though many people/accounts actually fit multiple constituency groups.) Total run time: 0:06.</p>
<pre style="white-space:pre-wrap;"><span style="color:#800080;">$SortedDevices</span><span style="color:#000000;"> </span><span style="color:#FF0000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">$Devices</span><span style="color:#000000;"> | </span><span style="color:#5F9EA0;font-weight:bold;">Sort-Object</span><span style="color:#000000;"> DeviceID,LastSyncAttemptTime</span></pre>
<p>I want the data to start sorted by the <strong>DeviceID</strong>, but I also included <strong>LastSyncAttemptTime</strong> in case the same device has been used by multiple accounts. (Turns out there was an iPad tested by 5 people in 1 department in 3 days.) Total run time: 27 milliseconds.</p>
<pre style="white-space:pre-wrap;"><span style="color:#800080;">$SortedDevices</span><span style="color:#000000;"> | </span><span style="color:#5F9EA0;font-weight:bold;">Export-Csv</span><span style="color:#000000;"> </span><span style="color:#800000;">'</span><span style="color:#800000;">C:\ActiveSyncDevices.csv</span><span style="color:#800000;">'</span><span style="color:#000000;"> </span><span style="color:#5F9EA0;font-style:italic;">-NoTypeInformation</span></pre>
<p>Finally, I export it to a CSV. No big deal here; I expect to open it in Excel and start filtering from there. Total run time: 126 ms.</p>
<p>In the end, the total processing time is about 21:13.</p>
<p>This is a great of example of when it&#8217;s <strong>not</strong> a good idea to chain all your commands into one-liners. Should you want to change something about the commands later down the chain (for example, get only ones from the last 7 days, or don&#8217;t include DeviceWipe* properties) you don&#8217;t want to run the first several commands all over again, and twiddle your thumbs for 15 minutes. When I really ran this, I did part of it (filtering by recent, custom columns, sorting, export) as one pipeline, but it&#8217;s worth thinking ahead before onelining too many things.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poshstudent.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poshstudent.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poshstudent.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poshstudent.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poshstudent.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poshstudent.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poshstudent.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poshstudent.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poshstudent.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poshstudent.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poshstudent.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poshstudent.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poshstudent.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poshstudent.wordpress.com/92/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=92&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poshstudent.wordpress.com/2010/08/23/why-i-dont-try-for-one-liners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>42.105874 -84.248282</georss:point>
		<geo:lat>42.105874</geo:lat>
		<geo:long>-84.248282</geo:long>
		<media:content url="http://0.gravatar.com/avatar/05bf0587857458ed131f3b8ad9f5dd6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sysiphus</media:title>
		</media:content>
	</item>
		<item>
		<title>Auto-cleaning IIS and other logs</title>
		<link>http://poshstudent.wordpress.com/2010/08/04/auto-cleaning-iis-and-other-logs/</link>
		<comments>http://poshstudent.wordpress.com/2010/08/04/auto-cleaning-iis-and-other-logs/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 21:46:54 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[hints]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[logs]]></category>
		<category><![CDATA[NTFS compression]]></category>
		<category><![CDATA[scheduled tasks]]></category>
		<category><![CDATA[scripts]]></category>

		<guid isPermaLink="false">http://poshstudent.wordpress.com/?p=83</guid>
		<description><![CDATA[I wanted a way to automate the cleaning out of IIS log files, so I wrote a simple script to do it. But rather than keep it too simple, I modularized it so that it can be reused for any sort of log file in any location and delete files of any age. I also [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=83&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I wanted a way to automate the cleaning out of IIS log files, so I wrote a simple script to do it. But rather than keep it too simple, I modularized it so that it can be reused for any sort of log file in any location and delete files of any age. I also added a switch that allows me to apply NTFS compression to the files instead of delete them if I desire. (I normally don&#8217;t use that, but need it on occasion. I&#8217;ve also had 2 system crashes when scripting the compression before I added the pause, so I now use it very sparingly!)</p>
<pre style="color:#6D381F;font-size:10px;">Param (
	[string]$Path = "C:\WINDOWS\system32\LogFiles\W3SVC1",
	[int]$Months,
	[int]$Weeks,
	[int]$Days,
	[string]$Extension = "log",
	[switch]$Compress,
	[switch]$Help
	)

If ($Help) {
	Write-Host -f yellow @"
PURPOSE:
  Deletes or compresses old log files by date last written.
USAGE:
  Clean-Logs.ps1 [[-Path] ] [-Months|-Weeks|-Days ] [-Extension ] [-Compress]
  Deletes files older than specified time in specified path.
  Defaults to the default IIS log file path.
  Defaults to *.log files.
  Defaults to 3 months back.
  Can optionally compress the files instead, but this is a risky maneuver known to occasionally crash Windows.
EXAMPLE:
  Clean-Logs.ps1 -Path "C:\Documents and Settings\All Users\Application Data\avg8\scanlogs" -Weeks 6
"@
	break
	}

if ($Months) {
	$EndDate = (Get-Date).AddMonths(-$Months)
	}
elseif ($Weeks) {
	$EndDate = (Get-Date).AddDays(-7 * $Weeks)
	}
elseif ($Days) {
	$EndDate = (Get-Date).AddDays(-$Days)
	}
else {
	$EndDate = (Get-Date).AddMonths(-3)
	}

$files = Get-ChildItem -Path $Path -Filter "*.$Extension" | Where-Object { $_.LastWriteTime -le $EndDate }

if (! $Compress -and $files) {
	$files | ForEach-Object { $_.Delete() }
	}

if ($Compress -and $files) {
	foreach ($file in $files) {
		$wmiFile = Get-WmiObject cim_datafile -Filter "Name='$($file.FullName.Replace('\','\\'))'"
		If (! $wmiFile.Compressed) {
			$wmiFile.Compress() | Out-Null
			Start-Sleep 1 #Seems to need to pause or the system crashes. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />
			}
		}
	}
</pre>
<p>I add this to the scheduled tasks on a server, set the parameters to match my needs on that server, run it once a month, and forget it.</p>
<p>This script also includes something I&#8217;ve started adding to every script and function I write, the parameter <span style="color:#6d381f;"><strong>[switch]$Help</strong></span>. Perhaps you don&#8217;t have this problem, but I tend to forget the switches and whatnots of a script/function when I have not used it in months. But this way, rather than needing to look into the script to remember what the switches are and what they do and what the defaults are, I run <span style="color:#6d381f;"><strong>Verb-Noun -Help</strong></span> and get a quick reminder.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poshstudent.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poshstudent.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poshstudent.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poshstudent.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poshstudent.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poshstudent.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poshstudent.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poshstudent.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poshstudent.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poshstudent.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poshstudent.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poshstudent.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poshstudent.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poshstudent.wordpress.com/83/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=83&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poshstudent.wordpress.com/2010/08/04/auto-cleaning-iis-and-other-logs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05bf0587857458ed131f3b8ad9f5dd6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sysiphus</media:title>
		</media:content>
	</item>
		<item>
		<title>Formatting in PowerShell</title>
		<link>http://poshstudent.wordpress.com/2010/02/26/formatting-in-powershell/</link>
		<comments>http://poshstudent.wordpress.com/2010/02/26/formatting-in-powershell/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 22:33:49 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://poshstudent.wordpress.com/?p=73</guid>
		<description><![CDATA[I&#8217;ve used most all of these formats when building custom strings, but I always seem to be digging all over other people&#8217;s posts to get to them. Here I brought together all the ones I expect I&#8217;ll ever use. There are a few other ones out there as well. Standard numeric strings Format Name Use [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=73&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve used most all of these formats when building custom strings, but I always seem to be digging all over other people&#8217;s posts to get to them. Here I brought together all the ones I expect I&#8217;ll ever use.</p>
<p>There are a <a href="http://tfl09.blogspot.com/2007/11/formatting-with-powershell.html">few</a> <a href="http://blogs.technet.com/heyscriptingguy/archive/2010/02/09/hey-scripting-guy-february-9-2010.aspx">other</a> <a href="http://msdn.microsoft.com/en-us/library/fht0f5be.aspx#ctl00_WikiContent_ctl01_Header">ones</a> <a href="http://pshscripts.blogspot.com/2008/12/get-formatstring2ps1.html">out</a> <a href="http://bsonposh.com/archives/229">there</a> as well.</p>
<table border="1px" cellpadding="2px" cellspacing="0px">
<tr valign="top">
<th colspan="5"><a href="http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx">Standard</a> numeric strings</th>
</tr>
<tr valign="top">
<th>Format</th>
<th>Name</th>
<th>Use</th>
<th>Output</th>
<th>Notes</th>
</tr>
<tr valign="top">
<td align="center">C | c</td>
<td align="center">Currency</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:c}&quot; -f 12345.6789<br />
&quot;{0:c}&quot; -f -12345.6789</td>
<td align="right">$12,345.68<br />
($12,345.68)</td>
<td>&nbsp;</td>
</tr>
<tr valign="top">
<td align="center">D | d</td>
<td align="center">Decimal</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:d}&quot; -f 12345<br />
&quot;{0:d}&quot; -f -12345</td>
<td align="right">12345<br />
-12345</td>
<td>Must be an integer</td>
</tr>
</tr>
<tr valign="top">
<td align="center">G<em>n</em> | g<em>n</em></td>
<td align="center">General</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:g6}&quot; -f 12345.6789<br />
&quot;{0:g16}&quot; -f 12345.6789<br />
&quot;{0:g}&quot; -f -12345.6789</td>
<td align="right">12345.7<br />
12345.6789<br />
-12345.6789</td>
<td>Display up to <em>n</em> digits.</td>
</tr>
<tr valign="top">
<td align="center">N<em>n | n</em>n</td>
<td align="center">Number</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:n6}&quot; -f 12345.6789<br />
&quot;{0:n}&quot; -f 12345.6789<br />
&quot;{0:n3}&quot; -f -12345.6789</td>
<td align="right">12,345.678900<br />
12,345.68<br />
-12,345.679</td>
<td>To exactly <em>n</em> decimal places.<br />
Defaults to 2 places.<br />
Use <strong>F<em>n</em></strong> for no separator mark.</td>
</tr>
<tr valign="top">
<td align="center">P<em>n</em>| p<em>n</em></td>
<td align="center">Percent</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:p}&quot; -f 0.12345<br />
&quot;{0:p0}&quot; -f -0.12345</td>
<td align="right">-12.35 %<br />
-12 %</td>
<td>Defaults to 2 decimal places.</td>
</tr>
<tr valign="top">
<td align="center">X<em>n</em> | x<em>n</em></td>
<td align="center">Hexadecimal</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:x}&quot; -f 123<br />
&quot;{0:x1}&quot; -f -2<br />
&quot;{0:x4}&quot; -f 123</td>
<td align="right">7B<br />
fffffffe<br />
007B</td>
<td>Uses minimum needed digits unless specified.<br />
Negatives work backwards from ffffffff.</td>
</tr>
<tr valign="top">
<th colspan="5"><a href="http://msdn.microsoft.com/en-us/library/0c899ak8.aspx"><br />
Custom</a> numeric strings</th>
</tr>
<tr valign="top">
<td align="center">0</td>
<td align="center">Zero<br />
placeholder</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:00.0}&quot; -f 5.6789</td>
<td align="right">05.7</td>
<td>Zero or the appropriate digit as needed.</td>
</tr>
<tr valign="top">
<td align="center">.</td>
<td align="center">Decimal<br />
point</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:00.0}&quot; -f 5.6789</td>
<td align="right">05.7</td>
<td></td>
</tr>
<tr valign="top">
<td align="center">#</td>
<td align="center">Digit<br />
placeholder</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:###.##}&quot; -f 5.6789<br />
&quot;{0:(###) ###-####}&quot; -f 5175551212</td>
<td align="right">5.68<br />
(517) 555-1212</td>
<td>Appropriate digit if available, or blank.</td>
</tr>
<tr valign="top">
<td align="center">%</td>
<td align="center">Percent<br />
placeholder</td>
<td style="font-family:monospace;color:#A0522D;">
&quot;{0:.##%}&quot; -f 1.23456<br />
&quot;{0:##%}&quot; -f 1.23456</td>
<td align="right">123.46%<br />
123%</td>
<td></td>
</tr>
<tr valign="top">
<th colspan="5"><a href="http://msdn.microsoft.com/en-us/library/az4se3k1.aspx"><br />
Standard</a> Date &amp; Time strings</th>
</tr>
<tr valign="top">
<td colspan="5" align="center">$date = Get-Date</td>
</tr>
<tr valign="top">
<td align="center">D | d</td>
<td align="center">Date<br />
long &amp; short</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:D}&quot; -f $date<br />
&quot;{0:d}&quot; -f $date</td>
<td align="right">Saturday, February 06, 2010<br />
2/6/2010</td>
<td></td>
</tr>
<tr valign="top">
<td align="center">T | t</td>
<td align="center">Time<br />
long &amp; short</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:T}&quot; -f $date<br />
&quot;{0:t}&quot; -f $date</td>
<td align="right">9:08:01 AM<br />
9:08 AM
		</td>
<td></td>
</tr>
<tr valign="top">
<td align="center">F | f</td>
<td align="center">Full<br />
long &amp; short</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:F}&quot; -f $date<br />
&quot;{0:f}&quot; -f $date</td>
<td align="right">Saturday, February 06, 2010 9:08:01 AM<br />
Saturday, February 06, 2010 9:08 AM</td>
<td></td>
</tr>
<tr valign="top">
<td align="center">G | g</td>
<td align="center">General<br />
long &amp; short</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:G}&quot; -f $date<br />
&quot;{0:g}&quot; -f $date</td>
<td align="right">2/6/2010 9:08:01 AM<br />
2/6/2010 9:08 AM</td>
<td></td>
</tr>
<tr valign="top">
<td align="center">R | r</td>
<td align="center">RFC1123</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:r}&quot; -f $date</td>
<td align="right">Sat, 06 Feb 2010 09:08:01 GMT</td>
<td>Always in GMT format.</td>
</tr>
<tr valign="top">
<td align="center">s</td>
<td align="center">Sortable</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:s}&quot; -f $date</td>
<td align="right">2010-02-06T09:08:01</td>
<td>Lower case &quot;s&quot; only.</td>
</tr>
<tr valign="top">
<td align="center">M | m</td>
<td align="center">Month/Day</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:m}&quot; -f $date</td>
<td align="right">February 06</td>
<td></td>
</tr>
<tr valign="top">
<td align="center">Y | y</td>
<td align="center">Year/Month</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:y}&quot; -f $date</td>
<td align="right">February, 2010</td>
<td></td>
</tr>
<tr valign="top">
<th colspan="5"><a href="http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx"><br />
Custom</a> Date &amp; Time strings</th>
</tr>
<tr valign="top">
<td colspan="5" align="center">$date = Get-Date</td>
</tr>
<tr valign="top">
<td align="center">y | yy | yyyy</td>
<td align="center">Year</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:y}&quot; -f $date<br />
&quot;{0:yy}&quot; -f $date<br />
&quot;{0:yyy}&quot; -f $date</td>
<td align="right">9<br />
09<br />
2009</td>
<td>&quot;y&quot; does not work standalone.</td>
</tr>
<tr valign="top">
<td align="center">M | MM</td>
<td align="center">Month</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:M}&quot; -f $date<br />
&quot;{0:MM}&quot; -f $date</td>
<td align="right">2<br />
02</td>
<td>&quot;M&quot; does not work standalone.</td>
</tr>
<tr valign="top">
<td align="center">MMM | MMMM</td>
<td align="center">Month name</td>
<td style="font-family:monospace;color:#A0522D;"></td>
<td align="right"></td>
<td></td>
</tr>
<tr valign="top">
<td align="center">d | dd</td>
<td align="center">Day</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:d}&quot; -f $date<br />
&quot;{0:dd}&quot; -f $date</td>
<td align="right">6<br />
06</td>
<td>&quot;d&quot; does not work standalone.</td>
</tr>
<tr valign="top">
<td align="center">ddd | dddd</td>
<td align="center">Day name</td>
<td style="font-family:monospace;color:#A0522D;">
&quot;{0:ddd}&quot; -f $date<br />
&quot;{0:dddd}&quot; -f $date</td>
<td align="right">Sat<br />
Saturday</td>
<td></td>
</tr>
<tr valign="top">
<td align="center">h | hh | H | HH</td>
<td align="center">Hour</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:h}&quot; -f $date<br />
&quot;{0:h}&quot; -f $date<br />
&quot;{0:H}&quot; -f $date<br />
&quot;{0:HH}&quot; -f $date</td>
<td align="right">1<br />
01<br />
13<br />
13</td>
<td>Only &quot;HH&quot; works standalone.</td>
</tr>
<tr valign="top">
<td align="center">m | mm</td>
<td align="center">Minute</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:m}&quot; -f $date<br />
&quot;{0:mm}&quot; -f $date</td>
<td align="right">8<br />
08</td>
<td>&quot;m&quot; does not work standalone.</td>
</tr>
<tr valign="top">
<td align="center">s | ss</td>
<td align="center">Second</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:s}&quot; -f $date<br />
&quot;{0:ss}&quot; -f $date</td>
<td align="right">1<br />
01</td>
<td>&quot;s&quot; does not work standalone.</td>
</tr>
<tr valign="top">
<td align="center">t | tt</td>
<td align="center">AM/PM</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:t}&quot; -f $date<br />
&quot;{0:tt}&quot; -f $date</td>
<td align="right">P<br />
PM</td>
<td>&quot;t&quot; does not work standalone.</td>
</tr>
<tr valign="top">
<td align="center">: | /</td>
<td align="center">Time &amp; Date<br />
separators</td>
<td style="font-family:monospace;color:#A0522D;">&quot;{0:ddd yy/M/d}&quot; -f $date<br />
&quot;{0:dddd yyyy/MM/dd}&quot; -f $date<br />
&quot;{0:h:mm:ss tt}&quot; -f $date<br />
&quot;{0:HH:m:s}&quot; -f $date<br />
&quot;{0:MMM d, yyyy}&quot; -f $date</td>
<td align="right">Sat 10/2/6<br />
Saturday 2010/02/06<br />
1:08:01 PM<br />
13:8:1<br />
Feb 6, 2010</td>
<td></td>
</tr>
<tr valign="top">
<th colspan="5">Other string tricks</th>
</tr>
<tr valign="top">
<td align="center">&nbsp;</td>
<td align="center">Alignment &amp;<br />
forced lenght</td>
<td style="font-family:monospace;color:#A0522D;">
&quot;&gt;{0,10}&lt;&quot; -f &quot;test&quot;<br />
&quot;&gt;{0,-10}&lt;&quot; -f &quot;test&quot;</td>
<td align="right">&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test&lt;<br />
&gt;test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</td>
<td></td>
</tr>
</table>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poshstudent.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poshstudent.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poshstudent.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poshstudent.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poshstudent.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poshstudent.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poshstudent.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poshstudent.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poshstudent.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poshstudent.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poshstudent.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poshstudent.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poshstudent.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poshstudent.wordpress.com/73/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=73&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poshstudent.wordpress.com/2010/02/26/formatting-in-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>42.105874 -84.248282</georss:point>
		<geo:lat>42.105874</geo:lat>
		<geo:long>-84.248282</geo:long>
		<media:content url="http://0.gravatar.com/avatar/05bf0587857458ed131f3b8ad9f5dd6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sysiphus</media:title>
		</media:content>
	</item>
		<item>
		<title>Get-NewVariables -or- View Only Non-System Variables</title>
		<link>http://poshstudent.wordpress.com/2009/07/30/get-newvariables/</link>
		<comments>http://poshstudent.wordpress.com/2009/07/30/get-newvariables/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 14:18:20 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Get-Variable]]></category>
		<category><![CDATA[Variables]]></category>

		<guid isPermaLink="false">http://poshstudent.wordpress.com/?p=61</guid>
		<description><![CDATA[Sometimes, I run Get-Variable to search for a certain variable and am confronted with: Name Value ---- ----- Error {System.Management.Automation.IncompleteParseException: Missing closing '}' in statem... DebugPreference SilentlyContinue PROFILE C:\Documents and Settings\nrandall\My Documents\WindowsPowerShell\Microsoft.PowerShel... HOME F:\ Host System.Management.Automation.Internal.Host.InternalHost MaximumHistoryCount 64 MaximumAliasCount 4096 input System.Array+SZArrayEnumerator StackTrace at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandN... ReportErrorShowSource 1 ExecutionContext System.Management.Automation.EngineIntrinsics true True VerbosePreference SilentlyContinue ShellId [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=61&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes, I run <span style="color:#6d381f;"><strong>Get-Variable</strong></span> to search for a certain variable and am confronted with:</p>
<pre style="color:#6D381F;font-size:10px;">Name                           Value
----                           -----
Error                          {System.Management.Automation.IncompleteParseException: Missing closing '}' in statem...
DebugPreference                SilentlyContinue
PROFILE                        C:\Documents and Settings\nrandall\My Documents\WindowsPowerShell\Microsoft.PowerShel...
HOME                           F:\
Host                           System.Management.Automation.Internal.Host.InternalHost
MaximumHistoryCount            64
MaximumAliasCount              4096
input                          System.Array+SZArrayEnumerator
StackTrace                        at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandN...
ReportErrorShowSource          1
ExecutionContext               System.Management.Automation.EngineIntrinsics
true                           True
VerbosePreference              SilentlyContinue
ShellId                        Microsoft.PowerShell
false                          False
null
MaximumFunctionCount           4096
line                           get-v
ConsoleFileName
ReportErrorShowStackTrace      0
FormatEnumerationLimit         4
?                              True
PSHOME                         C:\WINDOWS\system32\WindowsPowerShell\v1.0
lfFolderID                     AAMkAGYzN2JlMjg3LWJhZDAtNDhiMC05MTBjLTk4MTI3ODZjNDQ1ZQAuAAAAAAAyytDMTMDeT4xZYpkktVmKA...
lastWord                       get-v
MyInvocation                   System.Management.Automation.InvocationInfo
PWD                            F:\
^                              (
_
ReportErrorShowExceptionClass  0
ProgressPreference             Continue
ErrorActionPreference          Continue
args                           {}
MaximumErrorCount              256
NestedPromptLevel              0
WhatIfPreference               0
OutputEncoding                 System.Text.ASCIIEncoding
ReportErrorShowInnerException  0
ErrorView                      NormalView
WarningPreference              Continue
PID                            5576
ConfirmPreference              High
MaximumDriveCount              4096
MaximumVariableCount           4096
$                              fl</pre>
<p>Not very helpful, usually. Sometimes I may want to remember that built-in variables like <em>FormatEnumerationLimit</em> or <em>MaximumDriveCount</em> exist, but typically I am more concerned with the variables I&#8217;ve created. And not only are there about 45 built-in variables to filter through, PoSH is not nice enough to offer anything resembling automatic sorting.</p>
<p>So I made my own way of dealing with it. In my Profile.ps1 file, I&#8217;ve added a few lines:</p>
<pre style="color:#6D381F;font-size:11px;">$coreVars = Get-Variable | %{$_.Name}
$coreVars += "coreVars","_"
function Get-NewVariables {
	Get-Variable | ?{$coreVars -notcontains $_.Name} | sort Name
	}</pre>
<p>When I open PowerShell, it automatically gets the system variables, and stores their names in array <span style="color:#6d381f;"><strong>$coreVars</strong></span>. It also stores that variable&#8217;s own name into the array (plus <span style="color:#6d381f;"><strong>$_</strong></span>, which normally won&#8217;t be created till later.) From then on, I can use the function <span style="color:#6d381f;"><strong>Get-NewVariables</strong></span> to get all variables created since I opened PowerShell. And because I like them sorted, I sorted it by the name.</p>
<p>And if I ever want to remove all the variables I have created since I started a session, there&#8217;s a one-liner for that:</p>
<pre style="color:#6D381F;font-size:11px;">Get-NewVariables | select name | Remove-Variable</pre>
<p>The one downside: no ability to use the switches you normally can with <span style="color:#6d381f;"><strong>Get-Variable</strong></span>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poshstudent.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poshstudent.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poshstudent.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poshstudent.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poshstudent.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poshstudent.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poshstudent.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poshstudent.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poshstudent.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poshstudent.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poshstudent.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poshstudent.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poshstudent.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poshstudent.wordpress.com/61/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=61&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poshstudent.wordpress.com/2009/07/30/get-newvariables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05bf0587857458ed131f3b8ad9f5dd6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sysiphus</media:title>
		</media:content>
	</item>
		<item>
		<title>Mailbox Database Sizes</title>
		<link>http://poshstudent.wordpress.com/2009/06/26/mailbox-database-sizes/</link>
		<comments>http://poshstudent.wordpress.com/2009/06/26/mailbox-database-sizes/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 17:09:02 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://poshstudent.wordpress.com/?p=54</guid>
		<description><![CDATA[Sometimes it&#8217;s helpful to know just how much data I have in each mailbox database; both in terms of number of mailboxes, and in terms of the total size of those mailboxes. The Get-MailboxDatabase command has no detail concerning the database size. Get-MailboxStatistics is great, so long as all the mailboxes are on Exchange 2007; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=54&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes it&#8217;s helpful to know just how much data I have in each mailbox database; both in terms of number of mailboxes, and in terms of the total size of those mailboxes. The <span style="color:#6d381f;"><strong>Get-MailboxDatabase</strong></span> command has no detail concerning the database size. <span style="color:#6d381f;"><strong>Get-MailboxStatistics</strong></span> is great, so long as all the mailboxes are on Exchange 2007; ours are currently split between 2007 and 2003. <span style="color:#6d381f;"><strong>Get-MailboxFolderStatistics</strong></span> works on 2007 and 2003 mailboxes, and the root folder (which, conveniently, is always array item <span style="color:#6d381f;">[0]</span>) will tell you the <em>FolderAndSubfolderSize</em>. Combine these together with some basic data from <span style="color:#6d381f;"><strong>Get-Mailbox</strong></span>, and you can cobble together a massive one-liner that will tell you how many mailboxes are in each database, and the sum of all the mailbox sizes. I split the lines with the <span style="color:#6d381f;"><strong>`</strong></span> character to improve readability.</p>
<pre style="color:#6D381F;">Get-Mailbox -ResultSize Unlimited |`
    Select-Object Database,@{Name="Size";Expression={
    	(Get-MailboxFolderStatistics $_)[0].FolderAndSubfolderSize.ToBytes()
    	}} |`
    Group-Object -Property Database |`
    Select-Object Name,Count,@{
    	Name="SizeGB";Expression={[math]::Round(($_.Group | Measure-Object Size -sum).Sum / 1gb,2)
    	}} |`
    Sort-Object SizeGB -Desc | Format-Table -AutoSize</pre>
<p>First, a warning: If you have 15,000 mailboxes spread across 3 servers, this will take a LONG time.  It took more than an hour for me.</p>
<p>So, let&#8217;s break this down:</p>
<p><span style="color:#6d381f;"><tt>Get-Mailbox -ResultSize Unlimited |`</tt></span><br />
This should be obvious. I&#8217;m getting every mailbox in my exchange organization. Since I have more than 1000, I have to use a larger (or unlimited) result size.</p>
<p><span style="color:#6d381f;"><tt>Select-Object Database,@{Name="Size";Expression={(Get-MailboxFolderStatistics $_)[0].FolderAndSubfolderSize.ToBytes()}}</tt></span><br />
The only thing I need from<strong> <span style="color:#6d381f;">Get-Mailbox</span></strong> is the database, and I also want the size. So I filter out all other properties with <span style="color:#6d381f;"><strong>Select-Object</strong></span>. The trickier part starts with the <span style="color:#6d381f;"><strong>@{</strong></span> symbols. Essentially, I&#8217;m opening a one-item Hashtable (aka Associative Array) to create a custom property to select. (There&#8217;s an article <a href="http://thepowershellguy.com/blogs/posh/archive/2009/01/26/new-customcolumn-function-powershell-v1-0.aspx">here</a> on what commands need what formats; just remember that you always need an <em>Expression</em>, <span style="color:#6d381f;"><strong>Format-*</strong></span> commands need a <em>Label</em>, and non <span style="color:#6d381f;"><strong>Format-*</strong></span> commands need a <em>Name</em>. ) I use the easy propertyname <em>Size</em>. For the value, I run <span style="color:#6d381f;"><strong>Get-MailboxFolderStatistics</strong></span> on the mailbox, and on the first folder item (<span style="color:#6d381f;">[0]</span>) I get convert the <em>FolderAndSubfolderSize</em> (which is a <em>Microsoft.Exchange.Data.ByteQuantifiedSize</em> data type) to bytes. If I were to stop at this point, I would have a massive table of DatabaseName and Size for each mailbox.</p>
<p><span style="color:#6d381f;"><tt>Group-Object -Property Database |`</tt></span><br />
This is where the real magic happens. Powershell sorts all these into groups based on the Database. The result is one group object per database. Each object has properties <em>Count</em> (number of objects in the group), <em>Name</em> (in this case, the name of each database), and <em>Group</em> (an array of the objects in the group.)</p>
<p><span style="color:#6d381f;"><tt>Select-Object Name,Count,@{n="SizeGB";e={[math]::Round(($_.Group | Measure-Object Size -sum).Sum / 1gb,2)}} |`</tt></span><br />
Since I want a total, not an array, I need to do one more bit of math on this. So I select the Name and Count again, and then pull a few tricks. I use <span style="color:#6d381f;"><tt>($_.Group | Measure-Object Size -sum).Sum / 1gb</tt></span> to &#8220;measure&#8221; the <em>Size</em> property of each object in the <em>Group</em> array, and get the Sum of all those properties.  The <span style="color:#6d381f;"><strong>Measure-Object</strong></span> command is a little strange in it&#8217;s output, so I had to then select the Sum property of it&#8217;s output. (Try running <span style="color:#6d381f;"><tt>1..10 | Measure-Object -sum</tt></span> to see why.) And since I didn&#8217;t want it in Bytes, I divided by GB. And since I didn&#8217;t want (up to) 15 decimal points, I used a .NET math function to round it to 2 decimal points. (Try <span style="color:#6d381f;"><tt>[math]::Round(15.2561428,1)</tt></span> and similar for an understanding of how that works.)</p>
<p><span style="color:#6d381f;"><tt>Sort-Object SizeGB -Desc | Format-Table -AutoSize</tt></span><br />
These last 2 commands are very optional. I wanted to sort it by the size of the database, but you could sort by any property or none at all. And it&#8217;s already in table format, but I didn&#8217;t like the way it spread across the whole console, so I autosized it down. But that&#8217;s personal preference.</p>
<p>One other thought: this can be written much smaller. In fact, I originally wrote it using aliases, but then I expanded it out to post. Here&#8217;s the smaller version.</p>
<pre style="color:#6D381F;">Get-Mailbox -Res Unlimited |`
    Select Database,@{N="Size";E={(Get-MailboxFolderStatistics $_)[0].FolderAndSubfolderSize.ToBytes()}} |`
    Group -P Database |`
    Select Name,Count,@{N="SizeGB";E={[math]::Round(($_.Group | Measure-Object Size -sum).Sum / 1gb,2)}} |`
    Sort SizeGB -Desc | FT -A</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poshstudent.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poshstudent.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poshstudent.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poshstudent.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poshstudent.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poshstudent.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poshstudent.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poshstudent.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poshstudent.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poshstudent.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poshstudent.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poshstudent.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poshstudent.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poshstudent.wordpress.com/54/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=54&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poshstudent.wordpress.com/2009/06/26/mailbox-database-sizes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05bf0587857458ed131f3b8ad9f5dd6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sysiphus</media:title>
		</media:content>
	</item>
		<item>
		<title>Bad ASCII characters</title>
		<link>http://poshstudent.wordpress.com/2009/06/23/bad-ascii-characters/</link>
		<comments>http://poshstudent.wordpress.com/2009/06/23/bad-ascii-characters/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 17:12:37 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ASCII]]></category>
		<category><![CDATA[▼]]></category>
		<category><![CDATA[hints]]></category>
		<category><![CDATA[illegal characters]]></category>
		<category><![CDATA[odd characters]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[unit separator]]></category>
		<category><![CDATA[[char]]]></category>

		<guid isPermaLink="false">http://poshstudent.wordpress.com/?p=49</guid>
		<description><![CDATA[I was reading a database table, and trying to sort and display certain data. The table was the printers that are mapped by ScriptLogic Desktop Authority. All was going well, until I got to the point of displaying the rules. Some are simple, others are multiple: /S=BCREEK means Site is Battle Creek /W=2t1922 means Workstation [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=49&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was reading a database table, and trying to sort and display certain data. The table was the printers that are mapped by ScriptLogic Desktop Authority. All was going well, until I got to the point of displaying the rules. Some are simple, others are multiple:</p>
<ol>
<li><strong>/S=BCREEK</strong> means <em>Site is Battle Creek</em></li>
<li><strong>/W=2t1922</strong> means <em>Workstation name is 2t1922</em></li>
<li><strong>_/S=TOLEDO</strong> means <em>Site is NOT Toledo</em></li>
<li><strong>/W=2t1921▼▼/W=3t1656▼▼/W=4ibm4070▼▼/W=7ibm5011▼▼/W=7ibm4865</strong> means any of these workstations.</li>
</ol>
<p>But&#8230; what&#8217;s with the upside-down triangles?</p>
<p>And often I will split these sorts of things up into an array, but I get nothing from:</p>
<pre style="color:#6D381F;">$_.Rules.Split("▼")</pre>
<p>Hmm. So what it&#8217;s outputting is obviously not the same character it&#8217;s showing me. But what is it? What I needed was to get the ASCII code for that character.</p>
<pre style="color:#6D381F;">PS C:\&gt; $dataset.Tables[0].rows[0].Rules
CO=\- SAU COMP\CAMPUS\Art Lab\*▼▼/G=ARBOR\gaink1
PS C:\&gt; $temp = $dataset.Tables[0].rows[0].Rules.SubString(30,5)
PS C:\&gt; $temp.ToCharArray()
*
▼
▼
/
G
PS C:\&gt; $temp.ToCharArray() | %{ [int]$_ }
42
31
31
47
71</pre>
<p>Going back through this: I displayed the rule itself, then set a variable to the 21st through 25th characters (*▼▼/G), then displayed them as an array (for future comparison) then, for each for them, displayed the ASCII code by converting the letter to an [int]. I can then check the codes <a href="http://www.asciitable.com/">here</a>. Sure enough, &#8220;*&#8221; is character 42, &#8220;/&#8221; is 47, and &#8220;G&#8221; is 71. Thus, I can look and know that character 31 is &#8220;(unit separator)&#8221;. No wonder it didn&#8217;t display!</p>
<p>So what to DO with it?</p>
<pre style="color:#6D381F;">PS C:\&gt; $dataset.Tables[0].rows[0].Rules.Replace([char]31,"^").Replace("^^","^").Split("^")
CO=\- SAU COMP\CAMPUS\Art Lab\*
/G=ARBOR\gaink1</pre>
<p>Conveniently, the Replace() function on strings allows you to cheat a little. So I replaced every ▼ with a ^ (you can use any char you want), replaced every double ^ with a single, then split it on that character.</p>
<p><strong>Edit:</strong> If you are curious how some of the other characters will display, you can easily show them all with one of a few methods:</p>
<pre style="color:#6D381F;">0..127 | %{ [char]$_ }
0..255 | %{ [char]$_ }
0..127 | %{ "{0:0##} : $([char]$_)" -f $_ }
0..255 | %{ write-host -n ("{0:0##} : $([char]$_)   " -f $_) }</pre>
<p>These lines will, in the following order:</p>
<ol>
<li>Display the first 128 ASCII characters (the &#8216;standard&#8217; set), one per line.</li>
<li>Display the first 256 ASCII characters (&#8216;standard&#8217; and &#8216;extended&#8217;.)</li>
<li>Display each character, preceded by its character number and a colon, one per line.</li>
<li>Display #3, but one one (wrapping) line. It is forced to use 3 digit numbers with leading zeros.</li>
</ol>
<p>One warning on all of these: <strong>[char]007</strong> is &#8220;Bell&#8221;. That means your computer&#8217;s internal speaker will beep. If you really want to mess with someone, include that character on every iteration of a large loop&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poshstudent.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poshstudent.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poshstudent.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poshstudent.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poshstudent.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poshstudent.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poshstudent.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poshstudent.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poshstudent.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poshstudent.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poshstudent.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poshstudent.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poshstudent.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poshstudent.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=49&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poshstudent.wordpress.com/2009/06/23/bad-ascii-characters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05bf0587857458ed131f3b8ad9f5dd6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sysiphus</media:title>
		</media:content>
	</item>
		<item>
		<title>Get Subnets of Sites</title>
		<link>http://poshstudent.wordpress.com/2009/06/19/get-subnets-of-sites/</link>
		<comments>http://poshstudent.wordpress.com/2009/06/19/get-subnets-of-sites/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 14:42:23 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[AD Sites]]></category>
		<category><![CDATA[hints]]></category>
		<category><![CDATA[subnets]]></category>

		<guid isPermaLink="false">http://poshstudent.wordpress.com/?p=22</guid>
		<description><![CDATA[In trying to troubleshoot an issue, I needed to determine what subnets we were actually using for our 15 sites. Under Active Directory Sites and Services I can see all the subnets under the subnet folder, but I wanted a way to play with the way the data is displayed, and be able to copy [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=22&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In trying to troubleshoot an issue, I needed to determine what subnets we were actually using for our 15 sites. Under <strong>Active Directory Sites and Services</strong> I can see all the subnets under the subnet folder, but I wanted a way to play with the way the data is displayed, and be able to copy and paste it. Turns out it&#8217;s easy under PoSH, so long as you know where to look.</p>
<pre style="color:#6D381F;">$forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$forest.Sites | ft Name,Subnets -Auto</pre>
<p>Of course, that&#8217;s not <em>quite</em> what I wanted; the formatting was wrong, and it cut off some of the subnets at our largest site. I was also curious what other properties there were:</p>
<pre style="color:#6D381F;">PS C:\&gt;$forest.Sites[11]
Name                           : TOLEDO
Domains                        : {domain.tld}
Subnets                        : {192.168.38.0/24}
Servers                        : {TOL-SRV1.toledo.domain.tld}
AdjacentSites                  : {NOC}
SiteLinks                      : {NOC-TOL}
InterSiteTopologyGenerator     : TOL-SRV1.toledo.domain.tld
Options                        : None
Location                       :
BridgeheadServers              : {TOL-SRV1.toledo.domain.tld}
PreferredSmtpBridgeheadServers : {}
PreferredRpcBridgeheadServers  : {}
IntraSiteReplicationSchedule   : System.DirectoryServices.ActiveDirectory.ActiveDirectorySchedule</pre>
<p>For the time being, all I really want are <strong>Name</strong> and <strong>Subnets</strong>, but it&#8217;s useful to know the other info is available. As to the subnets, the <strong>{}</strong> characters around the data tells me that it&#8217;s a multi-valued field, so I can extract the individual subnets. I can either use:</p>
<pre style="color:#6D381F;">$sites = $forest.Sites
$sites | %{$_.Subnets}</pre>
<p>or, if I don&#8217;t want to continue to play with <em>$sites</em>:</p>
<pre style="color:#6D381F;">PS C:\&gt; $forest.Sites | %{$_.Subnets}

Name                                    Site                                    Location
----                                    ----                                    --------
10.101.0.0/16                           NOC
10.0.0.0/8                              NOC
10.102.0.0/16                           NOC
10.250.0.0/16                           NOC
10.103.0.0/16                           NOC
10.104.0.0/16                           NOC
10.111.0.0/16                           NOC
172.16.0.0/14                           NOC
192.168.45.0/24                         JACKSON                                 Jackson
192.168.41.0/24                         BCREEK                                  Battle Creek
192.168.42.0/24                         DETROIT                                 Detroit
192.168.44.0/24                         FLINT                                   Flint
192.168.43.0/24                         GAYLORD                                 Gaylord
192.168.39.0/24                         GRAPIDS                                 Grand Rapids
192.168.35.0/24                         KZOO                                    Kalamazoo
192.168.40.0/24                         LANSING                                 Lansing
192.168.32.0/24                         PETOSKEY                                Petoskey
192.168.38.0/24                         TOLEDO                                  Toledo
192.168.34.0/24                         TRAVERSE                                Traverse City
192.168.37.0/24                         TROY                                    Troy
192.168.30.0/24                         Sandbox</pre>
<p>And from there, I can see the subnets and where they belong. I could even sort, filter, or otherwise manipulate them. In the end, I found the problem, too. My <strong>10.0.0.0/8</strong> subnet was actually <strong>10.0.0.0/16</strong>; I changed it after discovering the problem, but before writing this, and thus corrected the catch-all subnet to, well, catch all.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poshstudent.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poshstudent.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poshstudent.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poshstudent.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poshstudent.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poshstudent.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poshstudent.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poshstudent.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poshstudent.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poshstudent.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poshstudent.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poshstudent.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poshstudent.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poshstudent.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poshstudent.wordpress.com&amp;blog=7460493&amp;post=22&amp;subd=poshstudent&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poshstudent.wordpress.com/2009/06/19/get-subnets-of-sites/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/05bf0587857458ed131f3b8ad9f5dd6f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sysiphus</media:title>
		</media:content>
	</item>
	</channel>
</rss>
