<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://thepowershellguy.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Xaegr's PowerShell blog</title><subtitle type="html">Collection of PowerShell usage examples, functions and snippets.</subtitle><id>http://thepowershellguy.com/blogs/xaegr/atom.aspx</id><link rel="alternate" type="text/html" href="http://thepowershellguy.com/blogs/xaegr/default.aspx" /><link rel="self" type="application/atom+xml" href="http://thepowershellguy.com/blogs/xaegr/atom.aspx" /><generator uri="http://communityserver.org" version="2.1.61129.2">Community Server</generator><updated>2007-06-02T15:47:00Z</updated><entry><title>Trace-Port</title><link rel="alternate" type="text/html" href="http://thepowershellguy.com/blogs/xaegr/archive/2007/06/03/trace-port.aspx" /><id>http://thepowershellguy.com/blogs/xaegr/archive/2007/06/03/trace-port.aspx</id><published>2007-06-03T09:51:00Z</published><updated>2007-06-03T09:51:00Z</updated><content type="html">&lt;P&gt;&amp;nbsp;This post is about home-made netcat :) Of course this is&amp;nbsp;only one function of it, but it can&amp;nbsp;be&amp;nbsp;useful too. Sometime you want to listen on&amp;nbsp;some port and see&amp;nbsp;data that arrives to it to analyze it. &amp;nbsp;There is the script:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Lucida Console" color=#808080&gt;function Trace-Port([int]$port=23, [string]$IPAdress="127.0.0.1", [switch]$Echo=$false){&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse($IPAdress), $port)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $listener.start()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [byte[]]$bytes = 0..255|%{0}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Waiting for a connection on port $port..."&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $client = $listener.AcceptTcpClient()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Connected from $($client.Client.RemoteEndPoint)"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $stream = $client.GetStream()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $bytes[0..($i-1)]|%{$_}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ($Echo){$stream.Write($bytes,0,$i)}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $client.Close()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $listener.Stop()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; write-host "Connection closed."&lt;BR&gt;}&lt;/FONT&gt; 
&lt;P&gt;After you add this function to your shell you can use it this way:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Lucida Console" color=#808080&gt;PS&amp;gt; Trace-Port -ip 192.168.1.99 -port 333&lt;BR&gt;Waiting for a connection on port 333...&lt;/FONT&gt; 
&lt;P&gt;Now script waiting for connection on port 333. I will connect to this port using telnet.exe, and then write word "Test" into it:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Lucida Console" color=#808080&gt;Connected from 192.168.1.99:61829&lt;BR&gt;84&lt;BR&gt;101&lt;BR&gt;115&lt;BR&gt;116&lt;BR&gt;13&lt;BR&gt;10&lt;BR&gt;Connection closed.&lt;/FONT&gt; 
&lt;P&gt;Function outputs byte array, so if you want to see characters you can pipe output to Foreach-Object cmd-let and convert it to chars:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Lucida Console" color=#808080&gt;PS&amp;gt; Trace-Port 123 | foreach { write-host ([char]$_) -NoNewLine }&lt;BR&gt;Waiting for a connection on port 123...&lt;BR&gt;Connected from 127.0.0.1:62174&lt;BR&gt;Test&lt;BR&gt;Connection closed.&lt;/FONT&gt; 
&lt;P&gt;You can also specify -Echo switch if you want all input echoed back to client.&lt;/P&gt;
&lt;P&gt;Because native PowerShell doesnt support threads, this script will run in same thread as PowerShell, and will not respond to events like Ctrl+C until client disconnect. But this problem can be solved by using &lt;A href="http://www.codeplex.com/PSEventing" target=_blank&gt;PSEventing&lt;/A&gt; library, and i will try to rewrite this script when i will have free time again :)&lt;/P&gt;&lt;img src="http://thepowershellguy.com/aggbug.aspx?PostID=2248" width="1" height="1"&gt;</content><author><name>Xaegr</name><uri>http://thepowershellguy.com/members/Xaegr.aspx</uri></author><category term="Function" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/Function/default.aspx" /><category term=".Net" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/.Net/default.aspx" /><category term="PowerShell" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/PowerShell/default.aspx" /><category term="PoSH" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/PoSH/default.aspx" /></entry><entry><title>Get-WwwContent</title><link rel="alternate" type="text/html" href="http://thepowershellguy.com/blogs/xaegr/archive/2007/06/02/get-wwwcontent.aspx" /><id>http://thepowershellguy.com/blogs/xaegr/archive/2007/06/02/get-wwwcontent.aspx</id><published>2007-06-02T17:51:00Z</published><updated>2007-06-02T17:51:00Z</updated><content type="html">&lt;p&gt;One of functions that i use very often is Get-WwwString. All what it does - downloads page from web and returns it in string. It is like&amp;nbsp;unix command &lt;em&gt;wget $url -O -&lt;/em&gt;&lt;/p&gt; &lt;p&gt;.Net have WebClient objects for doing things like this, so you just need to wrap it into function easy to use in PowerShell.&lt;/p&gt; &lt;p&gt;&lt;font face="Lucida Console" color="#808080"&gt;function Get-WwwContent ([string]$url, [string]$Encoding="utf-8"){&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Lucida Console" color="#808080"&gt;$wc = new-object System.Net.WebClient&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Lucida Console" color="#808080"&gt;$wc.Encoding = [System.Text.Encoding]::GetEncoding($Encoding)&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Lucida Console" color="#808080"&gt;$wc.DownloadString($url) }&lt;/font&gt;&lt;/p&gt; &lt;p&gt;You can just copy it and paste to PowerShell window, or place it in your profile. Also (and i prefer this way myself)&amp;nbsp;you can add it to separate .ps1 file, and preload it in your profile. For example if you add it to c:\powershell\functions.ps1 and then add following line to your profile:&lt;/p&gt; &lt;p&gt;&lt;font face="Lucida Console" color="#808080"&gt;. c:\powershell\functions.ps1&lt;/font&gt;&lt;/p&gt; &lt;p&gt;This will load all functions contained in this file to PowerShell every time when it starts.&lt;/p&gt; &lt;p&gt;You can determine your profile location by looking in $profile variable:&lt;/p&gt; &lt;p&gt;&lt;font color="#808080"&gt;&lt;font face="Lucida Console"&gt;PS&amp;gt; $profile&lt;br&gt;C:\Documents and Settings\User1\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1&lt;/font&gt; &lt;/font&gt; &lt;p&gt;This file and folder doesnt exists by default, so you might need to create it:&lt;/p&gt; &lt;p&gt;&lt;font face="Lucida Console" color="#808080"&gt;PS&amp;gt; New-Item -ItemType file -Path $PROFILE -Force&lt;/font&gt;&lt;/p&gt; &lt;p&gt;and then edit it with notepad for example:&lt;/p&gt; &lt;p&gt;&lt;font face="Lucida Console" color="#808080"&gt;PS&amp;gt; notepad $profile&lt;/font&gt;&lt;/p&gt; &lt;p&gt;Lets return to Get-WwwString. When this function is added to PowerShell, you can use it to get content of webpages:&lt;/p&gt; &lt;p&gt;&lt;font face="Lucida Console" color="#808080"&gt;$googlePage = Get-WwwContent "http://www.google.com"&lt;/font&gt;&lt;/p&gt; &lt;p&gt;You can also specify optional parameter encoding (by default it is utf-8, but you can change this default value in function body if you want):&lt;/p&gt; &lt;p&gt;&lt;font face="Lucida Console" color="#808080"&gt;Get-WwwString "http://ya.ru" "windows-1251"&lt;/font&gt;&lt;/p&gt;&lt;img src="http://thepowershellguy.com/aggbug.aspx?PostID=2230" width="1" height="1"&gt;</content><author><name>Xaegr</name><uri>http://thepowershellguy.com/members/Xaegr.aspx</uri></author><category term="Web" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/Web/default.aspx" /><category term="Function" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/Function/default.aspx" /><category term=".Net" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/.Net/default.aspx" /><category term="PowerShell" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/PowerShell/default.aspx" /><category term="PoSH" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/PoSH/default.aspx" /></entry><entry><title>Write-Host "Hello World"</title><link rel="alternate" type="text/html" href="http://thepowershellguy.com/blogs/xaegr/archive/2007/06/02/tessst.aspx" /><id>http://thepowershellguy.com/blogs/xaegr/archive/2007/06/02/tessst.aspx</id><published>2007-06-02T17:47:00Z</published><updated>2007-06-02T17:47:00Z</updated><content type="html">&lt;P&gt;Hello there. I'm Xaegr, heavily addicted PowerShell fun. I'm from Russia, and my english is not very good ;) But there i will try to write posts on english. My posts doesnt contains many text (more code and pictures), but this will be not easy task for me anyway. So if you have any corrections - feel free to notify me about it.&lt;/P&gt;
&lt;P&gt;I had a blog on &lt;A href="http://xaegr.livejournal.com/" target=_blank&gt;livejournal.com&lt;/A&gt;, it all in russian, but contains some useful post which i will try to translate to english and repost here. If you dont want to wait, you can use services like &lt;A href="http://www.google.com/translate_t" target=_blank&gt;google&lt;/A&gt;&amp;nbsp;to translate it. Remember though: automatic translation is much uglier than my english ;)&lt;/P&gt;
&lt;P&gt;Lets begin...&lt;/P&gt;
&lt;P&gt;PS: &lt;A class="" href="http://thepowershellguy.com/blogs/posh"&gt;Mow&lt;/A&gt;, thanks for this CS hosting! :)&lt;/P&gt;&lt;img src="http://thepowershellguy.com/aggbug.aspx?PostID=2229" width="1" height="1"&gt;</content><author><name>Xaegr</name><uri>http://thepowershellguy.com/members/Xaegr.aspx</uri></author><category term="Misc" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/Misc/default.aspx" /><category term="PowerShell" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/PowerShell/default.aspx" /><category term="PoSH" scheme="http://thepowershellguy.com/blogs/xaegr/archive/tags/PoSH/default.aspx" /></entry></feed>