Monday, February 20, 2012

PowerShell and MongoDB

I was exploring MongoDB last weekend and was a bit skeptical at first. The relational model (I used to teach it) is so ingrained into my way of thinking. Like the florescent bulbs in my garage during the Winter, the light slowly started to brighten. I can now see a lot of uses for a schema free database (especially as a tool for prototyping). After playing around with the JavaScript interface, I decided to see what PowerShell could do with it.

Luckily, there is a driver available for download.

Assuming you have MongoDB, the C# driver and PowerShell installed, you can play around with the following code:
# Add a reference to our dll        
Add-Type -Path 'C:\Program Files (x86)\MongoDB\CSharpDriver 1.3.1\MongoDB.Driver.dll'

# Name our test db (not actually created until we insert)
$db = [MongoDB.Driver.MongoDatabase]::Create('mongodb://localhost/PowerShellMongoTest');

# Name or test collection
$coll = $database["Stuff"]

# Define a couple list
$languages = @("C#","Haskell","PowerShell","Python")
$beers = @("Honkers Ale","Stella","Summer Shandy","Yuengling")

# Define our document
$doc = @{FirstName="Wes"; LastName="Stahler"; Languages=$languages; Beers=$beers}
$collection.Insert($doc)
$info = $collection.FindAll()
$info | Format-Table -AutoSize

#$collection.RemoveAll()

This yields:
Name       Value
---- -----
_id 4f42b6798359da1e7ce51bfb
Beers {Honkers Ale, Stella, Summer Shandy, Yuengling}
LastName Stahler
Languages {C#, Haskell, PowerShell, Python}
FirstName Wes

I will be experimenting more with MongoDB.
Share your insights with me if you decide to explore it as well.

Enjoy!

Tuesday, February 7, 2012

Calling vbScript via PowerShell

Following is an example of how to call a vbScript from PowerShell. I recently had to do something similar to this for a Postini SafeSender list conversion to Exchange 2007/AD.

First the vbScript:
Saved as CallFromPowerShell.vbs

Option Explicit
Dim strComputer, objWMI, OS

strComputer = WSH.Arguments(0)

On Error Resume Next
Set objWMI=GetObject("winmgmts://" & strComputer).InstancesOf("win32_operatingsystem")
If objWMI is nothing Then
WScript.Echo "Unable to connect to " & strComputer
Else
For Each OS In objWMI
wscript.Echo OS.Caption
Next
end If

To call this from PowerShell:

$computers = 'fatbeard-vp01','fatbeard-vp02','fatbeard-vp03'          
$computers |
foreach {"{0,20}`t{1}" -f $_,$(cscript.exe //nologo c:\temp\callfrompowershell.vbs $_) }

Yields...
fatbeard-vp01 Microsoft Windows 7 Enterprise
fatbeard-vp02 Microsoft(R) Windows(R) Server 2003, Enterprise Edition
fatbeard-vp03 Unable to connect to fatbeard-vp03

Monday, February 6, 2012

Project Euler #89

This one was fairly easy to do with PowerShell.
Problem 89 - " Develop a method to express Roman numerals in minimal form. "
$pre = $post = 0      
foreach ($line in Get-Content C:\temp\roman.txt) {
$pre += $line.Length
$line = $line -replace("DCCCC","CM")
$line = $line -replace("LXXXX","XC")
$line = $line -replace("VIIII","IX")
$line = $line -replace("IIII","IV")
$line = $line -replace("XXXX","XL")
$line = $line -replace("CCCC","CD")
$post += $line.Length
}
$pre-$post
Enjoy!