Sunday, October 22, 2006

Print picture shootingdate

Ok, I've played around with the System.Drawing-namespace in C# to do some image manipulation. I've written a small Powershell-thingy to print the EXIF-shootingdate (ID 306) from jpeg-files in the current directory.

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") |
foreach ($file in (dir *.jpg)) {

$image = [system.drawing.image]::FromFile($file.FullName)
foreach ($property in $image.PropertyItems) {
if ($property.Id -eq 306) {
[Console]::WriteLine($file.FullName + " : " + [System.Text.Encoding]::ASCII.GetString($property.Value))

Translate Swedish words to English

This script was inspired by a colleague of mine who wanted to create a translation-thingy.
Anyway, the concept is easy. The script POST:s the query to Skoldatanätet (who operates a english/swedish translationservice through a regular html-page) and uses Regular Expression to match the result (the second entry which is contained in a HTML <B>-tag.

I'm sure there are better ways to read input and output the result, but since I'm a C#-programmer I used the .NET Framework functions because I'm lazy.. ;)

[Console]::Write("Swedish: ")
[Console]::WriteLine("English: " + [RegEx]::Match([System.Text.Encoding]::ASCII.GetString((New-Object net.webclient).UploadData("", "POST", [System.Text.Encoding]::ASCII.GetBytes("sprak=källspråk&uppslagsord=" + [Console]::ReadLine().Trim()))),"<B\b[^>]*>(.*?)</B>").NextMatch().Groups[1].Value)

Print SID on all users in Active Directory

This little snippet was actually my first try at Powershell. Since I have experience from Perl, C# and Bash I hoped that the learning curve shouldn't be so steep and I was amazed at how easy the transition from Perl/C#(.NET) was.
However, I haven't yet grasped the Powershell output functions, so this script does an ugly string.Format to output the data...

foreach ($res in (New-Object System.DirectoryServices.DirectorySearcher -argument "(objectClass=user)").FindAll()) { $user = $res.GetDirectoryEntry() [string]::Format("{0}:{1}",$user.Name.ToString(),(New-Object System.Security.Principal.SecurityIdentifier -argument $user.objectSid[0],0).ToString())}


I have watched Microsofts Powershell (formerly Monad) from a distance without really taking a deeper look at what it really is. So, finally I have installed it just to see what it is...

Anyway, I intend to post small snippets of more or less usefull Powershell scripts on this blog. Hopefully you'll find something useful here...