<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Mike Wilbur's Blog</title><subtitle type="html" /><id>http://blogs.msdn.com/mwilbur/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/mwilbur/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2007-02-23T06:42:00Z</updated><entry><title>Renaming a network connection with netsh</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/08/01/renaming-a-network-connection-with-netsh.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/08/01/renaming-a-network-connection-with-netsh.aspx</id><published>2007-08-01T23:14:00Z</published><updated>2007-08-01T23:14:00Z</updated><content type="html">&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Example:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;netsh interface set interface name="Local Area Connection" newname="ExampleLan"&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4175504" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>Powershell Template Engine</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/03/14/powershell-template-engine.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/03/14/powershell-template-engine.aspx</id><published>2007-03-15T08:37:10Z</published><updated>2007-03-15T08:37:10Z</updated><content type="html">&lt;p&gt;A template engine that allows you to have powershell expressions in your templates is a very powerful thing: &lt;a title="http://weblogs.asp.net/soever/archive/2006/12/31/a-templating-engine-using-powershell-expressions.aspx" href="http://weblogs.asp.net/soever/archive/2006/12/31/a-templating-engine-using-powershell-expressions.aspx"&gt;http://weblogs.asp.net/soever/archive/2006/12/31/a-templating-engine-using-powershell-expressions.aspx&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Example Input:&lt;/strong&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Multi Line Expression:&lt;br&gt;[[ get-date&lt;br&gt;1..20&lt;br&gt;]] &lt;p&gt;Define function [[ function get-theanswer {42;} ]]  &lt;p&gt;Single Line: [[ theanswer ]]  &lt;p&gt;EOF&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;strong&gt;Example Output:&lt;/strong&gt;  &lt;blockquote&gt; &lt;p&gt;Multi Line Expression:&lt;br&gt;03/14/2007 22:38:09 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 &lt;p&gt;Define function  &lt;p&gt;Single Line: 42  &lt;p&gt;EOF&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;The implementation uses dynamic code generation to build an assembly in order to have a delegate to pass to&amp;nbsp;regex.replace.&amp;nbsp;  &lt;p&gt;Using code generation felt a bit over complicated, and I wondered if it could be simpler. &lt;p&gt;Some thinking and tinkering later, I arrived at a working implementation.  &lt;p&gt;Getting the multiline regex expression working correctly caused me some difficulty; as it only seems to correctly handle \R as a line separator.  &lt;p&gt;I also added the ability to pipe text to the script, since I like to use the syntax: gc file.txt | expand-template &lt;p&gt;Now as with any powerful tool you'll need to be careful, and be sure that you fully trust the template file (other wise you'll be vulnerable to a code injection attack) &lt;p&gt;&lt;strong&gt;Expand-Template.ps1:&lt;/strong&gt; &lt;/p&gt;&lt;pre class="code" style="overflow: auto"&gt;param (&lt;span style="color: #35687d"&gt;$text&lt;/span&gt;=&lt;span style="color: maroon"&gt;""&lt;/span&gt;,&lt;span style="color: #35687d"&gt;$BeginTag&lt;/span&gt;=&lt;span style="color: maroon"&gt;'[['&lt;/span&gt;,&lt;span style="color: #35687d"&gt;$EndTag&lt;/span&gt;=&lt;span style="color: maroon"&gt;']]'&lt;/span&gt;) 
&lt;span style="color: #35687d"&gt;$begintag&lt;/span&gt; = [regex]::escape(&lt;span style="color: #35687d"&gt;$begintag&lt;/span&gt;) 
&lt;span style="color: #35687d"&gt;$endtag&lt;/span&gt; = [regex]::escape(&lt;span style="color: #35687d"&gt;$endtag&lt;/span&gt;) 
&lt;span style="color: #35687d"&gt;$output&lt;/span&gt;=&lt;span style="color: maroon"&gt;""&lt;/span&gt; 

&lt;span style="color: #35687d"&gt;$input&lt;/span&gt; | %{&lt;span style="color: #35687d"&gt;$text&lt;/span&gt;+=[string]&lt;span style="color: #35687d"&gt;$_&lt;/span&gt; + &lt;span style="color: maroon"&gt;"`r"&lt;/span&gt;} 

&lt;span style="color: blue"&gt;while&lt;/span&gt; (&lt;span style="color: #35687d"&gt;$text&lt;/span&gt; -match &lt;span style="color: maroon"&gt;"(?m)(?&amp;lt;pre&amp;gt;.*?)$begintag(?&amp;lt;exp&amp;gt;.*?)$endtag(?&amp;lt;post&amp;gt;.*)"&lt;/span&gt;) { 
  &lt;span style="color: #35687d"&gt;$text&lt;/span&gt; = &lt;span style="color: #35687d"&gt;$matches&lt;/span&gt;.post 
  &lt;span style="color: #35687d"&gt;$output&lt;/span&gt; += &lt;span style="color: #35687d"&gt;$matches&lt;/span&gt;.pre 
  &lt;span style="color: #35687d"&gt;$output&lt;/span&gt; += &lt;span style="color: #2b91af"&gt;invoke-expression&lt;/span&gt; &lt;span style="color: #35687d"&gt;$matches&lt;/span&gt;.exp 
} 

&lt;span style="color: #35687d"&gt;$output&lt;/span&gt; += &lt;span style="color: #35687d"&gt;$text&lt;/span&gt; 
&lt;span style="color: #35687d"&gt;$output&lt;/span&gt; -replace &lt;span style="color: maroon"&gt;"`r"&lt;/span&gt;, [environment]::newline 
&lt;/pre&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1884968" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>get-SHA256</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/03/14/get-sha256.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/03/14/get-sha256.aspx</id><published>2007-03-15T06:10:49Z</published><updated>2007-03-15T06:10:49Z</updated><content type="html">&lt;p&gt;Get the SHA256 hash in one line of powershell:&lt;/p&gt; &lt;p&gt;function get-sha256 {&lt;br&gt;param($file);[system.bitconverter]::tostring([System.Security.Cryptography.sha256]::create().computehash([system.io.file]::openread((resolve-path $file)))) -replace "-",""&lt;br&gt;}&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1884389" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>get-datatable.ps1</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/03/10/get-datatable-ps1.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/03/10/get-datatable-ps1.aspx</id><published>2007-03-10T23:40:57Z</published><updated>2007-03-10T23:40:57Z</updated><content type="html">&lt;p&gt;get-datatable.ps1 is an improvement on the get-dataset.ps1 from an &lt;a href="http://blogs.msdn.com/mwilbur/archive/2007/02/25/updating-a-dataset-with-powershell-and-saving-changes-back-to-sql.aspx"&gt;earlier post&lt;/a&gt;. Instead of returning a dataset it returns a DataTable; and a UpdateSql method is added to the object returned, so you don't need a separate script to send the changes back to sql.&lt;/p&gt; &lt;p&gt;This allows updates to a small table&amp;nbsp;to look like:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;$t = datatable "select * from stuff" -db foo&lt;br&gt;$t | %{ .... update the data rows ...&amp;nbsp;}&lt;br&gt;$t.UpdateSql()&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Get-DataTable.ps1:&lt;/p&gt; &lt;div class="code" style="overflow: auto"&gt;&lt;pre&gt;param (&lt;span style="color: #35687d"&gt;$sql&lt;/span&gt;,&lt;span style="color: #35687d"&gt;$server&lt;/span&gt;=&lt;span style="color: maroon"&gt;"."&lt;/span&gt;,&lt;span style="color: #35687d"&gt;$db&lt;/span&gt;) 

&lt;span style="color: #35687d"&gt;$connectionstring&lt;/span&gt;= &lt;span style="color: maroon"&gt;"Server=$server;database=$db;trusted_connection=yes;"&lt;/span&gt; 
&lt;span style="color: #35687d"&gt;$dt&lt;/span&gt; = &lt;span style="color: #2b91af"&gt;new-object&lt;/span&gt; data.DataTable 
&lt;span style="color: #35687d"&gt;$da&lt;/span&gt; = &lt;span style="color: #2b91af"&gt;New-Object&lt;/span&gt; system.data.sqlclient.sqldataadapter &lt;span style="color: #35687d"&gt;$sql&lt;/span&gt;, &lt;span style="color: #35687d"&gt;$connectionstring&lt;/span&gt; 
&lt;span style="color: #35687d"&gt;$null&lt;/span&gt; = &lt;span style="color: #35687d"&gt;$da&lt;/span&gt;.Fill(&lt;span style="color: #35687d"&gt;$dt&lt;/span&gt;) 
&lt;span style="color: #35687d"&gt;$dt&lt;/span&gt;.ExtendedProperties[&lt;span style="color: maroon"&gt;"sql"&lt;/span&gt;]= &lt;span style="color: #35687d"&gt;$sql&lt;/span&gt; 
&lt;span style="color: #35687d"&gt;$dt&lt;/span&gt;.ExtendedProperties[&lt;span style="color: maroon"&gt;"connectionstring"&lt;/span&gt;]= &lt;span style="color: #35687d"&gt;$connectionstring&lt;/span&gt; 

&lt;span style="color: #35687d"&gt;$dt&lt;/span&gt; = &lt;span style="color: #2b91af"&gt;add-member&lt;/span&gt; ScriptMethod UpdateSql { 
&lt;span style="color: #35687d"&gt;$da&lt;/span&gt; = &lt;span style="color: #2b91af"&gt;New-Object&lt;/span&gt; system.data.sqlclient.sqldataadapter &lt;span style="color: #35687d"&gt;$this&lt;/span&gt;.ExtendedProperties[&lt;span style="color: maroon"&gt;"sql"&lt;/span&gt;], &lt;span style="color: #35687d"&gt;$this&lt;/span&gt;.ExtendedProperties[&lt;span style="color: maroon"&gt;"connectionstring"&lt;/span&gt;] 

&lt;span style="color: #35687d"&gt;$cb&lt;/span&gt; = &lt;span style="color: #2b91af"&gt;new-object&lt;/span&gt; system.data.sqlclient.sqlcommandbuilder &lt;span style="color: #35687d"&gt;$da&lt;/span&gt; 
&lt;span style="color: #35687d"&gt;$da&lt;/span&gt;.UpdateCommand = &lt;span style="color: #35687d"&gt;$cb&lt;/span&gt;.GetUpdateCommand() 
&lt;span style="color: #35687d"&gt;$da&lt;/span&gt;.InsertCommand = &lt;span style="color: #35687d"&gt;$cb&lt;/span&gt;.GetInsertCommand() 
&lt;span style="color: #35687d"&gt;$da&lt;/span&gt;.DeleteCommand = &lt;span style="color: #35687d"&gt;$cb&lt;/span&gt;.GetDeleteCommand() 
&lt;span style="color: #35687d"&gt;$null&lt;/span&gt; = &lt;span style="color: #35687d"&gt;$da&lt;/span&gt;.Update(&lt;span style="color: #35687d"&gt;$this&lt;/span&gt;) 
} -&lt;span style="color: blue"&gt;in&lt;/span&gt; &lt;span style="color: #35687d"&gt;$dt&lt;/span&gt; -pass 

&lt;span style="color: green"&gt;#return data table in array so table doesn't get decomposed into an array of data rows.&lt;/span&gt; 
,&lt;span style="color: #35687d"&gt;$dt&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1855167" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>Managing Performance Counters via Command Line</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/03/08/managing-performance-counters-via-command-line.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/03/08/managing-performance-counters-via-command-line.aspx</id><published>2007-03-09T08:56:51Z</published><updated>2007-03-09T08:56:51Z</updated><content type="html">&lt;p&gt;When it comes to scripting the setup of performance counters &lt;a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/nt_command_logman.mspx?mfr=true"&gt;Logman&lt;/a&gt; is your friend; and &lt;a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/nt_command_logman.mspx?mfr=true"&gt;Relog&lt;/a&gt; comes in really handy for converting a binary log to SQL log.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1841991" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>Windows Automated Installation Kit (AIK)</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/02/27/windows-automated-installation-kit-aik.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/02/27/windows-automated-installation-kit-aik.aspx</id><published>2007-02-28T08:28:57Z</published><updated>2007-02-28T08:28:57Z</updated><content type="html">&lt;p&gt;It's great to see powerful tools be made&amp;nbsp;publicly available; &lt;a href="http://en.wikipedia.org/wiki/Winpe"&gt;winpe&lt;/a&gt; and &lt;a href="http://technet.microsoft.com/en-us/windowsvista/aa905070.aspx"&gt;imagex&lt;/a&gt; are powerful tools I've been using for some time to deploy systems and recover from hardware &amp;amp; software failures.&lt;/p&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=c7d4bc6d-15f3-4284-9123-679830d629f2&amp;amp;DisplayLang=en"&gt;Windows Automated Installation Kit (AIK)&lt;/a&gt;  &lt;blockquote&gt; &lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;a&gt;&lt;/a&gt;The Windows Automated Installation Kit (Windows AIK) is designed to help corporate IT professionals customize and deploy the Microsoft Windows Vista™ family of operation systems. By using Windows AIK, you can perform unattended Windows installations, capture Windows images with ImageX, and create Windows PE images.&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1772399" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>Updating a DataSet with Powershell and saving changes back to SQL</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/02/25/updating-a-dataset-with-powershell-and-saving-changes-back-to-sql.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/02/25/updating-a-dataset-with-powershell-and-saving-changes-back-to-sql.aspx</id><published>2007-02-26T08:35:00Z</published><updated>2007-02-26T08:35:00Z</updated><content type="html">&lt;P&gt;This example uses the &lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyID=e719ecf7-9f46-4312-af89-6ad8702e4e6e&amp;amp;DisplayLang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?FamilyID=e719ecf7-9f46-4312-af89-6ad8702e4e6e&amp;amp;DisplayLang=en"&gt;AdventureWorks sample&lt;/A&gt; database.&lt;/P&gt;
&lt;DIV class=code style="OVERFLOW: auto"&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: green"&gt;# get the dataset&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #2b91af"&gt;PS&lt;/SPAN&gt; C:\demo&amp;gt; &lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt; = .\get-dataset.ps1 &lt;SPAN style="COLOR: maroon"&gt;"select * from Production.ProductModel"&lt;/SPAN&gt; -db adventureworks 
&lt;SPAN style="COLOR: green"&gt;# write out xml from the instructions column into seperate files&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #2b91af"&gt;PS&lt;/SPAN&gt; C:\demo&amp;gt; &lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt;.Tables[&lt;SPAN style="COLOR: maroon"&gt;0&lt;/SPAN&gt;] | ?{&lt;SPAN style="COLOR: #35687d"&gt;$_&lt;/SPAN&gt;[&lt;SPAN style="COLOR: maroon"&gt;"instructions"&lt;/SPAN&gt;] -ne [dbnull]::value } | %{&lt;SPAN style="COLOR: #35687d"&gt;$_&lt;/SPAN&gt;[&lt;SPAN style="COLOR: maroon"&gt;"instructions"&lt;/SPAN&gt;]| &lt;SPAN style="COLOR: #2b91af"&gt;sc&lt;/SPAN&gt; &lt;SPAN style="COLOR: maroon"&gt;"productmodel-$($_['productmodelid']).xml"&lt;/SPAN&gt;} 
&lt;SPAN style="COLOR: green"&gt;# verify output&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #2b91af"&gt;PS&lt;/SPAN&gt; C:\demo&amp;gt; &lt;SPAN style="COLOR: #2b91af"&gt;ls&lt;/SPAN&gt; productmodel-* 

    Directory: Microsoft.PowerShell.Core\FileSystem::C:\demo

Mode                LastWriteTime     Length Name   
----                -------------     ------ ----                                                                  
-a---         2/25/2007   8:47 PM       5567 productmodel-10.xml
-a---         2/25/2007   8:47 PM       2142 productmodel-43.xml
-a---         2/25/2007   8:47 PM       1967 productmodel-44.xml
-a---         2/25/2007   8:47 PM       4051 productmodel-47.xml
-a---         2/25/2007   8:47 PM       4078 productmodel-48.xml
-a---         2/25/2007   8:47 PM       1927 productmodel-53.xml
-a---         2/25/2007   8:47 PM       1565 productmodel-66.xml
-a---         2/25/2007   8:47 PM       1572 productmodel-67.xml
-a---         2/25/2007   8:47 PM       5340 productmodel-7.xml

&lt;SPAN style="COLOR: green"&gt;# now lets make some xml files for product model 1-5&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #2b91af"&gt;PS&lt;/SPAN&gt; C:\demo&amp;gt; &lt;SPAN style="COLOR: maroon"&gt;1&lt;/SPAN&gt;.&lt;SPAN style="COLOR: maroon"&gt;.5&lt;/SPAN&gt; | %{&lt;SPAN style="COLOR: #2b91af"&gt;cp&lt;/SPAN&gt; productmodel-&lt;SPAN style="COLOR: maroon"&gt;10&lt;/SPAN&gt;.xml productmodel-&lt;SPAN style="COLOR: #35687d"&gt;$_&lt;/SPAN&gt;.xml} 
&lt;SPAN style="COLOR: green"&gt;# Update the dataset with the new xml content from the files&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #2b91af"&gt;PS&lt;/SPAN&gt; C:\demo&amp;gt; &lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt;.Tables[&lt;SPAN style="COLOR: maroon"&gt;0&lt;/SPAN&gt;] | %{&lt;SPAN style="COLOR: #35687d"&gt;$file&lt;/SPAN&gt; = &lt;SPAN style="COLOR: maroon"&gt;"productmodel-$($_['productmodelid']).xml"&lt;/SPAN&gt;;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;test-path&lt;/SPAN&gt; &lt;SPAN style="COLOR: #35687d"&gt;$file&lt;/SPAN&gt;) {&lt;SPAN style="COLOR: #35687d"&gt;$_&lt;/SPAN&gt;[&lt;SPAN style="COLOR: maroon"&gt;"instructions"&lt;/SPAN&gt;] = [string]::join("`r`n",(&lt;SPAN style="COLOR: #2b91af"&gt;gc&lt;/SPAN&gt; &lt;SPAN style="COLOR: #35687d"&gt;$file&lt;/SPAN&gt;))}} 
&lt;SPAN style="COLOR: green"&gt;# save changes back to SQL&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #2b91af"&gt;PS&lt;/SPAN&gt; C:\demo&amp;gt; .\save-datasetchanges.ps1 &lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt; 
&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Get-DataSet.ps1 stores the sql command and connection string used to create the dataset as extended properties on the dataset. This is used by Save-DataSetChanges.ps1 to construct a SqlCommandBuilder object;&amp;nbsp;which is then used&amp;nbsp;to create the update/insert/delete command objects&amp;nbsp;and update the SQL Server using a SqlDataAdapter.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;get-dataset.ps1&lt;/STRONG&gt;:&lt;/P&gt;&lt;PRE class=code style="OVERFLOW: auto"&gt;param (&lt;SPAN style="COLOR: #35687d"&gt;$sql&lt;/SPAN&gt;,&lt;SPAN style="COLOR: #35687d"&gt;$server&lt;/SPAN&gt;=&lt;SPAN style="COLOR: maroon"&gt;"."&lt;/SPAN&gt;,&lt;SPAN style="COLOR: #35687d"&gt;$db&lt;/SPAN&gt;) 

&lt;SPAN style="COLOR: #35687d"&gt;$connectionstring&lt;/SPAN&gt;= &lt;SPAN style="COLOR: maroon"&gt;"Server=$server;database=$db;trusted_connection=yes;"&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt; = &lt;SPAN style="COLOR: #2b91af"&gt;new-object&lt;/SPAN&gt; data.dataset 
&lt;SPAN style="COLOR: #35687d"&gt;$da&lt;/SPAN&gt; = &lt;SPAN style="COLOR: #2b91af"&gt;New-Object&lt;/SPAN&gt; system.data.sqlclient.sqldataadapter &lt;SPAN style="COLOR: #35687d"&gt;$sql&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #35687d"&gt;$connectionstring&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #35687d"&gt;$null&lt;/SPAN&gt; = &lt;SPAN style="COLOR: #35687d"&gt;$da&lt;/SPAN&gt;.Fill(&lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt;) 
&lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt;.ExtendedProperties[&lt;SPAN style="COLOR: maroon"&gt;"sql"&lt;/SPAN&gt;]= &lt;SPAN style="COLOR: #35687d"&gt;$sql&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt;.ExtendedProperties[&lt;SPAN style="COLOR: maroon"&gt;"connectionstring"&lt;/SPAN&gt;]= &lt;SPAN style="COLOR: #35687d"&gt;$connectionstring&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;save-datasetchanges.ps1&lt;/STRONG&gt;:&lt;/P&gt;&lt;PRE class=code style="OVERFLOW: auto"&gt;param ([system.data.dataset]&lt;SPAN style="COLOR: #35687d"&gt;$dataset&lt;/SPAN&gt;)  

&lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt; = &lt;SPAN style="COLOR: #35687d"&gt;$dataset&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #35687d"&gt;$da&lt;/SPAN&gt; = &lt;SPAN style="COLOR: #2b91af"&gt;New-Object&lt;/SPAN&gt; system.data.sqlclient.sqldataadapter &lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt;.ExtendedProperties[&lt;SPAN style="COLOR: maroon"&gt;"sql"&lt;/SPAN&gt;], &lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt;.ExtendedProperties[&lt;SPAN style="COLOR: maroon"&gt;"connectionstring"&lt;/SPAN&gt;] 

&lt;SPAN style="COLOR: #35687d"&gt;$cb&lt;/SPAN&gt; = &lt;SPAN style="COLOR: #2b91af"&gt;new-object&lt;/SPAN&gt; system.data.sqlclient.sqlcommandbuilder &lt;SPAN style="COLOR: #35687d"&gt;$da&lt;/SPAN&gt; 
&lt;SPAN style="COLOR: #35687d"&gt;$da&lt;/SPAN&gt;.UpdateCommand = &lt;SPAN style="COLOR: #35687d"&gt;$cb&lt;/SPAN&gt;.GetUpdateCommand() 
&lt;SPAN style="COLOR: #35687d"&gt;$da&lt;/SPAN&gt;.InsertCommand = &lt;SPAN style="COLOR: #35687d"&gt;$cb&lt;/SPAN&gt;.GetInsertCommand() 
&lt;SPAN style="COLOR: #35687d"&gt;$da&lt;/SPAN&gt;.DeleteCommand = &lt;SPAN style="COLOR: #35687d"&gt;$cb&lt;/SPAN&gt;.GetDeleteCommand() 
&lt;SPAN style="COLOR: #35687d"&gt;$null&lt;/SPAN&gt; = &lt;SPAN style="COLOR: #35687d"&gt;$da&lt;/SPAN&gt;.Update(&lt;SPAN style="COLOR: #35687d"&gt;$ds&lt;/SPAN&gt;) 
&lt;/PRE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1760795" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>Capturing SQL Generated by SMO in Powershell</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/02/24/capturing-sql-generated-by-smo-in-powershell.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/02/24/capturing-sql-generated-by-smo-in-powershell.aspx</id><published>2007-02-25T06:30:00Z</published><updated>2007-02-25T06:30:00Z</updated><content type="html">&lt;p&gt;The following example uses the get-sqlserver.ps1 script in my search path.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;get-sqlserver.ps1:&lt;br&gt;&lt;/strong&gt;&lt;/p&gt; &lt;div class="code" style="overflow: auto"&gt;&lt;pre&gt;param (&lt;span style="color: #35687d"&gt;$server&lt;/span&gt;=&lt;span style="color: maroon"&gt;"."&lt;/span&gt;)  
&lt;span style="color: #35687d"&gt;$null&lt;/span&gt; = [reflection.assembly]::loadwithpartialname(&lt;span style="color: maroon"&gt;"microsoft.sqlserver.smo"&lt;/span&gt;) 
&lt;span style="color: #2b91af"&gt;new-object&lt;/span&gt; Microsoft.SqlServer.Management.Smo.Server &lt;span style="color: #35687d"&gt;$server&lt;/span&gt;;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This example will shows how to use SMO objects to generate a SQL Script that will drop all the rowguid columns from the adventure works database:&lt;/p&gt;
&lt;div class="code" style="overflow: auto"&gt;&lt;pre&gt;&lt;span style="color: #2b91af"&gt;PS&lt;/span&gt; C:\demo&amp;gt; &lt;span style="color: #35687d"&gt;$srv&lt;/span&gt; = get-sqlserver  
&lt;span style="color: #2b91af"&gt;PS&lt;/span&gt; C:\demo&amp;gt; &lt;span style="color: #35687d"&gt;$db&lt;/span&gt; = &lt;span style="color: #35687d"&gt;$srv&lt;/span&gt;.Databases[&lt;span style="color: maroon"&gt;"adventureworks"&lt;/span&gt;] 
&lt;span style="color: green"&gt;# Find the tables that have a rowguid column&lt;/span&gt; 
&lt;span style="color: #2b91af"&gt;PS&lt;/span&gt; C:\demo&amp;gt; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: #35687d"&gt;$tbl&lt;/span&gt; &lt;span style="color: blue"&gt;in&lt;/span&gt; &lt;span style="color: #35687d"&gt;$db&lt;/span&gt;.tables) {&lt;span style="color: #35687d"&gt;$tbl&lt;/span&gt;.columns | %{ &lt;span style="color: blue"&gt;if&lt;/span&gt;(&lt;span style="color: #35687d"&gt;$_&lt;/span&gt;.name -eq &lt;span style="color: maroon"&gt;"rowguid"&lt;/span&gt;) {&lt;span style="color: #35687d"&gt;$tbl&lt;/span&gt;.name}}} 
Employee 
EmployeeAddress 
Address 
AddressType 
Contact 
StateProvince 
Product 
... 

&lt;span style="color: green"&gt;# set SMO connection to capture mode&lt;/span&gt; 
&lt;span style="color: #2b91af"&gt;PS&lt;/span&gt; C:\demo&amp;gt; &lt;span style="color: #35687d"&gt;$srv&lt;/span&gt;.ConnectionContext.SqlExecutionModes = [Microsoft.SqlServer.Management.Common.SqlExecutionModes]::capturesql 

&lt;span style="color: green"&gt;# drop the row guid column &amp;amp; alter the table &lt;/span&gt; 
&lt;span style="color: green"&gt;#  SMO is set to capture only so the database is not updated&lt;/span&gt; 
&lt;span style="color: #2b91af"&gt;PS&lt;/span&gt; C:\demo&amp;gt; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: #35687d"&gt;$tbl&lt;/span&gt; &lt;span style="color: blue"&gt;in&lt;/span&gt; &lt;span style="color: #35687d"&gt;$db&lt;/span&gt;.tables) {&lt;span style="color: #35687d"&gt;$tbl&lt;/span&gt;.columns | %{ &lt;span style="color: blue"&gt;if&lt;/span&gt;(&lt;span style="color: #35687d"&gt;$_&lt;/span&gt;.name -eq &lt;span style="color: maroon"&gt;"rowguid"&lt;/span&gt;) {&lt;span style="color: #35687d"&gt;$_&lt;/span&gt;.drop();&lt;span style="color: #35687d"&gt;$tbl&lt;/span&gt;.alter();}}} 
&lt;span style="color: #2b91af"&gt;&lt;span style="color: green"&gt;&lt;br&gt;# Check the captured sql&lt;br&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;PS&lt;/span&gt; C:\demo&amp;gt; &lt;span style="color: #35687d"&gt;$srv&lt;/span&gt;.ConnectionContext.CapturedSql.Text 
USE [adventureworks] 
ALTER TABLE [HumanResources].[Employee] DROP CONSTRAINT [DF_Employee_rowguid] 
ALTER TABLE [HumanResources].[Employee] DROP COLUMN [rowguid] 
... 

&lt;span style="color: green"&gt;# save the sql to a file&lt;/span&gt; 
&lt;span style="color: #2b91af"&gt;PS&lt;/span&gt; C:\demo&amp;gt; &lt;span style="color: #35687d"&gt;$srv&lt;/span&gt;.ConnectionContext.CapturedSql.Text | &lt;span style="color: #2b91af"&gt;sc&lt;/span&gt; update.sql 

&lt;/pre&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1755549" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>EventTriggers</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/02/24/eventtriggers.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/02/24/eventtriggers.aspx</id><published>2007-02-24T23:51:31Z</published><updated>2007-02-24T23:51:31Z</updated><content type="html">&lt;p&gt;&lt;/p&gt; &lt;p&gt;This could be usefull; I always like knowing about obscure commands that are on every box I work with. &lt;p&gt;&lt;b&gt;eventtriggers&lt;/b&gt;[&lt;b&gt;.exe&lt;/b&gt;] &lt;b&gt;/create &lt;/b&gt;[&lt;b&gt;/s &lt;/b&gt;&lt;i&gt;Computer&lt;/i&gt; [&lt;b&gt;/u &lt;/b&gt;&lt;i&gt;Domain&lt;/i&gt;&lt;b&gt;\&lt;/b&gt;&lt;i&gt;User&lt;/i&gt; [&lt;b&gt;/p &lt;/b&gt;&lt;i&gt;Password&lt;/i&gt;]]] &lt;b&gt;/tr &lt;/b&gt;&lt;i&gt;TriggerName &lt;/i&gt;[&lt;b&gt;/l&lt;/b&gt; [&lt;b&gt;APPLICATION&lt;/b&gt;] [&lt;b&gt;SYSTEM&lt;/b&gt;] [&lt;b&gt;SECURITY&lt;/b&gt;] [&lt;b&gt;"DNS&amp;nbsp;Server"&lt;/b&gt;] [&lt;b&gt;LOG&lt;/b&gt;] [&lt;i&gt;DirectoryLogName&lt;/i&gt;] [&lt;b&gt;*&lt;/b&gt;]] {[&lt;b&gt;/eid &lt;/b&gt;&lt;i&gt;ID&lt;/i&gt;] | [&lt;b&gt;/t&lt;/b&gt; {&lt;b&gt;ERROR&lt;/b&gt; | &lt;b&gt;INFORMATION&lt;/b&gt; | &lt;b&gt;WARNING&lt;/b&gt; | &lt;b&gt;SUCCESSAUDIT&lt;/b&gt; | &lt;b&gt;FAILUREAUDIT&lt;/b&gt;}] | [&lt;b&gt;/so &lt;/b&gt;&lt;i&gt;Source&lt;/i&gt;]} [&lt;b&gt;/d &lt;/b&gt;&lt;i&gt;Description&lt;/i&gt;] &lt;b&gt;/tk &lt;/b&gt;&lt;i&gt;TaskName&lt;/i&gt;  &lt;p&gt;&lt;strong&gt;eventtriggers create:&lt;br&gt;&lt;/strong&gt;This command creates a new event trigger that monitors and acts upon the occurrence of log events of given criteria.&lt;/p&gt; &lt;p&gt;&lt;a title="http://technet2.microsoft.com/WindowsServer/en/library/e33bcf4c-dece-4b47-9bb7-31ecfcbc76d51033.mspx?mfr=true" href="http://technet2.microsoft.com/WindowsServer/en/library/e33bcf4c-dece-4b47-9bb7-31ecfcbc76d51033.mspx?mfr=true"&gt;http://technet2.microsoft.com/WindowsServer/en/library/e33bcf4c-dece-4b47-9bb7-31ecfcbc76d51033.mspx?mfr=true&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1754092" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>Connect to and Shadow the Console Session with Terminal Services</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/02/24/connect-to-and-shadow-the-console-session-with-terminal-services.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/02/24/connect-to-and-shadow-the-console-session-with-terminal-services.aspx</id><published>2007-02-24T23:44:07Z</published><updated>2007-02-24T23:44:07Z</updated><content type="html">&lt;p&gt;&lt;/p&gt; &lt;p&gt;This KB article shows how you have two separate TS sessions connected to the console:&lt;/p&gt;&lt;a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;278845"&gt;How to Connect to and Shadow the Console Session with Windows Server 2003 Terminal Services&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1754058" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>Remote Desktop shortcut keys</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/02/24/remote-desktop-shortcut-keys.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/02/24/remote-desktop-shortcut-keys.aspx</id><published>2007-02-24T23:20:00Z</published><updated>2007-02-24T23:20:00Z</updated><content type="html">Remote Desktop has some really handy shortcuts that are documented right there in the help file:&lt;BR&gt;
&lt;TABLE class=""&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class=""&gt;&lt;STRONG&gt;Shortcut key&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;STRONG&gt;Description&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;ALT+PAGE UP&lt;/TD&gt;
&lt;TD class=""&gt;Switches between programs from left to right. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;ALT+PAGE DOWN&lt;/TD&gt;
&lt;TD class=""&gt;Switches between programs from right to left.&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;ALT+INSERT &lt;/TD&gt;
&lt;TD class=""&gt;Cycles through the programs in the order they were started. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;ALT+HOME&lt;/TD&gt;
&lt;TD class=""&gt;Displays the Start menu. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;CTRL+ALT+BREAK &lt;/TD&gt;
&lt;TD class=""&gt;Switches the client between a window and full screen.&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;CTRL+ALT+END&lt;/TD&gt;
&lt;TD class=""&gt;Brings up the Windows Security dialog box.&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;ALT+DELETE&lt;/TD&gt;
&lt;TD class=""&gt;Displays the Windows menu. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;CTRL+ALT+Minus (-) symbol on the numeric keypad&lt;/TD&gt;
&lt;TD class=""&gt;Places a snapshot of the active window, within the client, on the Terminal server clipboard (provides the same functionality as pressing PrintScrn on a local computer.) &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=""&gt;CTRL+ALT+Plus (+) symbol on the numeric keypad &lt;/TD&gt;
&lt;TD class=""&gt;Places a snapshot of the entire client window area on the Terminal server clipboard (provides the same functionality as pressing ALT+PrintScrn on a local computer.) &lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1754011" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author></entry><entry><title>Powershell script that can schedule itself to run later</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/02/23/powershell-script-that-can-schedule-itself-to-run-later.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/02/23/powershell-script-that-can-schedule-itself-to-run-later.aspx</id><published>2007-02-23T18:08:00Z</published><updated>2007-02-23T18:08:00Z</updated><content type="html">&lt;P&gt;From time to time I find it useful to schedule tasks to run on my machine. I often realize that many people are unaware that windows 2003 has a built in task scheduler; and even more of a rarity for them to be aware it can be managed via the command line.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Enter SCHTASKS &lt;A href="http://support.microsoft.com/kb/814596/" mce_href="http://support.microsoft.com/kb/814596/"&gt;http://support.microsoft.com/kb/814596/&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;In the past I would crate a batch file to create the schedules; just to make it easy and repeatable. Having the setup separate from the batch file that does the work always bugged me.&lt;/P&gt;
&lt;P&gt;The other day I when I found the need to have a task run every five minutes, I did it in powershell.&amp;nbsp; I thought to myself hey this is powershell, we’ve got some more power here; what if this script could schedule it’s self? And that’s how this idea was born.&lt;/P&gt;
&lt;P&gt;When run without parameters it executes normaly.&lt;BR&gt;Specify the -schedule switch to have it schedule itself.&lt;BR&gt;For details on schtasks options see: &lt;A href="http://support.microsoft.com/kb/814596/" mce_href="http://support.microsoft.com/kb/814596/"&gt;http://support.microsoft.com/kb/814596/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Selfscheduling.ps1:&lt;BR&gt;&lt;/STRONG&gt;
&lt;DIV class=code&gt;param ([switch] $schedule) 
&lt;P&gt;if ($schedule) {&lt;BR&gt;$taskname = $myinvocation.mycommand.definition -replace '\W','_'&lt;BR&gt;schtasks /create&amp;nbsp; /sc MINUTE /MO 5 /tn "$taskname" /tr "powershell -c $($myinvocation.mycommand.definition)" &lt;BR&gt;return;&lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;#switch to directory where the script lives&lt;BR&gt;pushd (split-path -parent $myinvocation.mycommand.definition)&lt;/P&gt;
&lt;P&gt;#insert useful code here&lt;BR&gt;write-host "Hello World"&lt;/P&gt;
&lt;P&gt;popd&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1745753" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author><category term="Powershell" scheme="http://blogs.msdn.com/mwilbur/archive/tags/Powershell/default.aspx" /></entry><entry><title>Powershell 1 liner: Key Value pairs from text file to hashtable</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/mwilbur/archive/2007/02/23/powershell-1-liner-key-values-pairs-from-text-file-to-hashtable.aspx" /><id>http://blogs.msdn.com/mwilbur/archive/2007/02/23/powershell-1-liner-key-values-pairs-from-text-file-to-hashtable.aspx</id><published>2007-02-23T09:42:00Z</published><updated>2007-02-23T09:42:00Z</updated><content type="html">&lt;P&gt;&lt;STRONG&gt;Example Problem:&lt;/STRONG&gt;&lt;BR&gt;You have a text file that contains key value pair and you want to use them as a hash table in powershell.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Values.txt:&lt;BR&gt;&lt;/STRONG&gt;key1=value1&lt;BR&gt;key2=value2&lt;BR&gt;key3=value42&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;One way to do it:&lt;BR&gt;&lt;/STRONG&gt;PS C:\temp&amp;gt; gc values.txt | %{$h = @{}} {if ($_ -match "(.*)=(.*)") {$h[$matches[1]]=$matches[2];}}&lt;/P&gt;
&lt;P&gt;PS C:\temp&amp;gt; $h&lt;/P&gt;&lt;PRE&gt;Name                           Value
----                           -----
key3                           value42
key2                           value2
key1                           value1
&lt;/PRE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1745712" width="1" height="1"&gt;</content><author><name>mwilbur</name><uri>http://blogs.msdn.com/members/mwilbur.aspx</uri></author><category term="Powershell" scheme="http://blogs.msdn.com/mwilbur/archive/tags/Powershell/default.aspx" /></entry></feed>