Summary: A page of sample cmdlets for Windows PowerShell

An IRC Bot in Windows PowerShell


I whipped this up when I was first playing with Windows PowerShell. I can't guarantee it's perfect - I don't know how to use exceptions yet - but it should help people that are having problems.

It's a standard text cmdlet, just give the file a .ps extension:

		  # monadbot.ps
	

		  $SERVER = "irc.server.org"
		  $PORT = 6667
		  $NICK = "MonadBot"
		  $NICKSERV_PASS = "blablabla"
	

		  # set up our socket
		  $sock = new-object [System.Net.Sockets.TcpClient($SERVER,] $PORT);
		  echo "Connected to ${SERVER}!"
		  $stream = [$sock.GetStream();]
	

		  # and our read/write stream
		  $reader = new-object [System.IO.StreamReader($stream);]
		  $writer = new-object [System.IO.StreamWriter($stream);]
	

		  # Now define some decent functions
	

		  # <summary>Sends a raw command to the server</summary>
		  function [SendCommand($Command)]
		  {
		      echo "--> $Command"
		      [$writer.WriteLine($Command)]
		      $writer.Flush()
		  }
	

		  # <summary>Check if a command is a ping, and handle it if so</summary>
		  # <returns>true if a ping was handled, false otherwise</returns>
		  function CheckPing([string] $Line)
		  {
		      if ($Line.StartsWith("PING :"))
		      {
		          [SendCommand] ("PONG :" + [$Line.SubString(6))]
		          [bool] 1
		      }
		      else
		      {
		          [bool] 0
		      }
		  }
	

		  # <summary>Called when the bot has successfully connected and chosen a nick</summary>
		  function [OnConnect()]
		  {
		      if ($NICKSERV_PASS -ne "")
		      {
		          [SendCommand] ("PRIVMSG [NickServ] IDENTIFY " + $NICKSERV_PASS)
		          [SendCommand] "PRIVMSG [HostServ] ON"
		      }
		      [SendCommand] "JOIN #bots"
		  }
	


		  # <summary>Called when a command comes in from the server</summary>
		  function HandleRawMessage([string] $Nick, [string] $Hostname, [string] $Command, [string] $Args)
		  {
		      # "Welcome"
		      if ($Command -eq "001")
		      {
		          [OnConnect]
		      }
		  }
	

		  # <summary>Processes a raw string command from the server</summary>
		  function HandleCommand([string] $Line)
		  {
		      $split = $Line.Split(" ", 3)
	

		      $hostnick = $split[0]
		      $command  = $split[1];
		      $params   = $split[2];
		      $hostname = ""
		      $nick = ""
		      $split = $hostnick.Split("!", 2);
	

		      if ($split.Size -eq 1)
		      {
		          $hostname = $split[0]        
		      }
		      else
		      {
		          $nick = $split[0].SubString(1)
		          $hostname = $split[1]
		      }
		      [HandleRawMessage] $nick $hostname $command $params
		  }
	

		  [SendCommand] "USER bla bla bla :bla"
		  [SendCommand] ("NICK " + $NICK)
	

		  # Our main program loop
	

		  while (1)
		  {
		      # read a server command and process it
		      $line = [$reader.ReadLine()]
		      echo "<-- $line"
	

		      if (!(CheckPing $line))
		      {
		          [HandleCommand] $line
		      }
		  }
	
Microsoft Communities