Welcome to ThePowerShellGuy.com Sign in | Join | Help

 This post is about home-made netcat :) Of course this is only one function of it, but it can be useful too. Sometime you want to listen on some port and see data that arrives to it to analyze it.  There is the script:

function Trace-Port([int]$port=23, [string]$IPAdress="127.0.0.1", [switch]$Echo=$false){
    $listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse($IPAdress), $port)
    $listener.start()
    [byte[]]$bytes = 0..255|%{0}
    write-host "Waiting for a connection on port $port..."
    $client = $listener.AcceptTcpClient()
    write-host "Connected from $($client.Client.RemoteEndPoint)"
    $stream = $client.GetStream()
    while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0)
    {
        $bytes[0..($i-1)]|%{$_}
        if ($Echo){$stream.Write($bytes,0,$i)}
    }
    $client.Close()
    $listener.Stop()
    write-host "Connection closed."
}

After you add this function to your shell you can use it this way:

PS> Trace-Port -ip 192.168.1.99 -port 333
Waiting for a connection on port 333...

Now script waiting for connection on port 333. I will connect to this port using telnet.exe, and then write word "Test" into it:

Connected from 192.168.1.99:61829
84
101
115
116
13
10
Connection closed.

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:

PS> Trace-Port 123 | foreach { write-host ([char]$_) -NoNewLine }
Waiting for a connection on port 123...
Connected from 127.0.0.1:62174
Test
Connection closed.

You can also specify -Echo switch if you want all input echoed back to client.

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 PSEventing library, and i will try to rewrite this script when i will have free time again :)

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 unix command wget $url -O -

.Net have WebClient objects for doing things like this, so you just need to wrap it into function easy to use in PowerShell.

function Get-WwwContent ([string]$url, [string]$Encoding="utf-8"){

$wc = new-object System.Net.WebClient

$wc.Encoding = [System.Text.Encoding]::GetEncoding($Encoding)

$wc.DownloadString($url) }

You can just copy it and paste to PowerShell window, or place it in your profile. Also (and i prefer this way myself) 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:

. c:\powershell\functions.ps1

This will load all functions contained in this file to PowerShell every time when it starts.

You can determine your profile location by looking in $profile variable:

PS> $profile
C:\Documents and Settings\User1\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

This file and folder doesnt exists by default, so you might need to create it:

PS> New-Item -ItemType file -Path $PROFILE -Force

and then edit it with notepad for example:

PS> notepad $profile

Lets return to Get-WwwString. When this function is added to PowerShell, you can use it to get content of webpages:

$googlePage = Get-WwwContent "http://www.google.com"

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):

Get-WwwString "http://ya.ru" "windows-1251"

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.

I had a blog on livejournal.com, 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 google to translate it. Remember though: automatic translation is much uglier than my english ;)

Lets begin...

PS: Mow, thanks for this CS hosting! :)