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

<channel>
	<title>Elan Shudnow's Blog &#187; PowerShell</title>
	<atom:link href="http://www.shudnow.net/tag/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shudnow.net</link>
	<description>Just another IT guy!</description>
	<lastBuildDate>Fri, 30 Jul 2010 18:19:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Another Exchange Scriplet (Moving Mailboxes)</title>
		<link>http://www.shudnow.net/2009/05/19/another-exchange-scriplet-moving-mailboxes/</link>
		<comments>http://www.shudnow.net/2009/05/19/another-exchange-scriplet-moving-mailboxes/#comments</comments>
		<pubDate>Tue, 19 May 2009 18:34:50 +0000</pubDate>
		<dc:creator>Elan Shudnow</dc:creator>
				<category><![CDATA[Exchange]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://www.shudnow.net/?p=1063</guid>
		<description><![CDATA[This one I created for the following reasons: 1. Mailbox Database was filling up 2. Wanted to take the filled up database and split them across two different databases. The script will sort all the mailboxes by size and take 40% of the largest mailboxes and move them to a specified database and take the [...]]]></description>
			<content:encoded><![CDATA[<p>This one I created for the following reasons:<br />
1. Mailbox Database was filling up<br />
2. Wanted to take the filled up database and split them across two different databases.</p>
<p>The script will sort all the mailboxes by size and take 40% of the largest mailboxes and move them to a specified database and take the remaining 60% and move them to the other database.</p>
<p>If you don&#8217;t want to specify the exact database name and just want PowerShell to use part of the name you specify to find a matching database, you can change the part where it states {$_.Name -eq $sourceDB} to have -like instead of -eq.  Do the same for the following two pieces of code: {$_.Name -eq $targetDBLarge} and {$_.Name -eq $targetDBSmall}.  -eq needs the exact match for the database name and -like basically takes the database name and puts wild cards around it so it&#8217;s more like *databaseyouspecify* and then finds a matching database that is like the name you specified.</p>
<p><span style="color: #ff0000;">Important</span>: I added -whatif switches to the move-mailbox commands below to be extra safe.  To actually move the mailboxes, you&#8217;ll need to remove the -whatif.   And as always, <strong>MAKE SURE YOU TEST THIS IN LAB BEFORE RUNNING IT IN PRODUCTION AND HAVE A GOOD BACKUP UP AD/EXCHANGE/ETC AS WELL!</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1063code2'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10632"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p1063code2"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$sourceDB</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Specify your Source Database Here&quot;</span>
<span style="color: #800080;">$targetDBLarge</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Specify your larger database in which 40% of the largest mailboxes will be moved to&quot;</span>
<span style="color: #800080;">$targetDBSmall</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Specify your smaller database in which the remaining 60% of the smaller mailboxes will be moved to&quot;</span>
&nbsp;
<span style="color: #008000;">############################################</span>
<span style="color: #008000;">####### Don't modify below this line #######</span>
<span style="color: #008000;">############################################</span>
&nbsp;
<span style="color: #800080;">$a</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>MailboxStatistics <span style="color: pink;">-</span>Database $<span style="color: #000000;">&#40;</span>Get<span style="color: pink;">-</span>Mailboxdatabase <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$sourceDB</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.ObjectClass <span style="color: #FF0000;">-NotMatch</span> <span style="color: #800000;">'(SystemAttendantMailbox|ExOleDbSystemMailbox)'</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Sort-Object</span> Totalitemsize
&nbsp;
<span style="color: #800080;">$rounded</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$a</span>.count <span style="color: pink;">*</span> .4<span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$first</span> <span style="color: pink;">=</span> <span style="color: #800080;">$rounded</span>
<span style="color: #800080;">$last</span> <span style="color: pink;">=</span> <span style="color: #800080;">$a</span>.count <span style="color: pink;">-</span> <span style="color: #800080;">$first</span>
&nbsp;
<span style="color: #800080;">$a</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select-object</span> <span style="color: #008080; font-style: italic;">-First</span> <span style="color: #800080;">$first</span> <span style="color: pink;">|</span> Move<span style="color: pink;">-</span>Mailbox <span style="color: pink;">-</span>TargetDatabase $<span style="color: #000000;">&#40;</span>Get<span style="color: pink;">-</span>Mailboxdatabase <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$targetDBLarge</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-whatif</span>
<span style="color: #800080;">$a</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select-object</span> <span style="color: #008080; font-style: italic;">-Last</span> <span style="color: #800080;">$last</span> <span style="color: pink;">|</span> Move<span style="color: pink;">-</span>Mailbox <span style="color: pink;">-</span>TargetDatabase $<span style="color: #000000;">&#40;</span>Get<span style="color: pink;">-</span>Mailboxdatabase <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$targetDBSmall</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-whatif</span></pre></td></tr></table></div>

<img src="http://www.shudnow.net/?ak_action=api_record_view&id=1063&type=feed" alt="" /><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.shudnow.net/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.shudnow.net/2009/05/19/another-exchange-scriplet-moving-mailboxes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A few Exchange PowerShell scriptlets</title>
		<link>http://www.shudnow.net/2009/05/14/a-few-exchange-powershell-scriptlets/</link>
		<comments>http://www.shudnow.net/2009/05/14/a-few-exchange-powershell-scriptlets/#comments</comments>
		<pubDate>Thu, 14 May 2009 18:07:14 +0000</pubDate>
		<dc:creator>Elan Shudnow</dc:creator>
				<category><![CDATA[Exchange]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://www.shudnow.net/?p=1024</guid>
		<description><![CDATA[Figured I would share a few scriptlets I&#8217;ve written recently.  Hopefully some of you may find them useful at some point at least. Scriptlet 1 Find out all users who live on a specific mailbox without having to type the entire mailbox database name: ?View Code POWERSHELL1 get-mailbox -database $&#40;get-mailboxdatabase &#124; where-object &#123;$_.name -like &#34;*Database1*&#34;&#125;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>Figured I would share a few scriptlets I&#8217;ve written recently.  Hopefully some of you may find them useful at some point at least.</p>
<h4>Scriptlet 1</h4>
<p>Find out all users who live on a specific mailbox without having to type the entire mailbox database name:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1024code8'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10248"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1024code8"><pre class="powershell" style="font-family:monospace;">get<span style="color: pink;">-</span>mailbox <span style="color: pink;">-</span>database $<span style="color: #000000;">&#40;</span>get<span style="color: pink;">-</span>mailboxdatabase <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">where-object</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.name <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*Database1*&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Format-Table</span> Name<span style="color: pink;">,</span>Database <span style="color: #008080; font-style: italic;">-wrap</span> <span style="color: #008080; font-style: italic;">-autosize</span></pre></td></tr></table></div>

<h4>Scriptlet 2</h4>
<p>Get statistics on a specific user without knowing the entire name or if you&#8217;d want to find out statistics on all users who have the first name John or last name Doe:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1024code9'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10249"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1024code9"><pre class="powershell" style="font-family:monospace;">Get<span style="color: pink;">-</span>MailboxStatistics <span style="color: pink;">-</span>server serverhere <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.DisplayName <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*MailboxUserName*&quot;</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Format-Table</span> DisplayName<span style="color: pink;">,</span>ItemCount<span style="color: pink;">,</span>TotalItemSize</pre></td></tr></table></div>

<h4>Scriptlet 3</h4>
<p>When you are in an environment with multiple Mailbox Servers, it may be annoying to find what disconnected mailbox lives on which server since the Exchange Management Console will only connect to one server at a time and when connected, it&#8217;ll only show disconnected mailboxes for that specific server.  This scriptlet will cycle through all mailbox servers and report back all disconnected mailbox servers and which disconnected mailbox lives on which mailbox server.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1024code10'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p102410"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1024code10"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$mbx</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>ExchangeServer <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.IsMailboxserver <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$server</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$mbx</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	Get<span style="color: pink;">-</span>Mailboxstatistics <span style="color: pink;">-</span>Server <span style="color: #800080;">$Server</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.DisconnectDate <span style="color: #FF0000;">-ne</span> <span style="color: #800080;">$null</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Format-Table</span> DisplayName<span style="color: pink;">,</span>ItemCount<span style="color: pink;">,</span>OriginatingServer <span style="color: #008080; font-style: italic;">-wrap</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h4>Scriptlet 4</h4>
<p>Based on a CSV file, export users contacts that were created after a certain start date to a PST file.  A copy of how the CSV should be formatted is located <a href="http://www.shudnow.net/files/pstusers.csv">here</a>.  While the CSV contains multiple columns, the script only utilizes the Account Name column by default.  If you want to modify what columns it uses, you can change the line below that starts with $AdName =.  After the script pulls the name in the Account Name field, it finds the mailbox using the Get-Mailbox command with the name that was specified in the Account Name field.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1024code11'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p102411"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p1024code11"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$file</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;pstusers.csv&quot;</span>
<span style="color: #800080;">$PSTLocation</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\PSTs&quot;</span>
<span style="color: #800080;">$StartDate</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;03/20/2009&quot;</span>
&nbsp;
<span style="color: #008000;">############################################</span>
<span style="color: #008000;">####### Don't modify below this line #######</span>
<span style="color: #008000;">############################################</span>
<span style="color: #800080;">$erroractionpreference</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SilentlyContinue&quot;</span>
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: pink;">!</span><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Test-Path</span> <span style="color: #008080; font-style: italic;">-path</span> <span style="color: #800080;">$PSTLocation</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Creating the Directory $PSTLocation&quot;</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> Yellow
	<span style="color: #008080; font-weight: bold;">New-Item</span> <span style="color: #008080; font-style: italic;">-ItemType</span> <span style="color: #800000;">&quot;Directory&quot;</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$PSTlocation</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Test-Path</span> <span style="color: #800080;">$file</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$excel</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Import-CSV</span> <span style="color: #800080;">$file</span>
	<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$excel</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$AdName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$line</span>.<span style="color: #800000;">&quot;Account Name&quot;</span>
		<span style="color: #800080;">$Mailbox</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>Mailbox <span style="color: #800080;">$AdName</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$Mailbox</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;$AdName is being exported to $PSTLocation&quot;</span>
			<span style="color: #800080;">$Mailbox</span> <span style="color: pink;">|</span> Export<span style="color: pink;">-</span>Mailbox <span style="color: pink;">-</span>PSTFolderPath <span style="color: #800080;">$PSTLocation</span> <span style="color: pink;">-</span>StartDate <span style="color: #800080;">$StartDate</span> <span style="color: pink;">-</span>ExcludeFolders <span style="color: #800000;">&quot;\Inbox&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;\Deleted Items&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;\Drafts&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;\Junk E-mail&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;\Outbox&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;\Sent Items&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;\Journal&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;\Calendar&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;\Notes&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;\Tasks&quot;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Warning</span> <span style="color: #800000;">&quot;$ADName Mailbox Does not Exist&quot;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Warning</span> <span style="color: #800000;">&quot;The file $file does not exist&quot;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<h4>Scriptlet 5</h4>
<p>If you are familiar with Quest Notes Migrator for Exchange, you may have had the chance to migrate data to a PST file.  What you will see, is that the PST gets migrated with the Display Name of the user.  For example, we if migrated the Notes mailbox of Elan Shudnow to a PST, the PST file name would be Elan Shudnow.PST. A lot of people won&#8217;t want to migrate the PST directly from Quest NME to a user&#8217;s home share because that user&#8217;s home share may be located in a remote site and opening a PST file across a network isn&#8217;t really a good idea; especially over long distances.</p>
<p>So I wrote a script so you can migrate all the PST files locally and then it&#8217;ll check the Display Name portion of the PST file and use that display name to find a matching user in AD and then find their home directory and then copy that PST file to that home share.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1024code12'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p102412"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p1024code12"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Set Location to where the PST files are started</span>
<span style="color: #008080; font-weight: bold;">Set-Location</span> <span style="color: #800000;">&quot;E:\&quot;</span>
&nbsp;
<span style="color: #800080;">$pstData</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-recurse</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.Name <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*.pst&quot;</span><span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>HomeDirectory <span style="color: #000000;">&#40;</span><span style="color: #800080;">$displayName</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$ads</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.DirectoryServices.DirectorySearcher<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>ADSI<span style="color: #000000;">&#93;</span><span style="color: #800000;">''</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$ads</span>.<span style="color: #0000FF;">filter</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;(&amp;(objectClass=Person)(displayName=$displayName))&quot;</span>
	<span style="color: #800080;">$s</span> <span style="color: pink;">=</span> <span style="color: #800080;">$ads</span>.FindOne<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #0000FF;">return</span> <span style="color: #800080;">$s</span>.GetDirectoryEntry<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.homeDirectory
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$pstUser</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$pstData</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$PSTName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$pstUser</span>.Name
	<span style="color: #800080;">$PSTDisplayName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$pstUser</span>.Name.split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>
	<span style="color: #800080;">$PSTDisplayName</span> <span style="color: pink;">=</span> <span style="color: #800080;">$PSTDisplayName</span>.split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;-&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span>
	<span style="color: #800080;">$Path</span> <span style="color: pink;">=</span> <span style="color: #800080;">$PSTUser</span>.DirectoryName <span style="color: pink;">+</span> <span style="color: #800000;">&quot;\&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$PSTUser</span>.Name
	<span style="color: #800080;">$Directory</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>HomeDirectory <span style="color: #800080;">$PSTDisplayName</span>
	<span style="color: #008080; font-weight: bold;">Copy-Item</span> <span style="color: #800080;">$Path</span> <span style="color: #800080;">$Directory</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<img src="http://www.shudnow.net/?ak_action=api_record_view&id=1024&type=feed" alt="" /><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.shudnow.net/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.shudnow.net/2009/05/14/a-few-exchange-powershell-scriptlets/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PowerShell and its future</title>
		<link>http://www.shudnow.net/2008/11/05/powershell-and-its-future/</link>
		<comments>http://www.shudnow.net/2008/11/05/powershell-and-its-future/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 02:29:22 +0000</pubDate>
		<dc:creator>Elan Shudnow</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://www.shudnow.net/?p=469</guid>
		<description><![CDATA[As has been stated in the past, I am currently learning PowerShell (I am new to scripting).  Unfortunately, the last month has been a bit busy for me and I haven&#8217;t been able to focus on PowerShell at all.  But I&#8217;m getting back into it now.  Upon getting back into it, I discovered a couple [...]]]></description>
			<content:encoded><![CDATA[<p>As has been stated in the past, I am currently learning PowerShell (I am new to scripting).  Unfortunately, the last month has been a bit busy for me and I haven&#8217;t been able to focus on PowerShell at all.  But I&#8217;m getting back into it now.  Upon getting back into it, I discovered a couple cool utilities as well as information about PowerShell&#8217;s future and figured I would share this information with you.</p>
<p>I was debating on purchasing <a href="http://www.adminscripteditor.com/">AdminScriptEditor</a> for its ability to create create PowerShell forms and because I&#8217;d like to have a good Form Builder.  Recently, <a href="http://www.sapien.com/">Sapien Technologies, Inc</a>. released a free tool called <a href="http://www.primaltools.com/freetools/">PrimalForms </a>to do exactly this, create PowerShell Windows Forms.   They also have many other free tools such as WMIExplorer, Windows PowerShell Help Tool, and many more.  It&#8217;s only a matter of time before the next version of PrimalScript is out that&#8217;ll have some awesome features for PowerShell.</p>
<p>I also found some more information about PowerShell&#8217;s future and how it will relate to Windows Server 2008 R2 and Windows 7.  Fortunately, Windows PowerShell will be installed and accessible by default in both.  This version of Windows PowerShell will be version 2 which will support many new features such as a graphical powershell window, the ability to do remoting, and much more.  You can read about what&#8217;s coming in version 2 <a href="http://blogs.msdn.com/powershell/archive/2007/11/06/the-community-technology-preview-ctp-of-windows-powershell-2-0.aspx">here</a>.  Information about PowerShell being installed by default on Windows Server 2008 R2 and Windows 7 can be found <a href="http://blogs.msdn.com/powershell/archive/2008/10/28/powershell-will-be-installed-by-default-on-windows-server-08-r2-ws08r2-and-windows-7-w7.aspx">here</a>.</p>
<p>As many PowerShell people know, there was a lack of AD cmdlets in Server 2008.  The reason for this is Server 2008 development was too far along for cmdlets.  Fortunately, Quest provides QAD cmdlets to provide a lot of Active Directory Manamgent to alleviate the tediousness of connecting to ADSI to perform actions.  These QAD cmdlets can be located <a href="http://www.quest.com/powershell/activeroles-server.aspx">here</a>.</p>
<p>So what will will Server 2008 R2 and Windows 7 bring that makes having PowerShell version 2 so wonderful?  Well, having Powershell version 2 is only one thing.  There&#8217;s also a net total so far of 574 cmdlets added!  WOW!  This isn&#8217;t even including Providers.  The AD Management tool(s) are built on top of PowerShell and so are the remoting capabilities of Server Manager.  Dmitry posted a list of cmdlets <a href="http://dmitrysotnikov.wordpress.com/2008/11/07/full-list-of-ad-cmdlets/?referer=sphere_related_content/">here</a>.</p>
<p>Microsoft is definitely moving towards a model that anything that can be done in the GUI can be done through the CLI.  And  PowerShell is a great scripting language which means you can easily use tools such as PrimalForms to create your own Windows GUI to run PowerShell cmdlets and display them into a GUI as you see fit.  You can read more about this <a href="http://blogs.msdn.com/powershell/archive/2008/10/29/574-reasons-why-we-are-so-proud-and-optimistic-about-w7-and-ws08r2.aspx">here</a>.</p>
<img src="http://www.shudnow.net/?ak_action=api_record_view&id=469&type=feed" alt="" /><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.shudnow.net/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.shudnow.net/2008/11/05/powershell-and-its-future/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Obtaining Exchange Information including Edge with PowerShell</title>
		<link>http://www.shudnow.net/2008/09/25/obtaining-exchange-information-including-edge-with-powershell/</link>
		<comments>http://www.shudnow.net/2008/09/25/obtaining-exchange-information-including-edge-with-powershell/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 14:39:16 +0000</pubDate>
		<dc:creator>Elan Shudnow</dc:creator>
				<category><![CDATA[Exchange]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://www.shudnow.net/?p=375</guid>
		<description><![CDATA[So the last month I&#8217;ve been learning how to do PowerShell scripting. I&#8217;m new at scripting and figured I would share my first Exchange script due to sheer excitement. I&#8217;d like to give a special thanks to Shay Levy @ http://blogs.microsoft.co.il/blogs/ScriptFanatic. Shay has been a tremendous help over @ http://powershellcommunity.org when I run into issues [...]]]></description>
			<content:encoded><![CDATA[<p>So the last month I&#8217;ve been learning how to do PowerShell scripting.  I&#8217;m new at scripting and figured I would share my first Exchange script due to sheer excitement.  I&#8217;d like to give a special thanks to Shay Levy @  <a href="http://blogs.microsoft.co.il/blogs/ScriptFanatic">http://blogs.microsoft.co.il/blogs/ScriptFanatic</a>.  Shay has been a tremendous help over @ <a href="http://powershellcommunity.org">http://powershellcommunity.org</a> when I run into issues that any typical novice runs into (especially with scripting).  The other folks have been a great help as well.</p>
<p>I started this script last week and will be adding to it as I gain more knowledge. The skeleton of the script is complete with 1 function so far (to get disk information) and will allow me to easily add more functions to the script. I wanted to provide a method to dynamically check if a server was an Edge Server and prompt for authentication to allow PowerShell to obtain information from my Edge Servers.</p>
<p>With an Edge server, it is not going to be a part of your corporate domain . It may be a part of a forest/domain dedicated in your DMZ, but not your corporate forest/domain. Typically if you want to pull information from a domain-joined machine that is a part of your corporate domain, you just run a command against it and it&#8217;ll work if you have the access to do so. But since the Edge server is a part of a workgroup or DMZ forest/domain, it&#8217;s not that easy. This script will check if you are trying to check an Edge Server, and if so, it will display on the console that it is checking for Edge authentication and prompt you for credentials for that Edge Server.</p>
<p>Here&#8217;s the script I have so far:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p375code16'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37516"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
</pre></td><td class="code" id="p375code16"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Define what paramters (-role) can be utilized when running the switch</span>
<span style="color: #0000FF;">Param</span><span style="color: #000000;">&#40;</span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$role</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;All&quot;</span>
<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;"># Define what values (Mbx, Cas, Etc.) can be utilized when using our -role parameter</span>
<span style="color: #0000FF;">switch</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$role</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Mbx <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$role</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;IsMailboxServer&quot;</span> <span style="color: #000000;">&#125;</span>
	Cas <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$role</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;IsClientAccessServer&quot;</span> <span style="color: #000000;">&#125;</span>
	Um <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$role</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;IsUnifiedMessagingServer&quot;</span> <span style="color: #000000;">&#125;</span>
	Hub <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$role</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;IsHubTransportServer&quot;</span> <span style="color: #000000;">&#125;</span>
	Edge <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$role</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;IsEdgeServer&quot;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Test host to see if it replies to ping prior to allowing information gather functions to proceed.</span>
<span style="color: #0000FF;">function</span> Ping<span style="color: pink;">-</span>Host
<span style="color: #000000;">&#123;</span>
       <span style="color: #800080;">$result</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Gwmi</span> <span style="color: #008080; font-style: italic;">-Query</span> <span style="color: #800000;">&quot;SELECT * FROM Win32_PingStatus WHERE Address='$server'&quot;</span>
       <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$result</span>.statuscode <span style="color: #FF0000;">-eq</span> <span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;">$true</span><span style="color: #000000;">&#125;</span> <span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;">$false</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Function to display disk information.  If the server being checked is an Edge Server, you will be prompted for authentication.</span>
<span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>DiskInformation
<span style="color: #000000;">&#123;</span>
	<span style="color: #800000;">&quot;&quot;</span>
	<span style="color: #800000;">&quot;Server: $server&quot;</span>
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$server</span>.isEdgeServer<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
                <span style="color: #800080;">$erroractionpreference</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SilentlyContinue&quot;</span>
		<span style="color: #800080;">$cred</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Credential</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$cred</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$disk</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> Win32_LogicalDisk <span style="color: #008080; font-style: italic;">-namespace</span> root\cimv2 –<span style="color: #0000FF;">filter</span> <span style="color: #800000;">&quot;DriveType = 3&quot;</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$server</span> <span style="color: #008080; font-style: italic;">-Credential</span> <span style="color: #800080;">$cred</span> <span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Warning</span> <span style="color: #800000;">&quot;Could not obtain disk information for $server due to no credentials being provided&quot;</span> ; <span style="color: #0000FF;">break</span> <span style="color: #000000;">&#125;</span>
                <span style="color: #800080;">$erroractionpreference</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Continue&quot;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$disk</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> Win32_LogicalDisk <span style="color: #008080; font-style: italic;">-namespace</span> root\cimv2 –<span style="color: #0000FF;">filter</span> <span style="color: #800000;">&quot;DriveType = 3&quot;</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$server</span> <span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$disk</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$obj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> PSObject
		<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> NoteProperty <span style="color: #800000;">&quot;Size(G)&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.Size<span style="color: pink;">/</span>1GB<span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> NoteProperty <span style="color: #800000;">&quot;Freespace(G)&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> NoteProperty <span style="color: #800000;">&quot;Used(G)&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.Size<span style="color: pink;">/</span>1GB <span style="color: pink;">-</span> <span style="color: #800080;">$d</span>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> NoteProperty <span style="color: #800000;">&quot;Freespace(%)&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.Size<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> NoteProperty <span style="color: #800000;">&quot;Usedspace(%)&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span> <span style="color: pink;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.Size<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$obj</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Place servers into the $colServers variable which will later be fed into Functions</span>
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$role</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;All&quot;</span> <span style="color: #FF0000;">-and</span> <span style="color: #800080;">$role</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;IsEdgeServer&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$colServers</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>ExchangeServer <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.<span style="color: #800080;">$role</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;True&quot;</span> <span style="color: #FF0000;">-and</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.IsEdgeServer <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;True&quot;</span><span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">elseif</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$role</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;All&quot;</span> <span style="color: #FF0000;">-and</span> <span style="color: #800080;">$role</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;IsEdgeServer&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$colServers</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>ExchangeServer <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Where-Object</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.<span style="color: #800000;">&quot;IsEdgeServer&quot;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;True&quot;</span><span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$colServers</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>ExchangeServer <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: pink;">!</span><span style="color: #800080;">$colServers</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Warning</span> <span style="color: #800000;">&quot;There are no servers of the specified type to gather information for.&quot;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">else</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$server</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$colServers</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span>Ping<span style="color: pink;">-</span>Host <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;$true&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			Get<span style="color: pink;">-</span>DiskInformation
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">else</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Write-Warning</span> <span style="color: #800000;">&quot;$server is not pingable&quot;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>There&#8217;s one piece of the script I want to disuss.  It&#8217;s the function for Get-DiskInformation. I used to have the function written as such.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p375code17'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37517"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p375code17"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>DiskInformation
<span style="color: #000000;">&#123;</span>
	<span style="color: #800000;">&quot;&quot;</span>
	<span style="color: #800000;">&quot;Server: $server&quot;</span>
	$<span style="color: #000000;">&#40;</span><span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$server</span>.isEdgeServer<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> Win32_LogicalDisk <span style="color: #008080; font-style: italic;">-namespace</span> root\cimv2 –<span style="color: #0000FF;">filter</span> <span style="color: #800000;">&quot;DriveType = 3&quot;</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$server</span> <span style="color: #008080; font-style: italic;">-Credential</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Credential</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> Win32_LogicalDisk <span style="color: #008080; font-style: italic;">-namespace</span> root\cimv2 –<span style="color: #0000FF;">filter</span> <span style="color: #800000;">&quot;DriveType = 3&quot;</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$server</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> `
	<span style="color: #008080; font-weight: bold;">Format-Table</span> <span style="color: #008080; font-style: italic;">-autosize</span> DeviceID<span style="color: pink;">,</span>VolumeName<span style="color: pink;">,</span>`
	<span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>Label<span style="color: pink;">=</span><span style="color: #800000;">&quot;Size(G)&quot;</span>;Expression<span style="color: pink;">=</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span>math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.Size<span style="color: pink;">/</span>1GB<span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: pink;">,</span>`
	<span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>Label<span style="color: pink;">=</span><span style="color: #800000;">&quot;Freespace(G)&quot;</span>;Expression<span style="color: pink;">=</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span>math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: pink;">,</span>`
	<span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>Label<span style="color: pink;">=</span><span style="color: #800000;">&quot;Used(G)&quot;</span>;Expression<span style="color: pink;">=</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span>math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.Size<span style="color: pink;">/</span>1GB <span style="color: pink;">-</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: pink;">,</span>`
	<span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>Label<span style="color: pink;">=</span><span style="color: #800000;">&quot;Freespace(%)&quot;</span>;Expression<span style="color: pink;">=</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #000000;">&#40;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.Size<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: pink;">,</span>`
	<span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>Label<span style="color: pink;">=</span><span style="color: #800000;">&quot;Usedspace(%)&quot;</span>;Expression<span style="color: pink;">=</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span> <span style="color: pink;">-</span> <span style="color: #000000;">&#40;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #000000;">&#40;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.Size<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Now the issue with how I used to have it written is that it forces Format-Table and a specific style.  So essentially, I am forcing the output to look a certain way.  There&#8217;s no way for the user to dynamically change the way the output appears and will mess up the formatting if you wanted to place the data into an Excel sheet for example.</p>
<p>Now let&#8217;s take a look at the way I have it in the final script I posted.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p375code18'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p37518"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p375code18"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>DiskInformation
<span style="color: #000000;">&#123;</span>
	<span style="color: #800000;">&quot;&quot;</span>
	<span style="color: #800000;">&quot;Server: $server&quot;</span>
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$server</span>.isEdgeServer<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
                <span style="color: #800080;">$erroractionpreference</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;SilentlyContinue&quot;</span>
		<span style="color: #800080;">$cred</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Credential</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$cred</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$disk</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> Win32_LogicalDisk <span style="color: #008080; font-style: italic;">-namespace</span> root\cimv2 –<span style="color: #0000FF;">filter</span> <span style="color: #800000;">&quot;DriveType = 3&quot;</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$server</span> <span style="color: #008080; font-style: italic;">-Credential</span> <span style="color: #800080;">$cred</span> <span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-weight: bold;">Write-Warning</span> <span style="color: #800000;">&quot;Could not obtain disk information for $server due to no credentials being provided&quot;</span> ; <span style="color: #0000FF;">break</span> <span style="color: #000000;">&#125;</span>
                <span style="color: #800080;">$erroractionpreference</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Continue&quot;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;">$disk</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> Win32_LogicalDisk <span style="color: #008080; font-style: italic;">-namespace</span> root\cimv2 –<span style="color: #0000FF;">filter</span> <span style="color: #800000;">&quot;DriveType = 3&quot;</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$server</span> <span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$disk</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$obj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> PSObject
		<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> NoteProperty <span style="color: #800000;">&quot;Size(G)&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.Size<span style="color: pink;">/</span>1GB<span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> NoteProperty <span style="color: #800000;">&quot;Freespace(G)&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> NoteProperty <span style="color: #800000;">&quot;Used(G)&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.Size<span style="color: pink;">/</span>1GB <span style="color: pink;">-</span> <span style="color: #800080;">$d</span>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> NoteProperty <span style="color: #800000;">&quot;Freespace(%)&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.Size<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$obj</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Add-Member</span> NoteProperty <span style="color: #800000;">&quot;Usedspace(%)&quot;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span> <span style="color: pink;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.FreeSpace<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span> <span style="color: pink;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$d</span>.Size<span style="color: pink;">/</span>1GB<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$obj</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, it looks quite different.  What I did here was create a PowerShell object.  This allows me to add data to this object which in return allows me to pipe our command into many different outputs such as Format-Table, Format-List (which will be default since there are &gt;=4 lines of data being returned), Excel, etc..  I&#8217;ve also added other error checking such as if you don&#8217;t enter a user/password for your Edge Server, it won&#8217;t throw a bunch of errors on the screen.  Instead, it will provide a nice warning message letting you know credentials were not provided.</p>
<p>Let&#8217;s take a look at an example at pulling only disk information for a Mailbox (and yes this is a testlab and Exchange is running on my DC which I would never do in production!).</p>
<p><img class="alignnone" src="http://www.shudnow.net/images/ExchangeFirstScript01.jpg" alt="" /></p>
<p>So what happens if we didn&#8217;t specify the -role command or specified All for the role command?  The same thing will happen since -role switch is automatically assumed and the default for the command is All.</p>
<p><img class="alignnone" src="http://www.shudnow.net/images/ExchangeFirstScript02.jpg" alt="" /></p>
<p>Since we selected All, it found an Edge Server in the environment but the script runs a ping test on all servers before trying to obtain information.  Since our Edge Server was down, we were notified of this.  Now after bringing my Edge Server online, let&#8217;s run a -role All again.</p>
<p><img class="alignnone" src="http://www.shudnow.net/images/ExchangeFirstScript03.jpg" alt="" /></p>
<p>We still pulled information for our domain-joined Exchange Server, but now that our Edge server is up and we can ping it, it prompts us for authentication.  On the screen, the server that it is prompting for is OCS-EXCEdge which is the name of our Edge Server.  So let&#8217;s enter our credentials for this OCS-EXCEdge server.</p>
<p><img class="alignnone" src="http://www.shudnow.net/images/ExchangeFirstScript04.jpg" alt="" /></p>
<p>We now authenticated with the Edge Server and obtained disk information.  If we instead hit cancel on the authentication prompt, we would get:</p>
<p><img class="alignnone" src="http://www.shudnow.net/images/ExchangeFirstScript05.jpg" alt="" /></p>
<p>And the last thing I want to show is another safe guard in case you enter a wrong server type.</p>
<p><img class="alignnone" src="http://www.shudnow.net/images/ExchangeFirstScript06.jpg" alt="" /></p>
<img src="http://www.shudnow.net/?ak_action=api_record_view&id=375&type=feed" alt="" /><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.shudnow.net/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.shudnow.net/2008/09/25/obtaining-exchange-information-including-edge-with-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
