<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.technet.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>BATCHman Writes a PowerShell Script to Automate Handle</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2011/09/17/batchman-writes-a-powershell-script-to-automate-handle.aspx</link><description>Summary : Windows PowerShell superhero BATCHman writes a script to automate the Sysinternals Handle tool.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: BATCHman Writes a PowerShell Script to Automate Handle</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2011/09/17/batchman-writes-a-powershell-script-to-automate-handle.aspx#3454109</link><pubDate>Mon, 19 Sep 2011 09:42:08 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3454109</guid><dc:creator>Klaus Schulte</dc:creator><description>&lt;p&gt;... another interesting variant is this here:&lt;/p&gt;
&lt;p&gt;(.\handle\handle.exe $name) -match &amp;#39;pid:&amp;#39; | &lt;/p&gt;
&lt;p&gt;% {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;$processId,$fileHandle = ($_ -split &amp;#39;[:\s]+&amp;#39;)[2,5] &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;.\handle\handle.exe -c $fileHandle -p $processId -y&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;We call handle, look for lines containing the keyword &amp;#39;pid:&amp;#39; and pipe them to the split operator using the regexp &amp;#39;[:\s]&amp;#39; to split each line at a colon or whitespace.&lt;/p&gt;
&lt;p&gt;If we rely ( and we have to! ) the fact that the pid is located in part 2 and the handle in part5 of the splitted line, you can use this information to close the handles!&lt;/p&gt;
&lt;p&gt;Klaus.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3454109" width="1" height="1"&gt;</description></item><item><title>re: BATCHman Writes a PowerShell Script to Automate Handle</title><link>http://blogs.technet.com/b/heyscriptingguy/archive/2011/09/17/batchman-writes-a-powershell-script-to-automate-handle.aspx#3453943</link><pubDate>Sat, 17 Sep 2011 20:20:12 GMT</pubDate><guid isPermaLink="false">d5e57398-b9ef-4490-9955-07cbb4e4a80d:3453943</guid><dc:creator>KLaus Schulte</dc:creator><description>&lt;p&gt;BATCHman is back and saved us again!&lt;/p&gt;
&lt;p&gt;GREAT to know that he and his regex helped to &amp;quot;handle&amp;quot; this case!&lt;/p&gt;
&lt;p&gt;Well done, BATCHman!!&lt;/p&gt;
&lt;p&gt;As you know, I like regular expressions which is bad luck because I will add something else here :-)&lt;/p&gt;
&lt;p&gt;So .... there is onother approach to solve this case which might be a little more &amp;quot;straight&amp;quot; .. !?&lt;/p&gt;
&lt;p&gt;If we extract the lines conataining the interesting information and use the replace operator to get&lt;/p&gt;
&lt;p&gt;rid of all other information except the pid and handle,&lt;/p&gt;
&lt;p&gt;we can reduce the calculations of substrings and have a shorter form like this:&lt;/p&gt;
&lt;p&gt;$commands = ((c:\tools\handle total) -match &amp;#39;pid&amp;#39;) -replace &amp;nbsp;&amp;#39;^.*pid:\s*([0-9]+).*File\s*([0-9A-F]+).*$&amp;#39;, &amp;#39;c:\tools\handle.exe -c $2 -p $1 -y&amp;#39;&lt;/p&gt;
&lt;p&gt;This returns an array of commands that look like that&lt;/p&gt;
&lt;p&gt;c:\tools\handle.exe -c 210 -p 6380 -y&lt;/p&gt;
&lt;p&gt;If we pass them to Invoke-Expression, the handles are closed!&lt;/p&gt;
&lt;p&gt;The first part: ((c:\tools\handle total) -match &amp;#39;pid&amp;#39;) reduces handle&amp;#39;s output to the interesting lines,&lt;/p&gt;
&lt;p&gt;those that contain the word &amp;#39;pid&amp;#39;.&lt;/p&gt;
&lt;p&gt;The result is passed to replace, which skips the beginning &amp;#39;^.*&amp;#39; and end &amp;#39;.*$&amp;#39; of the line, &lt;/p&gt;
&lt;p&gt;looks for &amp;#39;pid:&amp;#39; followed by whitespace and captures the part in round braces: &amp;#39;([0-9]+)&amp;#39; which is the pid number.&lt;/p&gt;
&lt;p&gt;We skip &amp;#39;.*&amp;#39; anything up to &amp;#39;File&amp;#39; followed by whitespace and capture the hexadecimal handle number &amp;#39;([0-9A-F]+)&amp;#39;&lt;/p&gt;
&lt;p&gt;The replace operator is nice enough to store the captures in automatic variables $1 and $2.&lt;/p&gt;
&lt;p&gt;So we have the pid in $1 and the handle number in $2.&lt;/p&gt;
&lt;p&gt;Having replaced all other characters of each matching line, we can build a new line from scratch with the&lt;/p&gt;
&lt;p&gt;path to the handle in first place and add the parameters for -p = pid ( $1 ) and -c = handlenumber ( $2 )&lt;/p&gt;
&lt;p&gt;to the commandline. &amp;quot;-y&amp;quot; is just used to avoid confirmation questions.&lt;/p&gt;
&lt;p&gt;Regexps are GREAT!&lt;/p&gt;
&lt;p&gt;Take a look at them!!&lt;/p&gt;
&lt;p&gt;Klaus&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.technet.com/aggbug.aspx?PostID=3453943" width="1" height="1"&gt;</description></item></channel></rss>