<?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"
	>

<channel>
	<title>David Muegge's Blog</title>
	<atom:link href="http://muegge.com/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://muegge.com/blog</link>
	<description>IT Eclectia - A variety technical topics and tips.</description>
	<pubDate>Fri, 04 Mar 2011 04:58:32 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>VMWare SRM using HDS AMS 2000</title>
		<link>http://muegge.com/blog/?p=142</link>
		<comments>http://muegge.com/blog/?p=142#comments</comments>
		<pubDate>Sun, 08 Aug 2010 17:41:44 +0000</pubDate>
		<dc:creator>dmuegge</dc:creator>
		
		<category><![CDATA[AMS 2000]]></category>

		<guid isPermaLink="false">http://muegge.com/blog/?p=142</guid>
		<description><![CDATA[I have been working recently configuring VMWare SRM using Hitachi AMS 2000 arrays. This has not been the most straightforward or well documented process. I hope this post will save someone else a little time.
In this post I will focus on the configuration and the required prerequisites for the HDS SRA 2.0 for VMWare SRM. [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working recently configuring VMWare SRM using Hitachi AMS 2000 arrays. This has not been the most straightforward or well documented process. I hope this post will save someone else a little time.</p>
<p>In this post I will focus on the configuration and the required prerequisites for the HDS SRA 2.0 for VMWare SRM. During testing I used ESX 4.0 update1, vCenter 4.0 update1 and SRM 4.01.</p>
<p>When working with this solution there are some obvious pieces of documentation such as the following:</p>
<p><a href="http://www.hds.com/assets/pdf/hitachi-storage-replication-adapter-software-vmware-vcenter-site-recovery-manager-deployment-guide.pdf" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.hds.com');">Hitachi Storage Replication Adapter Software VMware vCenter Site Recovery Manager Deployment Guide</a></p>
<p><a href="http://www.vmware.com/pdf/srm_admin_4_1.pdf" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.vmware.com');">Site Recovery Manager Administration Guide</a></p>
<p>There are also some other helpful pieces of documentation which may not be as obvious, such as:</p>
<p>Hitachi AMS 2000 Family Command Control Interface (CCI) Installation, Reference, and User&#8217;s Guides. These can be found on the HDS Support portal.</p>
<p><a href="http://www.hitachidatasystems.com/assets/pdf/implementing-vmware-site-recovery-manager-with-hitachi-enterprise-storage-systems.pdf" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.hitachidatasystems.com');">Implementing VMware Site Recovery Manager with Hitachi Enterprise Storage Systems</a> Whitepaper</p>
<p><a href="https://tuf.hds.com/wiki/pub/Main/CCIProblems/How_To_Debug_CCI_Issues_1.3.DOC" onclick="javascript:pageTracker._trackPageview('/outbound/article/tuf.hds.com');">How To Debug CCI Issues 1.3</a> article on the HDS GSC website</p>
<p>Also, the sample horcm.conf file installed with the CCI has some good information in the comments.</p>
<p>Now on to the configuration.</p>
<h2>Prerequisites</h2>
<p>Before configuration of the HDS SRA is possible the following requirements must be in place:</p>
<ul>
<li>Two HDS AMS2000 arrays connected by WAN (FC or iSCSI)</li>
<li>One VMWare vCenter installation in the primary/protected site</li>
<li>One VMWare vCenter installation in the secondary/recovery site</li>
<li>One VMWare SRM installation in the primary/protected site</li>
<li>One VMWare SRM installation in the secondary/recovery site</li>
<li>TrueCopy replication in place for LUN&#8217;s with protected datastores</li>
<li>SRM Sites paired</li>
</ul>
<p></p>
<h2>Test Environment</h2>
<p>Our test environment consists of one cluster at the primary site and one at the secondary site. We have a test SharePoint environment which is stored across four VMFS datastores. See the diagram below.</p>
<p><img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_000.png" alt="Test Environment" /></p>
<p>Our goal for this test configuration will be to failover the SharePoint environment to the recovery site. We are replicating the LUN&#8217;s containing all of the SharePoint system data using TrueCopy Extended Distance. Also SRM is installed at both sites and the sites have been paired.</p>
<p>After the items above are in place we can move on to the configuring the SRA.</p>
<h2>HDS SRA 2.0 Configuration</h2>
<p>When configuring the storage replication adapter the primary documentation is the deployment guide referenced above. I thought there were a few things missing from the document.</p>
<p>The first step in getting the SRA configured is making sure you have a copy of the proper HDS CCI for your array firmware. The HDS SRA relies on the Hitachi Command Control Interface, which must be installed on the SRM servers. I installed the CCI in the default c:\HORCM directory on both SRM servers. This is a straightforward install and is documented in the Hitachi AMS 2000 Family Command Control Interface (CCI) Installation Guide.</p>
<p>The portion of the CCI install that was tough for me was determining it needed to be installed as a service and creating the horcm.conf files. Our above example will only require two instances of the horcm service. One on each SRM server they will be HORCM0 and HORCM1.</p>
<p>To create the services we create the horcm_run.txt files and issue the following commands.</p>
<p>On the first SRM server - create the c:\HORCM\Tool\horcm0_run.txt file and the execute the following command:<br />
     C:\HORCM\tool\svcexe /S=HORCM0 /A=C:\HORCM\Tool\svcexe.exe</p>
<p>On the second SRM server - create the c:\HORCM\Tool\horcm1_run.txt file and the execute the following command:<br />
    C:\HORCM\tool\svcexe /S=HORCM1 /A=C:\HORCM\Tool\svcexe.exe</p>
<p>The horcmx_run.txt is created by making a copy of the file naming it appropriately and setting the HORCMINST variable to the correct instance number. This is documented in the file located in HORCM\Tool</p>
<p>After running these commands you should see the services appear in the windows services MMC</p>
<p>Then add the following lines to the %systemroot%\drivers\etc\services file on each SRM server.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="dos dos" style="font-family:monospace;">horcm0     <span style="color: #cc66cc;">11000</span>/udp        #horcm0 CCI service
horcm1     <span style="color: #cc66cc;">11001</span>/udp        #horcm1 CCI service</pre></td></tr></table></div>

<p>The file should appear as below with one blank line below the last horcm service</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="dos dos" style="font-family:monospace;">rasadv                  <span style="color: #cc66cc;">9753</span>/udp  
horcm0                 <span style="color: #cc66cc;">11000</span>/udp         #horcm0 CCI service
horcm1                 <span style="color: #cc66cc;">11001</span>/udp         #horcm1 CCI service
&nbsp;
imip-channels          <span style="color: #cc66cc;">11320</span>/tcp         #IMIP Channels Port</pre></td></tr></table></div>

<p>Once the services are installed the next step is to create the horcm.conf files. The first thing we need to do this is a command device. The SRA deployment guide left this step out. This is documented in the VMWare SRM with Enterprise Storage whitepaper mentioned earlier. Basically you create a small LUN and present it to the SRM server as a physical compatibility RDM. Then you initialize the disk and create a basic primary partition, but do not assign a drive letter or format it. I found one HDS document that said this LUN should be 33MB and one that said 36MB so I made it 40MB. Once this is done we have all that is needed to create the horcm.conf files.</p>
<p>HORCM0.conf on the SRM server at primary site</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="dos dos" style="font-family:monospace;">&nbsp;
#/************************* <span style="color: #00b100; font-weight: bold;">For</span> HORCM_MON *************************************/
HORCM_MON
#ip_address            service      poll<span style="color: #66cc66;">&#40;</span>10ms<span style="color: #66cc66;">&#41;</span>      timeout<span style="color: #66cc66;">&#40;</span>10ms<span style="color: #66cc66;">&#41;</span>
srm1.test.local        horcm0       <span style="color: #cc66cc;">8000</span>            <span style="color: #cc66cc;">3000</span>
 
&nbsp;
#/************************** <span style="color: #00b100; font-weight: bold;">For</span> HORCM_CMD ************************************/
HORCM_CMD
#dev_name          dev_name              dev_name
\\.\CMD-<span style="color: #cc66cc;">11111111</span>-<span style="color: #cc66cc;">64</span>
&nbsp;
&nbsp;
#/************************** <span style="color: #00b100; font-weight: bold;">For</span> HORCM_LDEV ***********************************/
HORCM_LDEV
#dev_group            dev_name              Serial#       CU:LDEV<span style="color: #66cc66;">&#40;</span>LDEV#<span style="color: #66cc66;">&#41;</span>            MU#
DRLAB_SRM_TEST        DRLAB_AP_OS           <span style="color: #cc66cc;">11111111</span>      0x003C
DRLAB_SRM_TEST        DRLAB_DB_OS           <span style="color: #cc66cc;">11111111</span>      0x003D
DRLAB_SRM_TEST        DRLAB_OLTP_LOG        <span style="color: #cc66cc;">11111111</span>      0x003E
DRLAB_SRM_TEST        DRLAB_OLTP_DATA       <span style="color: #cc66cc;">11111111</span>      0x003F
 
&nbsp;
#/************************* <span style="color: #00b100; font-weight: bold;">For</span> HORCM_INST ************************************/
HORCM_INST
#dev_group           ip_address            service
DRLAB_SRM_TEST       srm2.test.local       horcm1 </pre></td></tr></table></div>

<p>HORCM1.conf on the SRM server at secondary site</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="dos dos" style="font-family:monospace;">#/************************* <span style="color: #00b100; font-weight: bold;">For</span> HORCM_MON *************************************/
HORCM_MON
#ip_address            service      poll<span style="color: #66cc66;">&#40;</span>10ms<span style="color: #66cc66;">&#41;</span>      timeout<span style="color: #66cc66;">&#40;</span>10ms<span style="color: #66cc66;">&#41;</span>
srm2.test.local        horcm1       <span style="color: #cc66cc;">8000</span>            <span style="color: #cc66cc;">3000</span>
&nbsp;
&nbsp;
#/************************** <span style="color: #00b100; font-weight: bold;">For</span> HORCM_CMD ************************************/
HORCM_CMD
#dev_name          dev_name           dev_name
\\.\CMD-<span style="color: #cc66cc;">11111112</span>-<span style="color: #cc66cc;">4</span>
&nbsp;
&nbsp;
#/************************** <span style="color: #00b100; font-weight: bold;">For</span> HORCM_LDEV ***********************************/
HORCM_LDEV
#dev_group                   dev_name             Serial#       CU:LDEV<span style="color: #66cc66;">&#40;</span>LDEV#<span style="color: #66cc66;">&#41;</span>       MU#
DRLAB_SRM_TEST               DRLAB_AP_OS          <span style="color: #cc66cc;">11111112</span>      0x0001
DRLAB_SRM_TEST               DRLAB_DB_OS          <span style="color: #cc66cc;">11111112</span>      0x0005
DRLAB_SRM_TEST               DRLAB_OLTP_LOG       <span style="color: #cc66cc;">11111112</span>      0x0006
DRLAB_SRM_TEST               DRLAB_OLTP_DATA      <span style="color: #cc66cc;">11111112</span>      0x0007
 
&nbsp;
#/************************* <span style="color: #00b100; font-weight: bold;">For</span> HORCM_INST ************************************/
HORCM_INST
#dev_group            ip_address        service
DRLAB_SRM_TEST        srm1.test.local   horcm0
 </pre></td></tr></table></div>

<p>A couple of points to note on these files is the relationship between the hosts and devices in the group. The HORCM_LDEV section on each instance contains a reference to the half of the pair it controls. The HORCM_INST section contains a reference to the opposite instance in each file. Also the command device naming format. It consists of &#8220;\\.\CMD-&#8221; followed by the array serial number and the LUN number &#8220;\\.\CMD-11111112-4&#8243;. Now that we have the configuration files we copy them into the %windir% on their respective SRM servers and start the services.</p>
<p>After this is complete we can install the SRA. This is downloaded from the VMWare website and the executable is named RMHTCSRA.exe. It is a simple, no option, install. After this there are some environment variable which need to be set.</p>
<p>setx SplitReplication true /m<br />
setx RMSRATMU 1 /m</p>
<p>Then reboot the SRM servers. We are now ready to configure the SRA using the SRM plug-in in vCenter.</p>
<p>Here we see the paired sites in site recovery.</p>
<p> <img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_001.png" alt="SRM Sites" /></p>
<p>Click on configure array managers and we see the following dialog.</p>
<p> <img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_002.png" alt="SRA Config 1" /></p>
<p>Click Add to add a protected site array manager and we see the following configuration dialog.</p>
<p> <img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_003.png" alt="SRM Config 2" /></p>
<p>We enter the name and HORCMINST=0 for the first instance on the primary server in the protected site. The we use a different name and HORCMINST=1 for the next instance on the secondary server in the recovery site. Here we see both sides configured.</p>
<p> <img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_005.png" alt="SRA Final Config" /><img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_006.png" alt="SRA Final Config 2" /></p>
<p>The last step in the wizard allows us to confirm the SRA sees the replicated datastores properly.</p>
<p> <img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_007.png" alt="Replicated Datastores" /></p>
<p>We see the LUN numbers match the devices in the horcm&lt;x&gt;.conf files. The datastore group in this diagram consists of four LUN&#8217;s which also belong to the same TCE consistency group. These are the LUN&#8217;s being used by our test SharePoint application and database servers.</p>
<p>At this point we are now ready to complete configuration of the protection groups and recovery plans in Site Recovery Manager. The process for configuring these is documented in the SRM administration guide. Protection groups are configured at the protected site and recovery plans are configured at the recovery site. Here is a screenshot of the test recovery plan for our SharePoint environment.</p>
<p><img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_008.png" alt="Recovery Plan 1" /> </p>
<p>When we run a test on this recovery plan we can see the test runs successfully and waits for us to complete testing before clicking continue to return to a ready state.</p>
<p><img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_009.png" alt="Recovery Plan 2" /> </p>
<p>During this phase we can look at a couple of things to confirm what is happening in the process. One is the new datastores we will see in the configuration tab of the DR ESX hosts.</p>
<p><img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_010.png" alt="Datastore Snapshots/Replicas" /> </p>
<p>There was no need to change the LVM.EnableResignature or LVM.DisallowSnapshotLun settings at the host level in ESX 4 as this is enabled at the volume level and SRM handles this at the time of testing or failover. Another part of the process we can confirm at this time is the status of the TrueCopy pairs. Here we see the pairs are in split status.</p>
<p><img src="http://muegge.com/images/blogimages/0012_SRM_Screenshot_011.png" alt="TrueCopy Split Status" /></p>
<p>Now we can complete any other testing to confirm success of the test and then click continue in the recovery plan to return to a ready state. After the test completes we can see the datastores are removed from the recovery ESX hosts and the TCE pairs are returned to a paired status after resynchronization.</p>
<p>After the testing process is completed we can review some of the steps in the SRM logs. The logs are located under %allusersprofile%\ VMware\VMware vCenter Site Recovery Manager\Logs. These log entries and the HORCM logs under c:\HORCM\log are the primary sources of information in troubleshooting problems with this process.</p>
<p>I hope someone finds this post useful. Next I am going to be testing this with secondary copies at the recovery site using Shadowimage and Copy-on-Write.<br />
 </p>
<p>Regards,</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://muegge.com/blog/?feed=rss2&amp;p=142</wfw:commentRss>
		</item>
		<item>
		<title>HDS AMS 2000 Storage Resource Reporting with PowerShell</title>
		<link>http://muegge.com/blog/?p=128</link>
		<comments>http://muegge.com/blog/?p=128#comments</comments>
		<pubDate>Sun, 29 Nov 2009 22:16:57 +0000</pubDate>
		<dc:creator>dmuegge</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://muegge.com/blog/?p=128</guid>
		<description><![CDATA[Welcome!
I have been creating a few PowerShell scripts for use with the HDS AMS 2000 array. One thing I found I needed was a quick way to look at DP(Dynamic Provisioning) pools, raid groups, and LUNs. I also wanted to be able to see the associations and filter easily. Since I am working on a [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome!</p>
<p>I have been creating a few PowerShell scripts for use with the HDS AMS 2000 array. One thing I found I needed was a quick way to look at DP(Dynamic Provisioning) pools, raid groups, and LUNs. I also wanted to be able to see the associations and filter easily. Since I am working on a new deployment I have been creating Raid Groups and Luns often. I needed a quick way to see what I currently had while creating new resources.</p>
<p>I created a PowerShell script that would quickly show existing resources by raid group or DP pool. It also uses nickname info for the devices that are maintained in three csv files(LU_Nicknames.csv,RG_Nicknames.csv,DP_Nicknames.csv). These are simple comma delimited text files which contain the ID and nickname of each resource. The files are updated as storage resources are added. This allows me to easily identify the resources and to filter for specific devices.</p>
<p>The script executes three HSNM2 CLI commands and reads the information into object form. The LUN information is then shown grouped by raid group or DP pool.</p>
<p>Here is the output with the nickname search parameter set to &#8220;DB&#8221;. This will return all database resources based on the naming standard. If this is left null it will return all resources.</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="text text" style="font-family:monospace;">PS D:\DTools\Scripts\PS\HDS-SNM2&gt; D:\DTools\Scripts\PS\HDS-SNM2\get-LunsByDPRG.ps1
Nickname Search: DB
&nbsp;
-- DP Pool  Number: 0  Nickname: DB_SQL_OLTP  Raid Level: 5( 5D+1P)  Type: SAS  Capacity: 1986.0 GB  Used: 734.0 GB ----------------------------------------------------------------
&nbsp;
&nbsp;
lu                                      nickname                                capacity                                stripesize                              status
--                                      --------                                --------                                ----------                              ------
3                                       DB01_OLTP_DATA                          300.0                                   256                                     Normal
20                                      DB01_OLTP_DATA                          300.0                                   256                                     Normal
22                                      DB01_OLTP_DATA                          300.0                                   256                                     Normal
29                                      DB01_DSS_DATA_TEMP                      600.0                                   256                                     Normal
&nbsp;
&nbsp;
&nbsp;
-- RAID GROUP  Number: 4  Nickname: DB_LOG  Raid Level: 1+0( 2D+2D)  Type: SAS  Capacity: 535.7 GB  Free: 0.0 GB -------------------------------------------------------------------
&nbsp;
&nbsp;
lu                                      nickname                                capacity                                stripesize                              status
--                                      --------                                --------                                ----------                              ------
5                                       DB01_OLTP_LOG                           267.0                                   64                                      Normal
21                                      DB01_OLTP_LOG                           268.7                                   64                                      Normal
&nbsp;
&nbsp;
&nbsp;
-- RAID GROUP  Number: 7  Nickname: DB_OS  Raid Level: 5( 3D+1P)  Type: SAS  Capacity: 398.4 GB  Free: 183.4 GB --------------------------------------------------------------------
&nbsp;
&nbsp;
lu                                      nickname                                capacity                                stripesize                              status
--                                      --------                                --------                                ----------                              ------
9                                       DB01_OS_DATA                            215.0                                   256                                     Normal
&nbsp;
&nbsp;
&nbsp;
-- RAID GROUP  Number: 8  Nickname: DB_LOG  Raid Level: 1+0( 2D+2D)  Type: SAS  Capacity: 535.7 GB  Free: 535.7 GB -----------------------------------------------------------------</pre></td></tr></table></div>

<p>Here is the script:</p>

<div class="wp_syntax"><table><tr><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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># -- Setup login info and SNM2 environment ------------------</span>
. .<span style="color: pink;">/</span>start<span style="color: pink;">-</span>session.ps1
<span style="color: #008080; font-weight: bold;">Set-Location</span> <span style="color: #800080;">$env</span>:STONAVM_HOME
&nbsp;
<span style="color: #008000;"># -- Import Nickname tables ---------------------------------------</span>
<span style="color: #800080;">$LUNicknames</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #800080;">$SCRIPTHOME</span>\LU_Nicknames.csv
<span style="color: #800080;">$RGNicknames</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #800080;">$SCRIPTHOME</span>\RG_Nicknames.csv
<span style="color: #800080;">$DPNicknames</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #800080;">$SCRIPTHOME</span>\DP_Nicknames.csv
&nbsp;
<span style="color: #008000;"># -- Get storage resource info -----------------------------------</span>
<span style="color: #800080;">$luresults</span> <span style="color: pink;">=</span> <span style="color: pink;">&amp;</span> .<span style="color: pink;">/</span>auluref.exe <span style="color: pink;">-</span>unit <span style="color: #800080;">$DEFAULTARRAY</span> <span style="color: pink;">-</span>g <span style="color: pink;">-</span>nosublu <span style="color: pink;">-</span>totalsize
<span style="color: #800080;">$rgresults</span> <span style="color: pink;">=</span> <span style="color: pink;">&amp;</span> .<span style="color: pink;">/</span>aurgref.exe <span style="color: pink;">-</span>unit <span style="color: #800080;">$DEFAULTARRAY</span> <span style="color: pink;">-</span>g
<span style="color: #800080;">$dpresults</span> <span style="color: pink;">=</span> <span style="color: pink;">&amp;</span> .<span style="color: pink;">/</span>audppool.exe <span style="color: pink;">-</span>unit <span style="color: #800080;">$DEFAULTARRAY</span> <span style="color: pink;">-</span>refer <span style="color: pink;">-</span>g
&nbsp;
<span style="color: #008000;"># -- Setup object collections -----------------------------------</span>
<span style="color: #800080;">$allrgdata</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$allludata</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$alldpdata</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;"># -- Get LUN data -----------------------------------------------------------</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$luresult</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$luresults</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$luresult</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">'LU\s+Capacity\s+Size\s+Group\s+Pool\s+Level\s+Type\s+Status'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #800080;">$LUDataRead</span> <span style="color: pink;">=</span> <span style="color: #800080;">$TRUE</span>
&nbsp;
	<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">elseif</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$LUDataRead</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$luresult</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">'(\d+)\s+(\d+\.\d+)\sGB\s+(\d+)KB\s+(\d+|N/A)\s+(\d+|N/A)\s+(([5,6]|1\+0)\(\s+\d+D\+\d+[D,P]\))\s+(SAS|SATA)\s+(.+)'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$ludata</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span> | <span style="color: #008080; font-weight: bold;">Select</span> lu<span style="color: pink;">,</span>nickname<span style="color: pink;">,</span>capacity<span style="color: pink;">,</span>stripesize<span style="color: pink;">,</span>raidgroup<span style="color: pink;">,</span>dppool<span style="color: pink;">,</span>raidlevel<span style="color: pink;">,</span><span style="color: #008080; font-weight: bold;">type</span><span style="color: pink;">,</span>status
			<span style="color: #800080;">$ludata</span>.lu <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$Nickname</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LUNicknames</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.LU <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$ludata</span>.lu<span style="color: #000000;">&#125;</span> | <span style="color: #008080; font-weight: bold;">select</span> nickname
			<span style="color: #800080;">$ludata</span>.nickname <span style="color: pink;">=</span> <span style="color: #800080;">$Nickname</span>.nickname
			<span style="color: #800080;">$ludata</span>.capacity <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">2</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$ludata</span>.stripesize <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">3</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$ludata</span>.raidgroup <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">4</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$ludata</span>.dppool <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">5</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$ludata</span>.raidlevel <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">6</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$ludata</span>.<span style="color: #008080; font-weight: bold;">type</span> <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">8</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$ludata</span>.status <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">9</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$allludata</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$ludata</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># --  Get Raid Group data --------------------------------------------------------</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$rgresult</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$rgresults</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$rgresult</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">'Group\s+Level\s+Groups\s+Type\s+Total\sCapacity\s+Free\sCapacity\s+Priority\s+Status'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #800080;">$RGDataRead</span> <span style="color: pink;">=</span> <span style="color: #800080;">$TRUE</span>
&nbsp;
	<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">elseif</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$RGDataRead</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$rgresult</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">'(\d+)\s+(([5,6]|1\+0)\(\s+\d+D\+\d+[D,P]\))\s+(\d+)\s+(SAS|SATA)\s+(\d+\.\d+)\sGB\s+(\d+\.\d+)\sGB\s\(\s*\d+\.{1}\d+%{1}\)\s+(.+\s.+)\s+(.+)'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #800080;">$rgdata</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span> | <span style="color: #008080; font-weight: bold;">Select</span> rg<span style="color: pink;">,</span>nickname<span style="color: pink;">,</span>raidlevel<span style="color: pink;">,</span>raidconfig<span style="color: pink;">,</span>paritygroups<span style="color: pink;">,</span><span style="color: #008080; font-weight: bold;">type</span><span style="color: pink;">,</span>totalcapacity<span style="color: pink;">,</span>freecapacity<span style="color: pink;">,</span>priority<span style="color: pink;">,</span>status
			<span style="color: #800080;">$rgdata</span>.rg <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$Nickname</span> <span style="color: pink;">=</span> <span style="color: #800080;">$RGNicknames</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.RG <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$rgdata</span>.rg<span style="color: #000000;">&#125;</span> | <span style="color: #008080; font-weight: bold;">select</span> nickname
			<span style="color: #800080;">$rgdata</span>.nickname <span style="color: pink;">=</span> <span style="color: #800080;">$Nickname</span>.nickname
			<span style="color: #800080;">$rgdata</span>.raidlevel <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">2</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$rgdata</span>.raidconfig <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">3</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$rgdata</span>.paritygroups <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">4</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$rgdata</span>.<span style="color: #008080; font-weight: bold;">type</span> <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">5</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$rgdata</span>.totalcapacity <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">6</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$rgdata</span>.freecapacity <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">7</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$rgdata</span>.priority <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">8</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$rgdata</span>.status <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">9</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$allrgdata</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$rgdata</span>
		<span style="color: #000000;">&#125;</span>	
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># --  Get DP pool data -------------------------------------------------------------</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$dpresult</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$dpresults</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$dpresult</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">'Pool\s+Level\s+Total\sCapacity\s+Consumed\sCapacity\s+Type\s+Status'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #800080;">$DPDataRead</span> <span style="color: pink;">=</span> <span style="color: #800080;">$TRUE</span>
&nbsp;
	<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">elseif</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$DPDataRead</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$dpresult</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">'(\d+)\s+(([5,6]|1\+0)\(\s+\d+D\+\d+[D,P]\))\s+(\d+\.\d+)\sGB\s+(\d+\.\d+)\sGB\s+(SAS|SATA)\s+(.+)'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> 
&nbsp;
			<span style="color: #800080;">$dpdata</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span> | <span style="color: #008080; font-weight: bold;">Select</span> dp<span style="color: pink;">,</span>nickname<span style="color: pink;">,</span>raidlevel<span style="color: pink;">,</span>totalcapacity<span style="color: pink;">,</span>capacityused<span style="color: pink;">,</span><span style="color: #008080; font-weight: bold;">type</span><span style="color: pink;">,</span>status
			<span style="color: #800080;">$dpdata</span>.dp <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$Nickname</span> <span style="color: pink;">=</span> <span style="color: #800080;">$DPNicknames</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.DP <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$dpdata</span>.dp<span style="color: #000000;">&#125;</span> | <span style="color: #008080; font-weight: bold;">select</span> nickname
			<span style="color: #800080;">$dpdata</span>.nickname <span style="color: pink;">=</span> <span style="color: #800080;">$Nickname</span>.nickname
			<span style="color: #800080;">$dpdata</span>.raidlevel <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">2</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$dpdata</span>.totalcapacity <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">4</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$dpdata</span>.capacityused <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">5</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$dpdata</span>.<span style="color: #008080; font-weight: bold;">type</span> <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">6</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$dpdata</span>.status <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">7</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$alldpdata</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$dpdata</span>
		<span style="color: #000000;">&#125;</span>	
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># -- Get search string -----------------------------</span>
<span style="color: #800080;">$searchstring</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Read-Host</span> <span style="color: #800000;">&quot;Nickname Search&quot;</span>
&nbsp;
<span style="color: #008000;"># -- Display LUN's by DP pool --------------------------------------------------------------</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$dppool</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$alldpdata</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #800080;">$dpnum</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dppool</span>.dp
	<span style="color: #800080;">$dpcapacity</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dppool</span>.totalcapacity
	<span style="color: #800080;">$dpused</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dppool</span>.capacityused
	<span style="color: #800080;">$dpnickname</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dppool</span>.nickname
	<span style="color: #800080;">$dplevel</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dppool</span>.raidlevel
	<span style="color: #800080;">$dptype</span> <span style="color: pink;">=</span> <span style="color: #800080;">$dppool</span>.<span style="color: #008080; font-weight: bold;">type</span>
&nbsp;
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$dpnickname</span> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*$searchstring*&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$message</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;-- DP Pool  Number: $dpnum  Nickname: $dpnickname  Raid Level: $dplevel  Type: $dptype  Capacity: $dpcapacity GB  Used: $dpused GB -&quot;</span>
		<span style="color: #800080;">$charadd</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">180</span> <span style="color: pink;">-</span> <span style="color: #800080;">$message</span>.length<span style="color: #000000;">&#41;</span>
		<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span><span style="color: pink;">=</span><span style="color: #000000;">1</span>;<span style="color: #800080;">$x</span> <span style="color: #FF0000;">-le</span> <span style="color: #800080;">$charadd</span>;<span style="color: #800080;">$x</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$message</span> <span style="color: pink;">+=</span> <span style="color: #800000;">&quot;-&quot;</span><span style="color: #000000;">&#125;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;&quot;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$message</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;&quot;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$searchstring</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$allludata</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.nickname <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*$searchstring*&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.dppool <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$dpnum</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span> | <span style="color: #008080; font-weight: bold;">FT</span> <span style="color: #008080; font-style: italic;">-property</span> lu<span style="color: pink;">,</span>nickname<span style="color: pink;">,</span>capacity<span style="color: pink;">,</span>stripesize<span style="color: pink;">,</span>status
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$allludata</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.dppool <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$dpnum</span><span style="color: #000000;">&#125;</span> | <span style="color: #008080; font-weight: bold;">FT</span> <span style="color: #008080; font-style: italic;">-property</span> lu<span style="color: pink;">,</span>nickname<span style="color: pink;">,</span>capacity<span style="color: pink;">,</span>stripesize<span style="color: pink;">,</span>status
	<span style="color: #000000;">&#125;</span>	
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># -- Display LUN&quot;s by raid group -------------------------------------------------</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$rgroup</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$allrgdata</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #800080;">$rgnum</span> <span style="color: pink;">=</span> <span style="color: #800080;">$rgroup</span>.rg
	<span style="color: #800080;">$rgcapacity</span> <span style="color: pink;">=</span> <span style="color: #800080;">$rgroup</span>.totalcapacity
	<span style="color: #800080;">$rgfree</span> <span style="color: pink;">=</span> <span style="color: #800080;">$rgroup</span>.freecapacity
	<span style="color: #800080;">$rgnickname</span> <span style="color: pink;">=</span> <span style="color: #800080;">$rgroup</span>.nickname
	<span style="color: #800080;">$rglevel</span> <span style="color: pink;">=</span> <span style="color: #800080;">$rgroup</span>.raidlevel
	<span style="color: #800080;">$rgtype</span> <span style="color: pink;">=</span> <span style="color: #800080;">$rgroup</span>.<span style="color: #008080; font-weight: bold;">type</span>
&nbsp;
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$rgnickname</span> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*$searchstring*&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$message</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;-- RAID GROUP  Number: $rgnum  Nickname: $rgnickname  Raid Level: $rglevel  Type: $rgtype  Capacity: $rgcapacity GB  Free: $rgfree GB -&quot;</span>
		<span style="color: #800080;">$charadd</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">180</span> <span style="color: pink;">-</span> <span style="color: #800080;">$message</span>.length<span style="color: #000000;">&#41;</span>
		<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span><span style="color: pink;">=</span><span style="color: #000000;">1</span>;<span style="color: #800080;">$x</span> <span style="color: #FF0000;">-le</span> <span style="color: #800080;">$charadd</span>;<span style="color: #800080;">$x</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$message</span> <span style="color: pink;">+=</span> <span style="color: #800000;">&quot;-&quot;</span><span style="color: #000000;">&#125;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;&quot;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$message</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;&quot;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$searchstring</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$allludata</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.nickname <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*$searchstring*&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.raidgroup <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$rgnum</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span> | <span style="color: #008080; font-weight: bold;">FT</span> <span style="color: #008080; font-style: italic;">-property</span> lu<span style="color: pink;">,</span>nickname<span style="color: pink;">,</span>capacity<span style="color: pink;">,</span>stripesize<span style="color: pink;">,</span>status
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$allludata</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.raidgroup <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$rgnum</span><span style="color: #000000;">&#125;</span> | <span style="color: #008080; font-weight: bold;">FT</span> <span style="color: #008080; font-style: italic;">-property</span> lu<span style="color: pink;">,</span>nickname<span style="color: pink;">,</span>capacity<span style="color: pink;">,</span>stripesize<span style="color: pink;">,</span>status
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># -- CD back to script directory and set window title ---------------------</span>
<span style="color: #008080; font-weight: bold;">Set-Location</span> <span style="color: #800080;">$SCRIPTHOME</span>
<span style="color: #800080;"><span style="color: #000080;">$Host</span></span>.UI.RawUI.WindowTitle <span style="color: pink;">=</span> <span style="color: #800000;">&quot;DP-RG-LU Info&quot;</span></pre></td></tr></table></div>

<p>The script uses the start-session.ps1 file to establish connectivity with the HDS array. Additional information regarding the use of this include file can be found at <a href="http://muegge.com/blog/?p=114" onclick="">this</a> post. Then the script executes HSNM2 CLI commands to return information on DP pools raid groups and LUNS. The script uses regular expressions to parse the output and convert it into objects. It also reads in the nickname files and adds the data to the custom objects. </p>
<p>The objects are then output using the built-in PowerShell formating engine with a little custom formating thrown in for the group headers. </p>
<p>Here is an example nickname file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="text text" style="font-family:monospace;">RG,Nickname
0,MG_OS
1,AP_OS
2,DT_OS
3,DB_SQL_OLTP
7,DB_OS
8,DB_LOG</pre></td></tr></table></div>

<p>I suppose this may not be necessary with the use of Device Manager, but I am still learning it and I could not quite get this view with it. Besides I am more of a scripting kind of guy. I also really like the output of this script as it gives me just the view of the array I need when I am allocating new storage and setting up new resources. I use this script in conjuction with two other scripts for creating LUN&#8217;s and raid groups. I plan to post those scripts soon.</p>
<p>Hope this helps,</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://muegge.com/blog/?feed=rss2&amp;p=128</wfw:commentRss>
		</item>
		<item>
		<title>HDS AMS 2000 Performance Analysis with PowerShell and PowerGadgets</title>
		<link>http://muegge.com/blog/?p=114</link>
		<comments>http://muegge.com/blog/?p=114#comments</comments>
		<pubDate>Sat, 24 Oct 2009 01:53:49 +0000</pubDate>
		<dc:creator>dmuegge</dc:creator>
		
		<category><![CDATA[AMS 2000]]></category>

		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://muegge.com/blog/?p=114</guid>
		<description><![CDATA[Welcome!
It has been a while since my last post due to a very busy schedule with a SAN and virtualization project.
I have been working on an implementation of a HDS AMS 2500 midrange array for a VMWare vShere 4 environment. So far everything has been working and performing well. The management software included with the [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome!</p>
<p>It has been a while since my last post due to a very busy schedule with a SAN and virtualization project.</p>
<p>I have been working on an implementation of a HDS AMS 2500 midrange array for a VMWare vShere 4 environment. So far everything has been working and performing well. The management software included with the <a href="http://www.hds.com/products/storage-systems/adaptable-modular-storage-2000-family/" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.hds.com');">HDS AMS 2000</a> series array is SNM2(Storage Navigator Modular 2), A java based web application. This software also has a command line version which appears to be pretty comprehensive. It consists of a series of DOS executables, which can be run from PowerShell. There are a series of scripts I have been working on for viewing and creating storage resources on the array. I will share many of these in future posts. In this post I want to share some scripts I have written to extend the functionality of the performance monitoring utility in SNM2.</p>
<p>The base functionality of the array allows you to capture performance statistics to a text file. The file can be captured manually or automatically for a specified time period and interval down to one minute. One text file is produced per capture or all captures can be written to one file. Also, I believe based on the information I read in the SNM2 manual you can do some graphing with the web interface, but it requires an additional license and personally I think the <a href="http://www.powergadgets.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.powergadgets.com');">PowerGadgets</a> graphs are better.</p>
<p>The 4 scripts I have started with are get-performance_processor.ps1, get-performance_ports.ps1, get-performance_raidgroups.ps1, get-performance_luns.ps1, which do pretty much what they say and produce the following PowerGadgets charts.</p>
<p><img alt="Chart" src="http://muegge.com/images/blogimages/0010-screenshot01.jpg" title="Chart" width="849" height="667" /></p>
<p>The chart group is a tabbed interface which allows you to tab through the controllers and ports/RG/LU/Procs depending on the script being used. Each script generates different groups of charts for different performance counters. I have not implemented all of the performance counters just the ones which are most important to me now. I will be improving these scripts over time and implementing more counters. Here is an example of how the script works.</p>
<p><img alt="Script" src="http://muegge.com/images/blogimages/0010-screenshot02.jpg" title="Script" width="753" height="373" /></p>
<p>After executing the script it will ask whether or not to collect data, if yes it will prompt for interval in minutes and time period. If no it will use previously collected data in the default output directory. Next it will ask to list data in text output. Then it will prompt for generation of each group of charts for ports, raid groups, luns or processors depending on the script run.</p>
<p>Now to the script. All of the scripts rely on the start-session.ps1 script and also require a password file be set for logging into the array. Additionally, an array has to be registered.</p>
<p>Example 1 shows a PowerShell script which will register an array and set the admin password.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #800080;">$env</span>:STONAVM_HOME<span style="color: pink;">=</span><span style="color: #800000;">&quot;C:\Program Files\Storage Navigator Modular 2 CLI&quot;</span>
<span style="color: #800080;">$env</span>:STONAVM_ACT<span style="color: pink;">=</span><span style="color: #800000;">&quot;on&quot;</span>
<span style="color: #800080;">$env</span>:STONAVM_RSP_PASS<span style="color: pink;">=</span><span style="color: #800000;">&quot;on&quot;</span>
<span style="color: #800080;">$env</span>:LANG<span style="color: pink;">=</span><span style="color: #800000;">&quot;en&quot;</span>
<span style="color: #008080; font-weight: bold;">cd</span> <span style="color: #800000;">&quot;C:\Program Files\Storage Navigator Modular 2 CLI&quot;</span>
.<span style="color: pink;">/</span>auunitadd <span style="color: pink;">-</span>unit ARRAYNAME <span style="color: pink;">-</span>ctl0 192.168.1.1 <span style="color: pink;">-</span>ctl1 192.168.1.2
.<span style="color: pink;">/</span>auaccountenv <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">set</span> <span style="color: pink;">-</span>uid USERNAME</pre></td></tr></table></div>

<p>You will need to replace ARRAYNAME, USERNAME and the IP Addresses for your environment.</p>
<p> Example 2 shows the start-session PowerShell script which defines environmental information.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># Comand Enviroment</span>
<span style="color: #800080;">$env</span>:STONAVM_HOME<span style="color: pink;">=</span><span style="color: #800000;">&quot;C:\Program Files\Storage Navigator Modular 2 CLI&quot;</span>
<span style="color: #800080;">$env</span>:STONAVM_ACT<span style="color: pink;">=</span><span style="color: #800000;">&quot;on&quot;</span>
<span style="color: #800080;">$env</span>:STONAVM_RSP_PASS<span style="color: pink;">=</span><span style="color: #800000;">&quot;on&quot;</span>
<span style="color: #800080;">$env</span>:LANG<span style="color: pink;">=</span><span style="color: #800000;">&quot;en&quot;</span>
&nbsp;
<span style="color: #008000;"># Global</span>
<span style="color: #800080;">$SCRIPTHOME</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Scripts\HDS-SNM2&quot;</span>
<span style="color: #800080;">$DEFAULTARRAY</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;ARRAYNAME&quot;</span></pre></td></tr></table></div>

<p>You will need to change the paths and ARRAYNAME for your environment.</p>
<p>Example 3 shows the get-performance_processor.ps1 script</p>

<div class="wp_syntax"><table><tr><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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;">&nbsp;
<span style="color: #008000;"># -- User Defined Constants ---------------------------</span>
<span style="color: #800080;">$LOG_PATH</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Scripts\HDS-SNM2\pfmlog&quot;</span>
&nbsp;
<span style="color: #008000;"># -- Modify controller and core options as needed -----</span>
<span style="color: #800080;">$Controllers</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span>0<span style="color: pink;">,</span><span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$cores</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;X&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;Y&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;"># -- Modifications below this line should not be required ---------------------------------------</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #008000;"># -- Setup environment ------------------------------</span>
<span style="color: #800080;">$testsnapin</span> <span style="color: pink;">=</span> <span style="color: #800080;">$null</span>
<span style="color: #800080;">$testsnapin</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">get-pssnapin</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;"><span style="color: #000080;">$_</span></span>.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;PowerGadgets&quot;</span><span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">-not</span> <span style="color: #800080;">$testsnapin</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">add-pssnapin</span> <span style="color: #008080; font-style: italic;">-Name</span> PowerGadgets<span style="color: #000000;">&#125;</span>
. .<span style="color: pink;">/</span>start<span style="color: pink;">-</span>session.ps1
<span style="color: #008080; font-weight: bold;">Set-Location</span> <span style="color: #800080;">$env</span>:STONAVM_HOME
&nbsp;
&nbsp;
<span style="color: #008000;"># --  Object to hold processor usage data ------------------------</span>
<span style="color: #800080;">$allprocusagedata</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;"># -- Processing flags -------------------------------------</span>
<span style="color: #800080;">$PROCUsageDataRead</span> <span style="color: pink;">=</span> <span style="color: #800080;">$FALSE</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># -- Specify whether to run new collection if not existing files are used --</span>
<span style="color: #008080; font-weight: bold;">Write-Host</span> ”Collect Data Y or N...”
<span style="color: #800080;">$modekey</span> <span style="color: pink;">=</span> <span style="color: #800080;"><span style="color: #000080;">$Host</span></span>.UI.RawUI.ReadKey<span style="color: #000000;">&#40;</span>”NoEcho<span style="color: pink;">,</span>IncludeKeyDown”<span style="color: #000000;">&#41;</span>
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$modekey</span>.Character.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.ToLower<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;y&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$collectiontime</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Read-Host</span> <span style="color: #800000;">&quot;Collection Time/Count in minutes&quot;</span>
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Collecting Data...&quot;</span>
	.<span style="color: pink;">/</span>auperform.exe <span style="color: pink;">-</span>unit <span style="color: #800080;">$DEFAULTARRAY</span> <span style="color: pink;">-</span>auto <span style="color: #000000;">1</span> <span style="color: #008080; font-style: italic;">-count</span> <span style="color: #800080;">$collectiontime</span> <span style="color: #008080; font-style: italic;">-path</span> <span style="color: #800080;">$LOG_PATH</span> <span style="color: pink;">-</span>pfmstatis	
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># -- Get data from files --------------------------------------------</span>
<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Analyzing Data...&quot;</span>
<span style="color: #800080;">$perffiles</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #800080;">$LOG_PATH</span>
&nbsp;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$perffile</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$perffiles</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$filedata</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800080;">$LOG_PATH</span>\<span style="color: #800080;">$perffile</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;">$filedata</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #008000;"># Get collection times</span>
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">'^(\d{4}\/(?:0[1-9]|1[0-2]?)\/(?:0[1-9]|[12][0-9]|3[01]?)\s(?:0[0-9]|1[0-9]|2[0-3]?)\:(?:0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]?)\:(?:0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]?))\s-\s(\d{4}\/(?:0[1-9]|1[0-2]?)\/(?:0[1-9]|[12][0-9]|3[01]?)\s(?:0[0-9]|1[0-9]|2[0-3]?)\:(?:0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]?)\:(?:0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]?))$'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #800080;">$collectfrom</span> <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$collectto</span> <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">2</span><span style="color: #000000;">&#93;</span>				
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #008000;"># Get Proc Usage Data</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">'CTL\s+Core\s+Usage'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #800080;">$PROCUsageDataRead</span> <span style="color: pink;">=</span> <span style="color: #800080;">$TRUE</span>
&nbsp;
		<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">elseif</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$PROCUsageDataRead</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #800080;">$line</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">'([0,1]{1})\s+([X,Y]{1})\s+(\d+)'</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span> 
			<span style="color: #800080;">$procusagedata</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span> | <span style="color: #008080; font-weight: bold;">Select</span> ctl<span style="color: pink;">,</span>core<span style="color: pink;">,</span>usage<span style="color: pink;">,</span>datetime
			<span style="color: #800080;">$procusagedata</span>.ctl <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$procusagedata</span>.core <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">2</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$procusagedata</span>.usage <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">3</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$procusagedata</span>.datetime <span style="color: pink;">=</span> <span style="color: #800080;">$collectto</span>
			<span style="color: #800080;">$allprocusagedata</span> <span style="color: pink;">+=</span> <span style="color: #800080;">$procusagedata</span>
&nbsp;
			<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$procusagedata</span>.ctl <span style="color: #FF0000;">-eq</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$procusagedata</span>.core <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;Y&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$PROCUsageDataRead</span> <span style="color: pink;">=</span> <span style="color: #800080;">$FALSE</span><span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># -- List Data ---------------------------------</span>
<span style="color: #008080; font-weight: bold;">Write-Host</span> ”List Processor Data Y or N...”
<span style="color: #800080;">$modekey</span> <span style="color: pink;">=</span> <span style="color: #800080;"><span style="color: #000080;">$Host</span></span>.UI.RawUI.ReadKey<span style="color: #000000;">&#40;</span>”NoEcho<span style="color: pink;">,</span>IncludeKeyDown”<span style="color: #000000;">&#41;</span>
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$modekey</span>.Character.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.ToLower<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;y&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$allprocusagedata</span> | <span style="color: #008080; font-weight: bold;">ft</span> <span style="color: pink;">-</span>prop <span style="color: pink;">*</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># -- Create Processor Usage Chart Group ----------------------</span>
<span style="color: #008080; font-weight: bold;">Write-Host</span> ”Show Processor Usage Charts Y or N...”
<span style="color: #800080;">$modekey</span> <span style="color: pink;">=</span> <span style="color: #800080;"><span style="color: #000080;">$Host</span></span>.UI.RawUI.ReadKey<span style="color: #000000;">&#40;</span>”NoEcho<span style="color: pink;">,</span>IncludeKeyDown”<span style="color: #000000;">&#41;</span>
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$modekey</span>.Character.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.ToLower<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">&quot;y&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;Generating Charts...&quot;</span>
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$Controller</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$Controllers</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$core</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$cores</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$ChartTitle</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Processor Usage Controller:$Controller Core:$core&quot;</span>
			<span style="color: #800080;">$allprocusagedata</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.core <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$core</span><span style="color: #000000;">&#41;</span><span style="color: #FF0000;">-and</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.ctl <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$Controller</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span> | <span style="color: #008080; font-weight: bold;">sort</span> datetime | <span style="color: #008080; font-weight: bold;">Select</span> core<span style="color: pink;">,</span>usage<span style="color: pink;">,</span>datetime | `
			Out<span style="color: pink;">-</span>Chart <span style="color: pink;">-</span>Values usage <span style="color: pink;">-</span>Series_0_Text <span style="color: #800000;">&quot;Processor Usage %&quot;</span> <span style="color: pink;">-</span>Label datetime `
			<span style="color: #008080; font-style: italic;">-Title</span> <span style="color: #800080;">$ChartTitle</span> <span style="color: pink;">-</span>Gallery Lines <span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">Group</span> <span style="color: #800000;">&quot;PROCUSAGE&quot;</span> <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800080;">$ChartTitle</span> <span style="color: pink;">-</span>Caption <span style="color: #800000;">&quot;Processor Usage %&quot;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-weight: bold;">Set-Location</span> <span style="color: #800080;">$SCRIPTHOME</span></pre></td></tr></table></div>

<p>This script collects the data from the array in separate files. Reads the pertinent data from the files and transforms it into object form which is fed into the PowerGadgets out-chart cmdlet. The other three scripts are longer as they digest more information.</p>
<p>To use these scripts you will need PowerShell, PowerGadgets( this a pay product with a free trial ), SNM2 CLI, and the script files attached to this post. Oh and an HDS AMS 2000 array.</p>
<p>Here are the script dowmloads<br />
<a href="http://muegge.com/blogfiles/start-Session.txt" onclick="">start-Session.txt</a><br />
<a href="http://muegge.com/blogfiles/get-performance_processor.txt" onclick="">get-performance_processor.txt</a><br />
<a href="http://muegge.com/blogfiles/get-performance_ports.txt" onclick="">get-performance_ports.txt</a><br />
<a href="http://muegge.com/blogfiles/get-performance_raidgroups.txt" onclick="">get-performance_raidgroups.txt</a><br />
<a href="http://muegge.com/blogfiles/get-performance_luns.txt" onclick="">get-performance_luns.txt</a></p>
<p>Save the files to your script directory and change the extensions to .ps1 </p>
<p>I hope someone finds this useful.</p>
<p>Regards,</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://muegge.com/blog/?feed=rss2&amp;p=114</wfw:commentRss>
		</item>
		<item>
		<title>Exchange DB Reporting with PowerShell and Log Parser</title>
		<link>http://muegge.com/blog/?p=112</link>
		<comments>http://muegge.com/blog/?p=112#comments</comments>
		<pubDate>Thu, 02 Jul 2009 01:09:51 +0000</pubDate>
		<dc:creator>dmuegge</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://muegge.com/blog/?p=112</guid>
		<description><![CDATA[I ran across a useful post today as I was roaming through Google Analytics.

Using PowerShell, LogParser and PowerGadgets to get Exchange 2003 storage information – Part 1
	
Wes Stahler uses Log Parser and PowerShell to report on the free space in an Exchange Database.

This is a task I have done in the past. I will add [...]]]></description>
			<content:encoded><![CDATA[<p>I ran across a useful post today as I was roaming through Google Analytics.
</p>
<p><a href="http://fatbeards.blogspot.com/2009/04/using-powershell-logparser-and.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/fatbeards.blogspot.com');">Using PowerShell, LogParser and PowerGadgets to get Exchange 2003 storage information – Part 1</a>
	</p>
<p>Wes Stahler uses Log Parser and PowerShell to report on the free space in an Exchange Database.
</p>
<p>This is a task I have done in the past. I will add this script to my toolkit.
</p>
<p>Regards,
</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://muegge.com/blog/?feed=rss2&amp;p=112</wfw:commentRss>
		</item>
		<item>
		<title>ADAM Administration with SharePoint and PowerShell</title>
		<link>http://muegge.com/blog/?p=101</link>
		<comments>http://muegge.com/blog/?p=101#comments</comments>
		<pubDate>Mon, 11 May 2009 01:39:14 +0000</pubDate>
		<dc:creator>dmuegge</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://muegge.com/blog/?p=101</guid>
		<description><![CDATA[Welcome!
Recently I worked on finding a simple way to create a web based administrative interface for an ADAM directory. The requirements were to create a simple web based interface to allow business personnel to manage users and groups for an application directory. It was also desirable if this solution would easily integrate with SharePoint.
After doing [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome!</p>
<p>Recently I worked on finding a simple way to create a web based administrative interface for an ADAM directory. The requirements were to create a simple web based interface to allow business personnel to manage users and groups for an application directory. It was also desirable if this solution would easily integrate with SharePoint.</p>
<p>After doing a little searching on the web, I found a combination that fit the bill.</p>
<p><b>The Quest AD Management Shell CmdLets</B> – This is a PowerShell Snap-In that allows administration of AD and ADAM. The cmdlets are from <a href="http://www.quest.com/powershell/activeroles-server.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.quest.com');">Quest Software</a> you can find more info <a href="http://www.quest.com/powershell/activeroles-server.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.quest.com');">here</a>. I have used them in other scripts and they have come in very handy. To make these work in this solution from SharePoint, the Quest Snapin .dll and it&#8217;s dependants need to be copied to the global assembly cache and entered as a safecontrol in the sharepoint web.config.</p>
<p><B>The iLoveSharePoint PowerWebPart 3.0</B> – This is a web part which allows the execution of PowerShell code from the web part. This web part is from the CodePlex project <a href="http://www.codeplex.com/iLoveSharePoint" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.codeplex.com');">iLoveSharePoint</a> by <a href="http://cglessner.blogspot.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/cglessner.blogspot.com');">Christian Glessner</a>. I was impressed with this web part. It is easy to install and configure and relatively simple to use.</p>
<p>The PowerWebPart allows you to execute scripts that will render asp.net web controls in the web part. This allows you to retrieve user input from the controls to use as script inputs. The possibilities are endless. For my purposes I only needed a very simple user interface.</p>
<p>I wanted a way to use this for different ADAM partitions so I tried to allow for different configuration scripts. The design I decided on consisted of three levels of scripts one for configuration one for data access and one UI script for each web part.</p>
<p>The code sample below is the configuration and connection script. This script defines the user and group containers and the directory connect and disconnect functions.</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Library name='ADAM_Config_Lib.ps1'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Adam connection and configuration library</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Dependencies&gt;</span>
<span style="color: #008000;">###	Quest AD CmdLets 1.2</span>
<span style="color: #008000;">### &lt;/Dependencies&gt;</span>
<span style="color: #008000;">### &lt;/Library&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #800080;">$SnapInName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Quest.ActiveRoles.ADManagement&quot;</span>
<span style="color: #800080;">$testsnapin</span> <span style="color: pink;">=</span> <span style="color: #800080;">$null</span>
<span style="color: #800080;">$testsnapin</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">get-pssnapin</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span> <span style="color: #800080;"><span style="color: #000080;">$_</span></span>.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$SnapInName</span><span style="color: #000000;">&#125;</span>
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">-not</span> <span style="color: #800080;">$testsnapin</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">add-pssnapin</span> <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800080;">$SnapInName</span><span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Container paths</span>
<span style="color: #800080;">$usercontainer</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;cn=Users,cn=ADAMDEV&quot;</span>
<span style="color: #800080;">$groupcontainer</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;cn=Groups,cn=ADAMDEV&quot;</span>
&nbsp;
&nbsp;
<span style="color: #0000FF;">function</span> connect<span style="color: pink;">-</span>ADAM<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #008000;"># This allows the use of a windows account for ADAM athentication without granting the application pool account rights to ADAM</span>
	<span style="color: #800080;">$passwd</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;C:\testpwd.txt&quot;</span> | <span style="color: #008080; font-weight: bold;">convertto-securestring</span>
	<span style="color: #800080;">$adamcred</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> <span style="color: #008080; font-style: italic;">-typename</span> System.Management.Automation.PSCredential <span style="color: #008080; font-style: italic;">-argumentlist</span> <span style="color: #800000;">&quot;TestSVR1\adamadmin&quot;</span><span style="color: pink;">,</span><span style="color: #800080;">$passwd</span>
&nbsp;
	connect<span style="color: pink;">-</span>QADService <span style="color: pink;">-</span>Service localhost <span style="color: #008080; font-style: italic;">-Credential</span> <span style="color: #800080;">$adamcred</span>
&nbsp;
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">function</span> disconnect<span style="color: pink;">-</span>ADAM<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$adamconnection</span><span style="color: #000000;">&#41;</span>
&nbsp;
	Disconnect<span style="color: pink;">-</span>QADService <span style="color: pink;">-</span>Connection <span style="color: #800080;">$adamconnection</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The next script is the function library for data access to the ADAM directory.</p>

<div class="wp_syntax"><table><tr><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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Library name='ADAM_Lib.ps1'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Adam directoy function library</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Dependencies&gt;</span>
<span style="color: #008000;">###	Quest AD CmdLets 1.2</span>
<span style="color: #008000;">### &lt;/Dependencies&gt;</span>
<span style="color: #008000;">### &lt;/Library&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> get<span style="color: pink;">-</span>Users<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #800080;">$adamconnection</span> <span style="color: pink;">=</span> connect<span style="color: pink;">-</span>ADAM
	<span style="color: #800080;">$results</span> <span style="color: pink;">=</span> get<span style="color: pink;">-</span>QADObject <span style="color: pink;">-</span>SearchRoot <span style="color: #800080;">$usercontainer</span> <span style="color: pink;">-</span>Connection <span style="color: #800080;">$adamconnection</span>
	disconnect<span style="color: pink;">-</span>ADAM <span style="color: pink;">-</span>adamconnection <span style="color: #800080;">$adamconnection</span>
	<span style="color: #0000FF;">return</span> <span style="color: #800080;">$results</span>
&nbsp;
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0000FF;">function</span> get<span style="color: pink;">-</span>Groups<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #800080;">$adamconnection</span> <span style="color: pink;">=</span> connect<span style="color: pink;">-</span>ADAM
	<span style="color: #800080;">$results</span> <span style="color: pink;">=</span> get<span style="color: pink;">-</span>qadgroup <span style="color: pink;">-</span>SearchRoot <span style="color: #800080;">$groupcontainer</span> <span style="color: pink;">-</span>Connection <span style="color: #800080;">$adamconnection</span>
	disconnect<span style="color: pink;">-</span>ADAM <span style="color: pink;">-</span>adamconnection <span style="color: #800080;">$adamconnection</span>
	<span style="color: #0000FF;">return</span> <span style="color: #800080;">$results</span>
&nbsp;
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0000FF;">function</span> new<span style="color: pink;">-</span>User<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$logonid</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008000;"># Random password generator here</span>
	<span style="color: #800080;">$newpassword</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;password123$&quot;</span>
&nbsp;
	<span style="color: #800080;">$adamconnection</span> <span style="color: pink;">=</span> connect<span style="color: pink;">-</span>ADAM
	New<span style="color: pink;">-</span>QADUser <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800080;">$newlogonid</span> <span style="color: pink;">-</span>UserPassword <span style="color: #800080;">$newpassword</span> <span style="color: pink;">-</span>ParentContainer <span style="color: #800080;">$usercontainer</span> <span style="color: pink;">-</span>Connection <span style="color: #800080;">$adamconnection</span>
	disconnect<span style="color: pink;">-</span>ADAM <span style="color: pink;">-</span>adamconnection <span style="color: #800080;">$adamconnection</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">function</span> delete<span style="color: pink;">-</span>user<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$logonid</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$adamconnection</span> <span style="color: pink;">=</span> connect<span style="color: pink;">-</span>ADAM
	<span style="color: #800080;">$UserDN</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;CN=$logonID,&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$usercontainer</span>
	Remove<span style="color: pink;">-</span>QADObject <span style="color: pink;">-</span>Identity <span style="color: #800080;">$UserDN</span> <span style="color: pink;">-</span>Connection <span style="color: #800080;">$adamconnection</span>
	disconnect<span style="color: pink;">-</span>ADAM <span style="color: pink;">-</span>adamconnection <span style="color: #800080;">$adamconnection</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0000FF;">function</span> new<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">Group</span><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$groupid</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$adamconnection</span> <span style="color: pink;">=</span> connect<span style="color: pink;">-</span>ADAM
	New<span style="color: pink;">-</span>QADGroup <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800080;">$groupid</span> <span style="color: pink;">-</span>GroupType <span style="color: #800000;">&quot;Default&quot;</span> <span style="color: pink;">-</span>ParentContainer <span style="color: #800080;">$groupcontainer</span> <span style="color: pink;">-</span>Connection <span style="color: #800080;">$adamconnection</span>
	disconnect<span style="color: pink;">-</span>ADAM <span style="color: pink;">-</span>adamconnection <span style="color: #800080;">$adamconnection</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0000FF;">function</span> delete<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">group</span><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$groupid</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$adamconnection</span> <span style="color: pink;">=</span> connect<span style="color: pink;">-</span>ADAM
	<span style="color: #800080;">$GroupDN</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;CN=$groupid,&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$groupcontainer</span>
	Remove<span style="color: pink;">-</span>QADObject <span style="color: pink;">-</span>Identity <span style="color: #800080;">$GroupDN</span> <span style="color: pink;">-</span>Connection <span style="color: #800080;">$adamconnection</span>
	disconnect<span style="color: pink;">-</span>ADAM <span style="color: pink;">-</span>adamconnection <span style="color: #800080;">$adamconnection</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0000FF;">function</span> Add<span style="color: pink;">-</span>UserToGroup<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$username</span><span style="color: pink;">,</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$groupname</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$adamconnection</span> <span style="color: pink;">=</span> connect<span style="color: pink;">-</span>ADAM
	<span style="color: #008000;"># Get user dn</span>
	<span style="color: #800080;">$results</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>QADUser <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800080;">$username</span> <span style="color: pink;">-</span>Connection <span style="color: #800080;">$adamconnection</span>
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$result</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$results</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$userdn</span> <span style="color: pink;">=</span> <span style="color: #800080;">$result</span>.dn.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008000;"># Get Group DN</span>
	<span style="color: #800080;">$results</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>QADGroup <span style="color: #008080; font-style: italic;">-Name</span> <span style="color: #800080;">$groupname</span> <span style="color: pink;">-</span>Connection <span style="color: #800080;">$adamconnection</span>
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$result</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$results</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$groupdn</span> <span style="color: pink;">=</span> <span style="color: #800080;">$result</span>.dn.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008000;"># Add User to Group</span>
	Add<span style="color: pink;">-</span>QADGroupMember <span style="color: pink;">-</span>Identity <span style="color: #800080;">$groupdn</span> <span style="color: pink;">-</span>Member <span style="color: #800080;">$userdn</span> <span style="color: pink;">-</span>Connection <span style="color: #800080;">$adamconnection</span>
	disconnect<span style="color: pink;">-</span>ADAM <span style="color: pink;">-</span>adamconnection <span style="color: #800080;">$adamconnection</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The next script is an example of a UI script for the web part. When a new PowerWebPart is created a template script is added by default. This script provides a framework and some sample code. Christian also has an add-on which allows you to use <a href="http://powergui.org/index.jspa" onclick="javascript:pageTracker._trackPageview('/outbound/article/powergui.org');">PowerGui</a> to edit your script from SharePoint. The entire solution contains one script similar to this for each web part.</p>

<div class="wp_syntax"><table><tr><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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;">########### Initialize ############</span>
. c:\ADAMAdmin\ADAM_Lib.ps1
&nbsp;
<span style="color: #008000;">### declare global variables and functions</span>
<span style="color: #800080;">$groupnametext</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Web.UI.WebControls.TextBox
<span style="color: #800080;">$createbutton</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Web.UI.WebControls.Button
<span style="color: #800080;">$groupnamelabel</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Web.UI.WebControls.Label
<span style="color: #800080;">$deletebutton</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Web.UI.WebControls.Button
&nbsp;
&nbsp;
<span style="color: #008000;">############## Load ##############</span>
&nbsp;
<span style="color: #008000;">### first time the OnLoad fires before CreateChildControls</span>
<span style="color: #008000;">#function OnLoad</span>
<span style="color: #008000;">#{</span>
<span style="color: #008000;">#	# Check if GET Request (first request). </span>
<span style="color: #008000;">#	if($isPostBack -eq $false)</span>
<span style="color: #008000;">#	{</span>
<span style="color: #008000;">#		$label.Text = 'GET request.'</span>
<span style="color: #008000;">#	}</span>
<span style="color: #008000;">#}</span>
&nbsp;
<span style="color: #008000;">######## 3. Create Controls ########</span>
&nbsp;
<span style="color: #008000;">## create child controls</span>
<span style="color: #0000FF;">function</span> CreateChildControls<span style="color: #000000;">&#40;</span><span style="color: #800080;">$controls</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$controls</span>.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$groupnametext</span><span style="color: #000000;">&#41;</span>	
&nbsp;
	<span style="color: #800080;">$createbutton</span>.Text <span style="color: pink;">=</span> <span style="color: #800000;">'Add Group'</span>
	Subscribe<span style="color: pink;">-</span>Event <span style="color: #800080;">$createbutton</span> <span style="color: #800000;">'Click'</span> <span style="color: #800000;">'OnCreateButtonClicked'</span>
  	<span style="color: #800080;">$controls</span>.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$createbutton</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$deletebutton</span>.Text <span style="color: pink;">=</span> <span style="color: #800000;">'Delete Group'</span>
	Subscribe<span style="color: pink;">-</span>Event <span style="color: #800080;">$deletebutton</span> <span style="color: #800000;">'Click'</span> <span style="color: #800000;">'OnDeleteButtonClicked'</span>
  	<span style="color: #800080;">$controls</span>.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$deletebutton</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$groupnamelabel</span>.Text <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Groupname&quot;</span>
	<span style="color: #800080;">$controls</span>.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$groupnamelabel</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">######## Events ########</span>
&nbsp;
<span style="color: #008000;">## handle control events</span>
<span style="color: #008000;">## subscribe to an event with &quot;Subscribe-Event($control, 'eventName','callback function name')&quot;</span>
<span style="color: #0000FF;">function</span> OnCreateButtonClicked<span style="color: #000000;">&#40;</span><span style="color: #800080;">$sender</span><span style="color: pink;">,</span> <span style="color: #800080;">$Args</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
   <span style="color: #800080;">$newgroupid</span> <span style="color: pink;">=</span> <span style="color: #800080;">$groupnametext</span>.text
   <span style="color: #008000;"># Create new group</span>
   new<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">Group</span> <span style="color: pink;">-</span>groupid <span style="color: #800080;">$newgroupid</span>
   <span style="color: #800080;">$groupnametext</span>.text <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span>::Empty
&nbsp;
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000FF;">function</span> OnDeleteButtonClicked<span style="color: #000000;">&#40;</span><span style="color: #800080;">$sender</span><span style="color: pink;">,</span> <span style="color: #800080;">$Args</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
   <span style="color: #800080;">$newgroupid</span> <span style="color: pink;">=</span> <span style="color: #800080;">$groupnametext</span>.text
   <span style="color: #008000;"># Delete group</span>
   Remove<span style="color: pink;">-</span><span style="color: #008080; font-weight: bold;">Group</span> <span style="color: pink;">-</span>groupid <span style="color: #800080;">$newgroupid</span>
   <span style="color: #800080;">$groupnametext</span>.text <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span>::Empty
&nbsp;
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">########## Render  #########</span>
&nbsp;
<span style="color: #008000;">### render html</span>
<span style="color: #0000FF;">function</span> Render<span style="color: #000000;">&#40;</span><span style="color: #800080;">$writer</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #800080;">$writer</span>.<span style="color: #008080; font-weight: bold;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;&lt;table&gt;&lt;tr&gt;&lt;td&gt;&quot;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$groupnamelabel</span>.RenderControl<span style="color: #000000;">&#40;</span><span style="color: #800080;">$writer</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$writer</span>.<span style="color: #008080; font-weight: bold;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;&lt;/td&gt;&lt;td&gt;&quot;</span><span style="color: #000000;">&#41;</span>		
	<span style="color: #800080;">$groupnametext</span>.RenderControl<span style="color: #000000;">&#40;</span><span style="color: #800080;">$writer</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$writer</span>.<span style="color: #008080; font-weight: bold;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp&lt;/td&gt;&lt;td colspan=2&gt;&quot;</span><span style="color: #000000;">&#41;</span>
  	<span style="color: #800080;">$createbutton</span>.RenderControl<span style="color: #000000;">&#40;</span><span style="color: #800080;">$writer</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$writer</span>.<span style="color: #008080; font-weight: bold;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;&amp;nbsp&quot;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$deletebutton</span>.RenderControl<span style="color: #000000;">&#40;</span><span style="color: #800080;">$writer</span><span style="color: #000000;">&#41;</span>
  	<span style="color: #800080;">$writer</span>.<span style="color: #008080; font-weight: bold;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;">######### Error Handling #########</span>
&nbsp;
<span style="color: #008000;">### render custom error message</span>
<span style="color: #008000;">##function OnError($exception, $writer){}</span>
&nbsp;
<span style="color: #008000;">########## Unload #########</span>
&nbsp;
<span style="color: #008000;">### clean up</span>
<span style="color: #008000;">##function OnUnload()</span>
<span style="color: #008000;">##{</span>
<span style="color: #008000;">##}</span></pre></td></tr></table></div>

<p>The screenshot below shows the complete solution. This method was simple, effective and easy to create. I dot sourced the corresponding web part script and the connection script in each web part.</p>
<p><img alt="" src="http://muegge.com/images/blogimages/0008_Screenshot_01.jpg" class="alignnone" width="839" height="315" /></p>
<p>This is a pretty quick and easy way to expose some simple administrative or user functionality on a SharePoint Intranet.</p>
<p>I hope this helps.</p>
<p>Regards,</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://muegge.com/blog/?feed=rss2&amp;p=101</wfw:commentRss>
		</item>
		<item>
		<title>Connection History with PowerShell and NetStat</title>
		<link>http://muegge.com/blog/?p=99</link>
		<comments>http://muegge.com/blog/?p=99#comments</comments>
		<pubDate>Fri, 20 Mar 2009 04:55:34 +0000</pubDate>
		<dc:creator>dmuegge</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://muegge.com/blog/?p=99</guid>
		<description><![CDATA[Welcome!

This is a little trick some might find useful. I was working on decommissioning some servers and I needed a way to find out what was connecting to these machines. I decided to create a script to log connections. I have done this in the past in various ways which usually involved logging a bunch [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome!
</p>
<p>This is a little trick some might find useful. I was working on decommissioning some servers and I needed a way to find out what was connecting to these machines. I decided to create a script to log connections. I have done this in the past in various ways which usually involved logging a bunch of data and then querying against it to find the unique connections.
</p>
<p>This time it finally occurred to me, just filter the data as it is being collected. So I set out to write a PowerShell script that would keep a running list of client TCP connections to a given machine. This information would be stored in a text file.
</p>
<p>The first step was to collect the information and put it into a PowerShell object.</p>

<div class="wp_syntax"><table><tr><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"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># Create array to hold netstat data</span>
<span style="color: #800080;">$allnetstatdata</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008000;"># Get TCP connections from netstat</span>
<span style="color: #800080;">$netstatResults</span> <span style="color: pink;">=</span> <span style="color: pink;">&amp;</span> netstat <span style="color: pink;">-</span>p TCP 
<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;">$netstatResults</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #008000;"># Create object data and add to object</span>
	<span style="color: #800080;">$netstatdata</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span> | <span style="color: #008080; font-weight: bold;">Select</span> local<span style="color: pink;">,</span>port<span style="color: pink;">,</span>foreign
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">-not</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span>.Trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span>::Empty<span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-or</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span>.contains<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Active Connections&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-or</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span>.contains<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Local Address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span> <span style="color: #FF0000;">-match</span> <span style="color: #800000;">'\s*(TCP)\s{1,}(\D\S{1,})\s{1,}(\D\S{1,})\s{1,}(\D\S{1,}$)'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$localhost</span> <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">2</span><span style="color: #000000;">&#93;</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;:&quot;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #800080;">$netstatdata</span>.local <span style="color: pink;">=</span> <span style="color: #800080;">$dchost</span><span style="color: #000000;">&#91;</span>0<span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$netstatdata</span>.port <span style="color: pink;">=</span> <span style="color: #800080;">$dchost</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$foreignhost</span> <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">3</span><span style="color: #000000;">&#93;</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;:&quot;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #800080;">$netstatdata</span>.foreign <span style="color: pink;">=</span> <span style="color: #800080;">$foreignhost</span><span style="color: #000000;">&#91;</span>0<span style="color: #000000;">&#93;</span>
			<span style="color: #800080;">$allnetstatdata</span> <span style="color: pink;">=</span> <span style="color: #800080;">$allnetstatdata</span> <span style="color: pink;">+</span> <span style="color: #800080;">$netstatdata</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>
<p>Then the next step was to read the file with the previous information and add it to the PowerShell object.
</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># Define Log File</span>
<span style="color: #800080;">$logfile</span> <span style="color: pink;">=</span> C:\TCPConnections.txt
&nbsp;
<span style="color: #008000;"># Get data from file and add to object</span>
<span style="color: #800080;">$filedata</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800080;">$logfile</span>
<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$filedata</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$filedata</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$netstatdata</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span> | <span style="color: #008080; font-weight: bold;">Select</span> local<span style="color: pink;">,</span>port<span style="color: pink;">,</span>foreign
		<span style="color: #800080;">$entry</span> <span style="color: pink;">=</span> <span style="color: #800080;">$line</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`t</span>&quot;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$netstatdata</span>.port <span style="color: pink;">=</span> <span style="color: #800080;">$entry</span><span style="color: #000000;">&#91;</span>0<span style="color: #000000;">&#93;</span>
		<span style="color: #800080;">$netstatdata</span>.local <span style="color: pink;">=</span> <span style="color: #800080;">$entry</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">1</span><span style="color: #000000;">&#93;</span>
		<span style="color: #800080;">$netstatdata</span>.foreign <span style="color: pink;">=</span> <span style="color: #800080;">$entry</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">2</span><span style="color: #000000;">&#93;</span>
		<span style="color: #800080;">$allnetstatdata</span> <span style="color: pink;">=</span> <span style="color: #800080;">$allnetstatdata</span> <span style="color: pink;">+</span> <span style="color: #800080;">$netstatdata</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

</p>
<p>We can now remove the duplicates from the combined information and save the updated file.
</p>
<p>
<div class="wp_syntax"><table><tr><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"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># Get unique rows</span>
<span style="color: #800080;">$newnetstatdata</span> <span style="color: pink;">=</span> <span style="color: #800080;">$allnetstatdata</span> | <span style="color: #008080; font-weight: bold;">Select-Object</span> <span style="color: pink;">*</span> <span style="color: #008080; font-style: italic;">-unique</span> 
&nbsp;
<span style="color: #008000;"># Add combined data to string</span>
<span style="color: #800080;">$tempcontent</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Text.StringBuilder
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$netstat</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$newnetstatdata</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$netstat</span>.port<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$tempcontent</span>.Append<span style="color: #000000;">&#40;</span><span style="color: #800080;">$netstat</span>.port.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span><span style="color: #000000;">&#125;</span>
	<span style="color: #800080;">$tempcontent</span>.Append<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`t</span>&quot;</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span> 
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$netstat</span>.local<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$tempcontent</span>.Append<span style="color: #000000;">&#40;</span><span style="color: #800080;">$netstat</span>.local.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span><span style="color: #000000;">&#125;</span>
	<span style="color: #800080;">$tempcontent</span>.Append<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`t</span>&quot;</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span> 
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$netstat</span>.foreign<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$tempcontent</span>.Append<span style="color: #000000;">&#40;</span><span style="color: #800080;">$netstat</span>.foreign.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span><span style="color: #000000;">&#125;</span>
	<span style="color: #800080;">$tempcontent</span>.Append<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`n</span>&quot;</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span> 
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Write to file</span>
<span style="color: #008080; font-weight: bold;">Set-Content</span> <span style="color: #008080; font-style: italic;">-path</span> <span style="color: #800080;">$logfile</span> <span style="color: #800080;">$tempcontent</span>.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

</p>
<p>We can run this script in a scheduled task at whatever interval is required. Now we have a log of unique inbound TCP connections.
</p>
<p>Best Regards,
</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://muegge.com/blog/?feed=rss2&amp;p=99</wfw:commentRss>
		</item>
		<item>
		<title>PowerShell, Log Parser, PowerGadgets, and GeoIP what fun!</title>
		<link>http://muegge.com/blog/?p=93</link>
		<comments>http://muegge.com/blog/?p=93#comments</comments>
		<pubDate>Sun, 15 Mar 2009 01:03:53 +0000</pubDate>
		<dc:creator>dmuegge</dc:creator>
		
		<category><![CDATA[Log Parser]]></category>

		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://muegge.com/blog/?p=93</guid>
		<description><![CDATA[Welcome!
One day I was pondering how I might use log parser to map visitors to a website by state. I am aware this is easily done with tools like Google Analytics, but I was interested in using existing logs for the info.
Using the PowerShell and Log Parser functions from the library listed in a previouse [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome!</p>
<p>One day I was pondering how I might use log parser to map visitors to a website by state. I am aware this is easily done with tools like Google Analytics, but I was interested in using existing logs for the info.</p>
<p>Using the PowerShell and Log Parser functions from the library listed in a previouse post. Log parser can easily get the visitors by IP Address from an IIS log.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># Get unique ip addresses to working file</span>
<span style="color: #800080;">$query</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #800000;">&quot;
SELECT Distinct c-ip As IPAddress
INTO D:\Unique_IP_Addresses.txt
From C:\Logs\ex080310.log
&quot;</span><span style="color: pink;">@</span>
<span style="color: #800080;">$LPInputFormat</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>LPInputFormat <span style="color: #800000;">&quot;w3c&quot;</span>
<span style="color: #800080;">$LPOutputFormat</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>LPOutputFormat <span style="color: #800000;">&quot;csv&quot;</span>
Invoke<span style="color: pink;">-</span>LPExecuteBatch <span style="color: #008080; font-style: italic;">-query</span> <span style="color: #800080;">$query</span> <span style="color: pink;">-</span>inputtype <span style="color: #800080;">$LPInputFormat</span> <span style="color: pink;">-</span>outputtype <span style="color: #800080;">$LPOutputFormat</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span></pre></div></div>

<p>The next task is to get the location of the IP addresses. The tool I chose for this task was the free GeoLite City from MaxMind <a href="http://www.maxmind.com/app/geolitecity" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.maxmind.com');">http://www.maxmind.com/app/geolitecity</a>. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># Get location for IP addresses from GEOIP DB</span>
<span style="color: #800080;">$geodbpath</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\GeoLiteCity&quot;</span>
<span style="color: #800080;">$visitors</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;D:\Unique_IP_Addresses.txt&quot;</span>
<span style="color: #800080;">$ipinfo</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-ComObject</span> <span style="color: #800000;">&quot;GeoIPCOMEx.GeoIPEx&quot;</span>
<span style="color: #800080;">$ipinfo</span>.set_db_path<span style="color: #000000;">&#40;</span><span style="color: #800080;">$geodbpath</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;D:\IP_Locations.txt&quot;</span> <span style="color: #800000;">&quot;Location&quot;</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$visitor</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$visitors</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$ipinfo</span>.find_by_addr<span style="color: #000000;">&#40;</span><span style="color: #800080;">$visitor</span>.ipaddress<span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$ipinfo</span>.Region<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;D:\IP_Locations.txt&quot;</span> <span style="color: #800080;">$ipinfo</span>.Region
	<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;">Add-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;D:\IP_Locations.txt&quot;</span> <span style="color: #800000;">&quot;Unknown&quot;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Get State counts</span>
<span style="color: #800080;">$query</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #800000;">&quot;
SELECT Location, Count(Location) As Visitors
INTO D:\VisitorsByState.txt
From D:\IP_Locations.txt
Group By Location
&quot;</span><span style="color: pink;">@</span>
<span style="color: #800080;">$LPInputFormat</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>LPInputFormat <span style="color: #800000;">&quot;csv&quot;</span>
<span style="color: #800080;">$LPOutputFormat</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>LPOutputFormat <span style="color: #800000;">&quot;csv&quot;</span>
Invoke<span style="color: pink;">-</span>LPExecuteBatch <span style="color: #008080; font-style: italic;">-query</span> <span style="color: #800080;">$query</span> <span style="color: pink;">-</span>inputtype <span style="color: #800080;">$LPInputFormat</span> <span style="color: pink;">-</span>outputtype <span style="color: #800080;">$LPOutputFormat</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span></pre></div></div>

<p>There are a couple of ways to use the MaxMind GeoIP database. It can be used in it&#8217;s native binary format or it can be imported into SQL from csv files. MaxMind recommends using the binary format, which is what I chose to do. MaxMind also provides API&#8217;s for use with a variety of platform&#8217;s. I chose to use the COM version.</p>
<p>After the location is determined the counts are calculated. This brings us to the point where we need to chart the results. The tool I chose for this operation is <a href="http://www.softwarefx.com/sfxSqlProducts/powerGadgets" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.softwarefx.com');">PowerGadgets.</a> This is a charting tool made for use with PowerShell, it can be handy. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># Create PowerGadgets Map</span>
<span style="color: #800080;">$charttitle</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Daily visitors by state&quot;</span>
<span style="color: #800080;">$visits</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> D:\VisitorsByState.txt
<span style="color: #800080;">$visits</span> | Out<span style="color: pink;">-</span>Map <span style="color: pink;">-</span>mapsource <span style="color: #800000;">&quot;US\USA-StatesAbrev&quot;</span> `
		<span style="color: pink;">-</span>label location `
		<span style="color: pink;">-</span>values visitors `
		<span style="color: pink;">-</span>palette <span style="color: #800000;">&quot;highcontrast&quot;</span> `
		<span style="color: pink;">-</span>output <span style="color: #800080;">$chartout</span> `
		<span style="color: pink;">-</span>size <span style="color: #000000;">600</span><span style="color: pink;">,</span><span style="color: #000000;">400</span> `
		<span style="color: pink;">-</span>BackColor White `
		<span style="color: pink;">-</span>Titles_0_BackColor White `
		<span style="color: pink;">-</span>Titles_0_Font <span style="color: #800000;">&quot;Arial, 14&quot;</span> `
		<span style="color: pink;">-</span>Titles_0_Text <span style="color: #800080;">$charttitle</span> `
		<span style="color: pink;">-</span>Titles_0_TextColor Black `
		<span style="color: pink;">-</span>conditionalattributes_0_condition_from <span style="color: #000000;">1</span> `
		<span style="color: pink;">-</span>conditionalattributes_0_condition_to <span style="color: #000000;">10</span> `
		<span style="color: pink;">-</span>conditionalattributes_0_color Red `
		<span style="color: pink;">-</span>conditionalattributes_0_text <span style="color: #800000;">&quot;1 to 10&quot;</span> `
		<span style="color: pink;">-</span>conditionalattributes_1_condition_from <span style="color: #000000;">11</span> `
		<span style="color: pink;">-</span>conditionalattributes_1_condition_to <span style="color: #000000;">25</span> `
		<span style="color: pink;">-</span>conditionalattributes_1_color Blue `
		<span style="color: pink;">-</span>conditionalattributes_1_text <span style="color: #800000;">&quot;11 to 25&quot;</span> `
		<span style="color: pink;">-</span>conditionalattributes_2_condition_from <span style="color: #000000;">26</span> `
		<span style="color: pink;">-</span>conditionalattributes_2_condition_to <span style="color: #000000;">50</span> `
		<span style="color: pink;">-</span>conditionalattributes_2_color Green `
		<span style="color: pink;">-</span>conditionalattributes_2_text <span style="color: #800000;">&quot;26 to 50&quot;</span> `
		<span style="color: pink;">-</span>conditionalattributes_3_condition_from <span style="color: #000000;">51</span> `
		<span style="color: pink;">-</span>conditionalattributes_3_condition_to <span style="color: #000000;">100</span> `
		<span style="color: pink;">-</span>conditionalattributes_3_color Yellow `
		<span style="color: pink;">-</span>conditionalattributes_3_text <span style="color: #800000;">&quot;51 to 100&quot;</span> `
		<span style="color: pink;">-</span>conditionalattributes_4_condition_from <span style="color: #000000;">101</span> `
		<span style="color: pink;">-</span>conditionalattributes_4_condition_to <span style="color: #000000;">1000</span> `
		<span style="color: pink;">-</span>conditionalattributes_4_color Violet `
		<span style="color: pink;">-</span>conditionalattributes_4_text <span style="color: #800000;">&quot;101 to 1000&quot;</span></pre></div></div>

<p> </p>
<p>And here is our final Chart.</p>
<p><img class="alignnone" title="VisitorsByState" src="http://muegge.com/images/blogimages/visitorsbystate.jpg" alt="" /> </p>
<p>This works pretty well the only drawback to this solution is that PowerGadgets is a pay tool, but since I own a copy it suites my needs.</p>
<p>Regards,</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://muegge.com/blog/?feed=rss2&amp;p=93</wfw:commentRss>
		</item>
		<item>
		<title>Automated Machine Builds with PowerShell and AutoIT</title>
		<link>http://muegge.com/blog/?p=73</link>
		<comments>http://muegge.com/blog/?p=73#comments</comments>
		<pubDate>Sun, 22 Feb 2009 01:41:07 +0000</pubDate>
		<dc:creator>dmuegge</dc:creator>
		
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://muegge.com/blog/?p=73</guid>
		<description><![CDATA[I have been using PowerShell recently to create scripted builds for virtual machines. This method provides some benefits over imaging.

A single base image per OS can be used
Applications which have issues with imaging can be easily installed such as SQL Server, BizTalk, Exchange, Etc.
Scripted installs can be changed or updated easier than images
Additional configuration changes [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using PowerShell recently to create scripted builds for virtual machines. This method provides some benefits over imaging.</p>
<ul>
<li>A single base image per OS can be used</li>
<li>Applications which have issues with imaging can be easily installed such as SQL Server, BizTalk, Exchange, Etc.</li>
<li>Scripted installs can be changed or updated easier than images</li>
<li>Additional configuration changes such as registry settings and/or configuration files can be easily done</li>
</ul>
<p></br>One challenge I faced with this process was automating the installation of applications which do not have unattended install capabilities.
</p>
<p>A great tool I found for this was <a href="http://www.autoitscript.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.autoitscript.com');">AutoIT</a>. It provides a method to script the windows GUI and enables automating those installs. AutoIT consists of a COM object, scripting language, editor, compiler, and a cool window info tool. I will not get into the details of AutoIT here it has good documentation. I found PowerShell to be much stronger at the scripting part, but the COM object provides excellent window control functionality to PowerShell. The window info tool that comes with AutoIT is also very handy. Here is a simple example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;">	<span style="color: #800080;">$AutoIt</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-ComObject</span> AutoItX3.Control
	<span style="color: #800080;">$result</span> <span style="color: pink;">=</span> <span style="color: #800080;">$AutoIt</span>.Run<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;notepad.exe&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$wintitle</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Untitled - Notepad&quot;</span>
	<span style="color: #800080;">$AutoIt</span>.WinWaitActive<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
	<span style="color: #800080;">$AutoIt</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;This is a test&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>To run the code above you will need to install AutoIT or just register the AutoItX.dll COM object. The code will launch notepad and type &#8220;This is a test&#8221;. While this is a pretty useless example it shows the basic purpose of AutoIT and how it is used from PowerShell.</p>
<p>The next piece I looked at was creating a function library to make it easy to use AutoIT from PowerShell.</p>
<p>Example: AutoIT PowerShell function library</p>

<div class="wp_syntax"><table><tr><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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># Installation path</span>
<span style="color: #800080;">$InstallDir</span> <span style="color: pink;">=</span> <span style="color: #800000;">'\\SERVERNAME\Installs$'</span>
&nbsp;
<span style="color: #008000;"># -- Copy and register AutoIt COM object -------------------------------------</span>
<span style="color: #008080; font-weight: bold;">Copy-item</span> <span style="color: #800080;">$InstallDir</span>\AutoItX3.dll <span style="color: #800080;">$env</span>:windir\system32
<span style="color: pink;">&amp;</span> regsvr32 AutoItX3.dll <span style="color: pink;">/</span>s
&nbsp;
<span style="color: #008000;"># Wait for registration and create AutoIT instance</span>
<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #000000;">2</span>
<span style="color: #800080;">$AutoIt</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-ComObject</span> AutoItX3.Control
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Test-ProcessRunning'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;CreateDate&gt;20080510&lt;/CreateDate&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Test if process in running give name</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">### 	$result = Test-ProcessRunning -processname &lt;string&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;Notes&gt;</span>
<span style="color: #008000;">### 	Helper function used to wait for process to complete</span>
<span style="color: #008000;">### &lt;/Notes&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Test<span style="color: pink;">-</span>ProcessRunning<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;">$processname</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #800080;">$currentprocess</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Process</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.Name <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$processname</span><span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$currentprocess</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">return</span> 0
	<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">else</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">return</span> <span style="color: #000000;">1</span>
	<span style="color: #000000;">&#125;</span>	
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Test-PIDRunning'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;CreateDate&gt;20080510&lt;/CreateDate&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Test if process in running given process ID</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">### 	$result = Test-PIDRunning</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;Notes&gt;</span>
<span style="color: #008000;">### 	Helper function used to wait for process to complete</span>
<span style="color: #008000;">### &lt;/Notes&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Test<span style="color: pink;">-</span>PIDRunning<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">Int</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$pidnumber</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #800080;">$currentprocess</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Process</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.ID <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$pidnumber</span><span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$currentprocess</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">return</span> 0
	<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">else</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">return</span> <span style="color: #000000;">1</span>
	<span style="color: #000000;">&#125;</span>	
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Start-WindowControl'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;CreateDate&gt;20080516&lt;/CreateDate&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Wait for window presence and activate</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">### 	Start-WindowControl -wintitle &lt;window title&gt; -wintext &lt;window text&gt; -keyarray &lt;(&quot;v1&quot;,&quot;v2&quot;,&quot;v3&quot;)&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;Notes&gt;</span>
<span style="color: #008000;">### 	The sleeps and the winactivate winwaitactive sequence was required for greater reliability.</span>
<span style="color: #008000;">### &lt;/Notes&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Start<span style="color: pink;">-</span>WindowControl<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span>
	   	  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$wintext</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: pink;">,</span>
		  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$keyarray</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008000;"># Window text was supplied</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">do</span><span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">100</span><span style="color: #000000;">&#125;</span><span style="color: #0000FF;">until</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$AutoIt</span>.WinActivate<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
		<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
		<span style="color: #800080;">$AutoIt</span>.WinWaitActive<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
		<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># Only window title was supplied</span>
		<span style="color: #0000FF;">do</span><span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">100</span><span style="color: #000000;">&#125;</span><span style="color: #0000FF;">until</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$AutoIt</span>.WinActivate<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
		<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
		<span style="color: #800080;">$AutoIt</span>.WinWaitActive<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
		<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>	
	<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #008000;"># If keys were supplied</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #008000;"># Send Keys</span>
		<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$AutoIt</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Start-WindowControlAB'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;CreateDate&gt;20081125&lt;/CreateDate&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Wait for window presence and activate</span>
<span style="color: #008000;">###		will accept one of two windows texts</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">### 	Start-WindowControlAB -wintitle &lt;window title&gt; -wintext &lt;window text&gt; -altwintext &lt;alternate window text&gt; -keyarray &lt;(&quot;v1&quot;,&quot;v2&quot;,&quot;v3&quot;)&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;Notes&gt;</span>
<span style="color: #008000;">### 	The sleeps and the winactivate winwaitactive sequence was required for greater reliability.</span>
<span style="color: #008000;">### &lt;/Notes&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Start<span style="color: pink;">-</span>WindowControlAB<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span>
	   	  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$wintext</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: pink;">,</span>
		  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$altwintext</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: pink;">,</span>
		  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$keyarray</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
&nbsp;
		<span style="color: #008000;"># Wait for window to exist and activate</span>
		<span style="color: #800080;">$winfoundflag</span> <span style="color: pink;">=</span> <span style="color: #800080;">$false</span>
		<span style="color: #0000FF;">do</span><span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
			<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$winfoundflag</span> <span style="color: pink;">=</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#125;</span>
			<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$altwintext</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$winfoundflag</span> <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;<span style="color: #800080;">$wintext</span> <span style="color: pink;">=</span> <span style="color: #800080;">$altwintext</span><span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">until</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$winfoundflag</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$AutoIt</span>.WinActivate<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
		<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
		<span style="color: #800080;">$AutoIt</span>.WinWaitActive<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
		<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">500</span>
&nbsp;
		<span style="color: #008000;"># If keys were supplied</span>
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># Send Keys</span>
			<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #800080;">$AutoIt</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</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>
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Start-WindowControlQAB'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;CreateDate&gt;20081125&lt;/CreateDate&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Wait for window presence and activate</span>
<span style="color: #008000;">###		will accept one of two windows texts and Title texts</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">### 	Start-WindowControlQAB -wintitle &lt;window title&gt; -altwintitle &lt;window title&gt; -wintext &lt;window text&gt; -altwintext &lt;alternate window text&gt; -keyarray &lt;(&quot;v1&quot;,&quot;v2&quot;,&quot;v3&quot;)&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;Notes&gt;</span>
<span style="color: #008000;">### 	The sleeps and the winactivate winwaitactive sequence was required for greater reliability.</span>
<span style="color: #008000;">### &lt;/Notes&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Start<span style="color: pink;">-</span>WindowControlQAB<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span>
		  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$altwintitle</span><span style="color: pink;">,</span>
	   	  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$wintext</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: pink;">,</span>
		  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$altwintext</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: pink;">,</span>
		  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$keyarray</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008000;"># Wait for window to exist and activate</span>
	<span style="color: #800080;">$winfoundflag</span> <span style="color: pink;">=</span> <span style="color: #800080;">$false</span>
	<span style="color: #0000FF;">do</span><span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$winfoundflag</span> <span style="color: pink;">=</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#125;</span>
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$altwintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$altwintext</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$winfoundflag</span> <span style="color: pink;">=</span> <span style="color: #800080;">$true</span>;<span style="color: #800080;">$wintext</span> <span style="color: pink;">=</span> <span style="color: #800080;">$altwintext</span>;<span style="color: #800080;">$wintitle</span> <span style="color: pink;">=</span> <span style="color: #800080;">$altwintitle</span><span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">until</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$winfoundflag</span><span style="color: #000000;">&#41;</span>
	<span style="color: #800080;">$AutoIt</span>.WinActivate<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
	<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
	<span style="color: #800080;">$AutoIt</span>.WinWaitActive<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
	<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">500</span>
&nbsp;
	<span style="color: #008000;"># If keys were supplied</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #008000;"># Send Keys</span>
		<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$AutoIt</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</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>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Start-WindowControlN'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;CreateDate&gt;20080516&lt;/CreateDate&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Checks for window presence once a second for specified number of times</span>
<span style="color: #008000;">###		Sends keys when found</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">### 	Start-WindowControlN -check &lt;integer&gt; -wintitle &lt;window title&gt; -wintext &lt;window text&gt; -keyarray &lt;@(&quot;'n&quot;)&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;Notes&gt;</span>
<span style="color: #008000;">### 	The sleeps and the winactivate winwaitactive sequence was required for greater reliability.</span>
<span style="color: #008000;">### &lt;/Notes&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Start<span style="color: pink;">-</span>WindowControlN<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>Int32<span style="color: #000000;">&#93;</span><span style="color: #800080;">$check</span><span style="color: pink;">,</span>
		  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span>
	   	  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$wintext</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: pink;">,</span>
		  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$keyarray</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008000;"># Check if window exists once a second for N times</span>
	<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">=</span> 0; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$check</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Seconds</span> <span style="color: #000000;">1</span>
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$AutoIt</span>.WinActivate<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span>
			<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">100</span>
			<span style="color: #800080;">$AutoIt</span>.WinWaitActive<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span> 
			<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
&nbsp;
			<span style="color: #008000;"># If keys were supplied</span>
			<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #008000;"># Send Keys</span>
				<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
					<span style="color: #800080;">$AutoIt</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #0000FF;">break</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Start-WindowControlS'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;CreateDate&gt;20080516&lt;/CreateDate&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Wait for window presence and activate</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">### 	Start-WindowControlS -wintitle &lt;window title&gt; -wintext &lt;window text&gt; -keyarray &lt;(&quot;v1&quot;,&quot;v2&quot;,&quot;v3&quot;)&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;Notes&gt;</span>
<span style="color: #008000;">### 	The sleeps and the winactivate winwaitactive sequence was required for greater reliability.</span>
<span style="color: #008000;">### &lt;/Notes&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Start<span style="color: pink;">-</span>WindowControlS<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span>
		  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$wintext</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: pink;">,</span>
		  <span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$keyarray</span><span style="color: pink;">=</span><span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008000;"># Window text was supplied	 </span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0000FF;">do</span><span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
		<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">until</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
&nbsp;
		<span style="color: #0000FF;">do</span><span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$AutoIt</span>.WinActivate<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
			<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
			<span style="color: #800080;">$AutoIt</span>.WinWaitActive<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
			<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">500</span>
&nbsp;
			<span style="color: #008000;"># If keys were supplied</span>
			<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
				<span style="color: #008000;"># Send Keys</span>
				<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
					<span style="color: #800080;">$AutoIt</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">until</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">-not</span> <span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: pink;">,</span><span style="color: #800080;">$wintext</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">else</span>
	<span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #008000;"># Only window title was supplied</span>
		<span style="color: #0000FF;">do</span><span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
		<span style="color: #000000;">&#125;</span><span style="color: #0000FF;">until</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
&nbsp;
		<span style="color: #0000FF;">do</span><span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$AutoIt</span>.WinActivate<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
			<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">200</span>
			<span style="color: #800080;">$AutoIt</span>.WinWaitActive<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: #000000;">&#41;</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span>
			<span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #000000;">500</span>
&nbsp;
			<span style="color: #008000;"># If keys were supplied</span>
			<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
&nbsp;
				<span style="color: #008000;"># Send Keys</span>
				<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$keyarray</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
					<span style="color: #800080;">$AutoIt</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$key</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><span style="color: #0000FF;">until</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">-not</span> <span style="color: #800080;">$AutoIt</span>.WinExists<span style="color: #000000;">&#40;</span><span style="color: #800080;">$wintitle</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Write-Log'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;CreateDate&gt;20080517&lt;/CreateDate&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Writes output to host with timestamp</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">### 	Write-Log &lt;logentry&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;Notes&gt;</span>
<span style="color: #008000;">### 	Intended for us with Transcript functionality</span>
<span style="color: #008000;">### &lt;/Notes&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Write<span style="color: pink;">-</span>Log<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$logentry</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$logtime</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span> <span style="color: #008080; font-style: italic;">-Format</span> T
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;$logtime - $logentry&quot;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Then using the functions from the library above I created application installation functions for various packages.</p>
<p>Example: PowerShell Application Installation Function</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Install-TortoiseSVN152'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;CreateDate&gt;20080826&lt;/CreateDate&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Installs Tortoise Subversion Client 1.5.2.13595-win32-svn-1.5.1</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">### 	$result = Install-TortoiseSVN152</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;Notes&gt;</span>
<span style="color: #008000;">### 	Requires reboot when complete</span>
<span style="color: #008000;">###		Will not always prompt for reboot and will restart automatically</span>
<span style="color: #008000;">###		Always put at end of install step</span>
<span style="color: #008000;">### &lt;/Notes&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Install<span style="color: pink;">-</span>TortoiseSVN152<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">switch</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$restart</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$result</span> <span style="color: pink;">=</span> <span style="color: #800080;">$AutoIt</span>.Run<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;msiexec /i $InstallDir\TortoiseSVN-1.5.2.13595-win32-svn-1.5.1.msi&quot;</span><span style="color: #000000;">&#41;</span>
	Write<span style="color: pink;">-</span>Log <span style="color: #800000;">&quot;Tortoise Subversion Client 1.5.2 Install Launched PID:$result&quot;</span>
&nbsp;
	Start<span style="color: pink;">-</span>WindowControl <span style="color: pink;">-</span>wintitle <span style="color: #800000;">&quot;TortoiseSVN 1.5.2.13595 (32 bit) Setup&quot;</span> <span style="color: pink;">-</span>wintext <span style="color: #800000;">&quot;Welcome to the TortoiseSVN 1.5.2.13595 (32 bit) Setup Wizard&quot;</span> <span style="color: pink;">-</span>keyarray <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;!n&quot;</span><span style="color: #000000;">&#41;</span>
	Start<span style="color: pink;">-</span>WindowControl <span style="color: pink;">-</span>wintitle <span style="color: #800000;">&quot;TortoiseSVN 1.5.2.13595 (32 bit) License Agreement&quot;</span> <span style="color: pink;">-</span>wintext <span style="color: #800000;">&quot;I &amp;accept the terms in the License Agreement&quot;</span> <span style="color: pink;">-</span>keyarray <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;!a&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;!n&quot;</span><span style="color: #000000;">&#41;</span>
	Start<span style="color: pink;">-</span>WindowControl <span style="color: pink;">-</span>wintitle <span style="color: #800000;">&quot;TortoiseSVN 1.5.2.13595 (32 bit) Setup&quot;</span> <span style="color: pink;">-</span>wintext <span style="color: #800000;">&quot;Custom Setup&quot;</span> <span style="color: pink;">-</span>keyarray <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;!n&quot;</span><span style="color: #000000;">&#41;</span>
	Start<span style="color: pink;">-</span>WindowControl <span style="color: pink;">-</span>wintitle <span style="color: #800000;">&quot;TortoiseSVN 1.5.2.13595 (32 bit) Setup&quot;</span> <span style="color: pink;">-</span>wintext <span style="color: #800000;">&quot;Ready to Install&quot;</span> <span style="color: pink;">-</span>keyarray <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;!i&quot;</span><span style="color: #000000;">&#41;</span>
	Start<span style="color: pink;">-</span>WindowControl <span style="color: pink;">-</span>wintitle <span style="color: #800000;">&quot;TortoiseSVN 1.5.2.13595 (32 bit) Setup&quot;</span> <span style="color: pink;">-</span>wintext <span style="color: #800000;">&quot;Click the Finish button to exit the Setup Wizard.&quot;</span> <span style="color: pink;">-</span>keyarray <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;!f&quot;</span><span style="color: #000000;">&#41;</span>
	Start<span style="color: pink;">-</span>WindowControl <span style="color: pink;">-</span>wintitle <span style="color: #800000;">&quot;Changelog.txt - Notepad&quot;</span> <span style="color: pink;">-</span>keyarray <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;!{F4}&quot;</span><span style="color: #000000;">&#41;</span>
	Start<span style="color: pink;">-</span>WindowControl <span style="color: pink;">-</span>wintitle <span style="color: #800000;">&quot;Installer Information&quot;</span> <span style="color: pink;">-</span>keyarray <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;&amp;Yes&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008000;"># Check if reboot window exists once a second for five seconds</span>
	Start<span style="color: pink;">-</span>WindowControlN <span style="color: pink;">-</span>check <span style="color: #000000;">5</span><span style="color: pink;">-</span>wintitle <span style="color: #800000;">&quot;Installer Information&quot;</span> <span style="color: pink;">-</span>wintext <span style="color: #800000;">&quot;&amp;Yes&quot;</span> <span style="color: pink;">-</span>keyarray <span style="color: pink;">@</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;!n&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
	Write<span style="color: pink;">-</span>Log <span style="color: #800000;">&quot;Tortoise Subversion Client 1.5.2 Install Completed&quot;</span>
&nbsp;
	<span style="color: #008000;"># Restart if selected</span>
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$restart</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;$logtime - Restarting...&quot;</span>
		Shutdown <span style="color: pink;">/</span><span style="color: #008080; font-weight: bold;">r</span> <span style="color: pink;">/</span>t <span style="color: #000000;">1</span> <span style="color: pink;">/</span>d P:<span style="color: #000000;">2</span>:<span style="color: #000000;">4</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Using the method above I created a library of PowerShell installation functions to be called from automated build scripts. </p>
<p>This seems to be working pretty well so far and it allows automated builds to be scripted quickly. PowerShell and AutoIT work well together, the AutoIT COM object provides excellent GUI control and PowerShell provides strong scripting and debugging features.</p>
<p>Best Regards,</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://muegge.com/blog/?feed=rss2&amp;p=73</wfw:commentRss>
		</item>
		<item>
		<title>Log Parser and PowerShell – Part II</title>
		<link>http://muegge.com/blog/?p=65</link>
		<comments>http://muegge.com/blog/?p=65#comments</comments>
		<pubDate>Sat, 22 Nov 2008 02:05:34 +0000</pubDate>
		<dc:creator>dmuegge</dc:creator>
		
		<category><![CDATA[Log Parser]]></category>

		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://muegge.com/blog/?p=65</guid>
		<description><![CDATA[Welcome back!

Last post I talked about using the Log Parser executable from PowerShell. I also briefly mentioned the Log Parser COM component. In this post I will go into more depth on using the COM component from PowerShell.

The COM component exposes a simple object model consisting of only three main objects.


LogQuery – Object used to [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome back!
</p>
<p>Last post I talked about using the Log Parser executable from PowerShell. I also briefly mentioned the Log Parser COM component. In this post I will go into more depth on using the COM component from PowerShell.
</p>
<p>The COM component exposes a simple object model consisting of only three main objects.
</p>
<ul>
<li>LogQuery – Object used to execute queries and batches
</li>
<li>LogRecordSet – Object returned by LogQuery.Execute method
</li>
<li>LogRecord – Child object of LogRecordset. Object returned by  LogRecordSet.getRecord method
</li>
</ul>
<p>
<p>There are also objects containing the input and output formats. There are several of them and they are well documented in the Log Parser documentation. I will give examples as we progress.
</p>
<p>The first step on my task to integrate with the Log Parser COM object was to put together a function library of the basic building blocks.
</p>
<p>The library consists of the following functions.
</p>
<ul>
<li>Get-LPInputFormat – Returns a Log Parser input format object
</li>
<li>Get-LPOutputFormat – Returns a Log Parser output format object
</li>
<li>Invoke-LPExecute – Executes the LogQuery.Execute method and returns a LogRecordSet
</li>
<li>Invoke-LPExecuteBatch – Executes the LogQuery.ExecuteBatch method to output the query in the requested format
</li>
<li>Get-LPRecordSet – Executes a Log Parser Query and returns a LogRecordSet as an array of PowerShell objects
</li>
<li>Get-LPRecord – Returns LogRecord object as a PowerShell object from the current record of a LogRecordSet object
</li>
</ul>
<p>
<p>With these functions we can support almost all of the Log Parser functionality in PowerShell. I did not build in support for the custom COM input type or the NAT and Datagrid output types. The NAT and Datagrid output types can be handled in a different way in PowerShell. The COM input format is a challenge I left for another day.
</p>
<p>Here is the function library.</p>

<div class="wp_syntax"><table><tr><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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;">&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Library name='Muegge_LogParser_Lib.ps1'&gt;</span>
<span style="color: #008000;">### &lt;Author&gt;David Muegge&lt;/Author&gt;</span>
<span style="color: #008000;">### &lt;CreateDate&gt;20081108&lt;/CreateDate&gt;</span>
<span style="color: #008000;">### &lt;ModifiedDate&gt;20081121&lt;/ModifiedDate&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Log Parser function library</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Dependencies&gt;</span>
<span style="color: #008000;">###		Log Parser 2.2 COM component</span>
<span style="color: #008000;">### &lt;/Dependencies&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">### 	Dot source from calling script</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;/Library&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Get-LPInputFormat'&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Returns Log Parser Input Format object based on passed string</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">###		Get-LPInputFormat -InputType &lt;string&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>LPInputFormat<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;"><span style="color: #000080;">$Input</span>Type</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">switch</span><span style="color: #000000;">&#40;</span><span style="color: #800080;"><span style="color: #000080;">$Input</span>Type</span>.ToLower<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #800000;">&quot;ads&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.ADSInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;bin&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.IISBINInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;csv&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.CSVInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;etw&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.ETWInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;evt&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.EventLogInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;fs&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.FileSystemInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;httperr&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.HttpErrorInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;iis&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.IISIISInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;iisodbc&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.IISODBCInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;ncsa&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.IISNCSAInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;netmon&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.NetMonInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;reg&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.RegistryInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;textline&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.TextLineInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;textword&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.TextWordInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;tsv&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.TSVInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;urlscan&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.URLScanLogInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;w3c&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.W3CInputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;xml&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$inputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.XMLInputFormat<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000FF;">return</span> <span style="color: #800080;">$inputobj</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Get-LPOutputFormat'&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Returns Log Parser Output Format object based on passed string</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">###		Get-LPOutputFormat -OutputType &lt;string&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>LPOutputFormat<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">String</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$OutputType</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">switch</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$OutputType</span>.ToLower<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
		<span style="color: #800000;">&quot;csv&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$outputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.CSVOutputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;chart&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$outputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.ChartOutputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;iis&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$outputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.IISOutputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;sql&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$outputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.SQLOutputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;syslog&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$outputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.SYSLOGOutputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;tsv&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$outputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.TSVOutputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;w3c&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$outputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.W3COutputFormat<span style="color: #000000;">&#125;</span>
		<span style="color: #800000;">&quot;tpl&quot;</span><span style="color: #000000;">&#123;</span><span style="color: #800080;">$outputobj</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-comObject</span> MSUtil.LogQuery.TemplateOutputFormat<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0000FF;">return</span> <span style="color: #800080;">$outputobj</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Invoke-LPExecute'&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Executes a Log Parser Query and returns a recordset</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">###		Invoke-LPExecute -query &lt;string&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Invoke<span style="color: pink;">-</span>LPExecute<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$query</span><span style="color: pink;">,</span> <span style="color: #800080;">$inputtype</span><span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #800080;">$LPQuery</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> <span style="color: pink;">-</span>com MSUtil.LogQuery
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$inputtype</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    	<span style="color: #800080;">$LPRecordSet</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LPQuery</span>.Execute<span style="color: #000000;">&#40;</span><span style="color: #800080;">$query</span><span style="color: pink;">,</span> <span style="color: #800080;">$inputtype</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: #800080;">$LPRecordSet</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LPQuery</span>.Execute<span style="color: #000000;">&#40;</span><span style="color: #800080;">$query</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#125;</span>
    <span style="color: #0000FF;">return</span> <span style="color: #800080;">$LPRecordSet</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Invoke-LPExecuteBatch'&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Executes Log Parser batch query with passed input and output types</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">###		Invoke-LPExecuteBatch -query &lt;string&gt; -inputtype &lt;LogParserInputFormat&gt; -outputtype &lt;LogParserOutputFormat&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Invoke<span style="color: pink;">-</span>LPExecuteBatch<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$query</span><span style="color: pink;">,</span> <span style="color: #800080;">$inputtype</span><span style="color: pink;">,</span> <span style="color: #800080;">$outputtype</span><span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #800080;">$LPQuery</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> <span style="color: pink;">-</span>com MSUtil.LogQuery
    <span style="color: #800080;">$result</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LPQuery</span>.ExecuteBatch<span style="color: #000000;">&#40;</span><span style="color: #800080;">$query</span><span style="color: pink;">,</span> <span style="color: #800080;">$inputtype</span><span style="color: pink;">,</span> <span style="color: #800080;">$outputtype</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0000FF;">return</span> <span style="color: #800080;">$result</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Get-LPRecord'&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">###		Returns PowerShell custom object from Log Parser recordset for current record</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">###		Get-LPRecord -rs &lt;RecordSet&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>LPRecord<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$LPRecordSet</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #800080;">$LPRecord</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-Object</span> System.Management.Automation.PSObject
	<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">-not</span> <span style="color: #800080;">$LPRecordSet</span>.atEnd<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$Record</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LPRecordSet</span>.getRecord<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">=</span> 0; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$LPRecordSet</span>.getColumnCount<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>        
			<span style="color: #800080;">$LPRecord</span> | <span style="color: #008080; font-weight: bold;">add-member</span> NoteProperty <span style="color: #800080;">$LPRecordSet</span>.getColumnName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-value</span> <span style="color: #800080;">$Record</span>.getValue<span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0000FF;">return</span> <span style="color: #800080;">$LPRecord</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #008000;">### &lt;Function name='Get-LPRecordSet'&gt;</span>
<span style="color: #008000;">### &lt;Description&gt;</span>
<span style="color: #008000;">### 	Executes a Log Parser Query and returns a LogRecordSet as a custom powershell object</span>
<span style="color: #008000;">### &lt;/Description&gt;</span>
<span style="color: #008000;">### &lt;Usage&gt;</span>
<span style="color: #008000;">###		Get-LPRecordSet -query &lt;string&gt;</span>
<span style="color: #008000;">### &lt;/Usage&gt;</span>
<span style="color: #008000;">### &lt;/Function&gt;</span>
<span style="color: #008000;"># ---------------------------------------------------------------------------</span>
<span style="color: #0000FF;">function</span> Get<span style="color: pink;">-</span>LPRecordSet<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$query</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #008000;"># Execute Query</span>
	<span style="color: #800080;">$LPRecordSet</span> <span style="color: pink;">=</span> Invoke<span style="color: pink;">-</span>LPExecute <span style="color: #800080;">$query</span>
	<span style="color: #800080;">$LPRecords</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> System.Management.Automation.PSObject<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> 0
	<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span>; <span style="color: #FF0000;">-not</span> <span style="color: #800080;">$LPRecordSet</span>.atEnd<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #800080;">$LPRecordSet</span>.moveNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008000;"># Add record</span>
		<span style="color: #800080;">$LPRecord</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>LPRecord<span style="color: #000000;">&#40;</span><span style="color: #800080;">$LPRecordSet</span><span style="color: #000000;">&#41;</span>
		<span style="color: #800080;">$LPRecords</span> <span style="color: pink;">+=</span> <span style="color: #008080; font-weight: bold;">new-Object</span> System.Management.Automation.PSObject	
        <span style="color: #800080;">$RecordCount</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LPQueryResult</span>.length<span style="color: pink;">-</span><span style="color: #000000;">1</span>
        <span style="color: #800080;">$LPRecords</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$RecordCount</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$LPRecord</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #800080;">$LPRecordSet</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #0000FF;">return</span> <span style="color: #800080;">$LPRecords</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>This library provides the basic functionality needed for Log Parser. We can use it in two basic scenarios.
</p>
<p>One - We want to execute a Log Parser batch. This mode works exactly as the Log Parser command line tool works it queries an input file of a given type and writes the results to an output file of a given type.
</p>
<p>Two - We want the results of a Log Parser query returned to a PowerShell object. This will allow us to further process the results using PowerShell or simply utilize the output facilities to display the results.
</p>
<p>Example of Scenario One:
</p>

<div class="wp_syntax"><table><tr><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"><pre class="powershell powershell" style="font-family:monospace;">&nbsp;
. .\Muegge_LogParser_Lib.ps1
&nbsp;
<span style="color: #800080;">$infile</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;D:\Temp\LPTest.log&quot;</span>
<span style="color: #800080;">$outfile</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;D:\Temp\LPTest.gif&quot;</span>
&nbsp;
<span style="color: #008000;"># Set input format and parameters</span>
<span style="color: #800080;">$inputformat</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>LPInputFormat <span style="color: #800000;">&quot;w3c&quot;</span>
&nbsp;
<span style="color: #008000;"># Set output format and parameters</span>
<span style="color: #800080;">$outputformat</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>LPOutputFormat <span style="color: #800000;">&quot;chart&quot;</span>
<span style="color: #800080;">$outputformat</span>.chartTitle <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Top 10 Pages&quot;</span>
<span style="color: #800080;">$outputformat</span>.chartType <span style="color: pink;">=</span> <span style="color: #800000;">&quot;BarStacked&quot;</span>
<span style="color: #800080;">$outputformat</span>.groupSize <span style="color: pink;">=</span> <span style="color: #800000;">&quot;600x400&quot;</span>
&nbsp;
<span style="color: #800080;">$query</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #800000;">&quot;
SELECT Top 10 cs-uri-stem, Count(*)
INTO $outfile
FROM $infile
Group By cs-uri-stem
Order by cs-uri-stem desc	
&quot;</span><span style="color: pink;">@</span>
&nbsp;
<span style="color: #008000;"># Execute batch</span>
Invoke<span style="color: pink;">-</span>LPExecuteBatch <span style="color: #800080;">$query</span> <span style="color: #800080;">$inputformat</span> <span style="color: #800080;">$outputformat</span> | <span style="color: #008080; font-weight: bold;">Out-Null</span></pre></td></tr></table></div>

<p>The code above provides a good way to create scheduled reports. The syntax is easier to follow than the command line switches at least for me.
</p>
<p>Here is the sample output chart.<br/><br />
<img alt="" src="http://muegge.com/images/blogimages/lptest.gif" class="alignnone" width="600" height="400" />
	</p>
<p>Yes, I took this log from a box with nothing but hacker traffic <span style="font-family:Wingdings">J</span>
	</p>
<p>Now let&#8217;s look at the second scenario. We will return the results of a Log Parser query in a PSObject.
</p>
<p>Here we are querying for application hang events in the application event log. We will use a Log Parser query to retrieve just the events we want. Then we will use PowerShell to filter out just the events for IE. The we can easily display the output any way we like.
</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;">&nbsp;
. .\Muegge_LogParser_Lib.ps1
&nbsp;
<span style="color: #800080;">$query</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #800000;">&quot;
SELECT *
FROM Application
WHERE EventID = 1002
&quot;</span><span style="color: pink;">@</span>
&nbsp;
<span style="color: #800080;">$inputformat</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>LPInputFormat <span style="color: #800000;">&quot;evt&quot;</span>
<span style="color: #800080;">$records</span> <span style="color: pink;">=</span> Get<span style="color: pink;">-</span>LPRecordSet <span style="color: #800080;">$query</span> <span style="color: #800080;">$inputformat</span>
<span style="color: #800080;">$records</span> | <span style="color: #0000FF;">where</span> <span style="color: #000000;">&#123;</span><span style="color: #800080;"><span style="color: #000080;">$_</span></span>.Strings <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*iexplore.exe*&quot;</span><span style="color: #000000;">&#125;</span> | <span style="color: #008080; font-weight: bold;">ft</span> <span style="color: #008080; font-style: italic;">-property</span> TimeGenerated<span style="color: pink;">,</span>SourceName<span style="color: pink;">,</span>Strings</pre></td></tr></table></div>

<p>Here is the output.</p>
<pre>
TimeGenerated				SourceName   			Strings
-------------     				----------       			-------
11/1/2008 11:21:02 PM			Application Hang			iexplore.exe|7.0.6000.16735|hungapp|0.0...
11/2/2008 6:25:35 PM			Application Hang			iexplore.exe|7.0.6000.16735|hungapp|0.0...
11/4/2008 7:32:35 AM			Application Hang			iexplore.exe|7.0.6000.16735|hungapp|0.0...
11/6/2008 4:21:22 PM			Application Hang			iexplore.exe|7.0.6000.16735|hungapp|0.0...
11/8/2008 7:05:20 PM			Application Hang			iexplore.exe|7.0.6000.16735|hungapp|0.0...
11/10/2008 3:01:45 PM			Application Hang			iexplore.exe|7.0.6000.16735|hungapp|0.0...
11/11/2008 5:10:40 PM			Application Hang			iexplore.exe|7.0.6000.16735|hungapp|0.0...
11/14/2008 12:13:06 PM			Application Hang			iexplore.exe|7.0.6000.16735|hungapp|0.0...
11/17/2008 7:34:29 AM			Application Hang			iexplore.exe|7.0.6000.16735|hungapp|0.0...
</pre>
<p> </p>
<p>To me this really seems like the best of both worlds utilizing each tool for its strength.
</p>
<p>Now I just have to figure out why IE hangs <span style="font-family:Wingdings">J</span>
	</p>
<p>Best Regards,
</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://muegge.com/blog/?feed=rss2&amp;p=65</wfw:commentRss>
		</item>
		<item>
		<title>Log Parser and PowerShell – Part I</title>
		<link>http://muegge.com/blog/?p=62</link>
		<comments>http://muegge.com/blog/?p=62#comments</comments>
		<pubDate>Wed, 12 Nov 2008 13:33:16 +0000</pubDate>
		<dc:creator>dmuegge</dc:creator>
		
		<category><![CDATA[Log Parser]]></category>

		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://muegge.com/blog/?p=62</guid>
		<description><![CDATA[Welcome!
Log Parser and PowerShell are both great tools and they work well when used together. Yes, you can do pretty much everything Log Parser does with PowerShell alone, but part of PowerShell&#8217;s mission is to better leverage current tools. I believe this is an excellent example. Also it has been my experience Log Parser performs [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome!</p>
<p>Log Parser and PowerShell are both great tools and they work well when used together. Yes, you can do pretty much everything Log Parser does with PowerShell alone, but part of PowerShell&#8217;s mission is to better leverage current tools. I believe this is an excellent example. Also it has been my experience Log Parser performs better at the task. Steve Schofield also blogged about the performance of Log Parser and PowerShell for querying logs <a href="http://weblogs.asp.net/steveschofield/archive/2007/01/25/searching-logfiles-with-powershell-log-parser-findstr-qgrep.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/weblogs.asp.net');">here</a>.</p>
<p>There are two ways to interact with Log Parser from PowerShell. The first and the easiest to get started with is the command line version logparser.exe.</p>
<p>Here is the same command line query example from the last post but with PowerShell.</p>
<p>&amp; ./logparser.exe &#8220;SELECT Top 10 cs-uri-stem, Count(*) FROM D:\Logs\ex081110.log Group By cs-uri-stem Order by cs-uri-stem desc&#8221; –i:w3c</p>
<p>Big difference isn&#8217;t it. <span style="font-family: Wingdings;">J</span></p>
<p>Here is a simple powershell script using Log Parser. This is a little easier to follow than the batch file example in the last post.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="powershell powershell" style="font-family:monospace;"><span style="color: #800080;">$infile</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;D:\Temp\LPTest.log&quot;</span>
&nbsp;
<span style="color: #800080;">$query</span> <span style="color: pink;">=</span> <span style="color: pink;">@</span><span style="color: #800000;">&quot;
SELECT Top 10 cs-uri-stem, Count(*)
FROM $infile
Group By cs-uri-stem
Order by cs-uri-stem desc	
&quot;</span><span style="color: pink;">@</span>
&nbsp;
<span style="color: #800080;">$result</span> <span style="color: pink;">=</span> <span style="color: pink;">&amp;</span> C:\LogParser\logparser.exe <span style="color: #800080;">$query</span> <span style="color: pink;">-</span>i:w3c</pre></td></tr></table></div>

<p>The drawback to these examples is we are not really gaining the full benefit from PowerShell. We could write some functions in PowerShell and convert the text output from Log Parser into PowerShell objects, but there is an easier way to do this.</p>
<p>OK, now for the second way to interact with Log Parser from PowerShell, the Log Parser COM component (logparser.dll). This component installs with Log Parser and should be registered and ready to use if you have installed Log Parser.</p>
<p>The COM component exposes a simple object model consisting of only three main objects. The LogQuery, LogRecorset, and LogRecord objects. There are also a series of objects for the input and output formats.</p>
<p>The COM component is more difficult to use, but is an advantage because data can be returned in object form. We will look at this in detail starting in Log Parser and PowerShell - Part II</p>
<p>Best Regards,</p>
<p>Dave</p>
]]></content:encoded>
			<wfw:commentRss>http://muegge.com/blog/?feed=rss2&amp;p=62</wfw:commentRss>
		</item>
	</channel>
</rss>

