Why Visual studio helps PowerShell

The next proper PowerShell piece I write should be on using  it to get at EXIF Data in images. I want to share a couple of things I've learnt along the way. The fGoirst is around finding libraries. If you find anything that is written for Visual Studio to work with EXIF fields, it use the system.drawing.bitmap object. So lets fire up powershell and create one of those shall we ?

 Windows PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved. 
PS C:\Users\Jamesone> $foo=New-Object -TypeName system.drawing.bitmap
New-Object : Cannot find type [system.drawing.bitmap]: make sure the assembly containing this type is loaded.
At line:1 char:16
+ $foo=New-Object  <<<< -TypeName system.drawing.bitmap
PS C:\Users\Jamesone> 

Click for a bigger view  Oh dear, powershell doesn't load the assembly by default - you've got to be a lot more of an expert in PowerShell than me to know what's loaded and what isn't - until you try.  People working in Visual studio take it for granted so the examples out there don't tell us what to load.

At this point I fire up Visual Studio - and you see from the picture I only have the free Express version. You don't even need to create a project, just go to the object browser, and browse to the object you want, and work up from there until you can see the DLL. Then you can copy and paste this to load the assembly, and try again.

 PS C:\Users\Jamesone>[reflection.assembly]::loadfile( "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll")
GAC    Version        Location
---    -------        --------
True   v2.0.50727     C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll 
PS C:\Users\Jamesone> $foo=New-Object -TypeName system.drawing.bitmap
New-Object : Constructor not found. Cannot find an appropriate constructor for type system.drawing.bitmap.
At line:1 char:16
+ $foo=New-Object  <<<< -TypeName system.drawing.bitmap
PS C:\Users\Jamesone>

Click for a larger versionOh dear. It turns out anywhere I see  one of thee objects being invoked it's being passed parameters. I can go and look at the Object's .NEW method in the object browser to find out what the arguments should be  - there are several ways to Invoke new but the one I want is with the Filename. So I invoke it like this (and see what I got) 

 PS C:\Users\Jamesone> $foo=New-Object -TypeName system.drawing.bitmap -ArgumentList "C:\Tour\RAW\TdF23760.JPG"
PS C:\Users\Jamesone> $foo 
Tag                  :
PhysicalDimension    : {Width=3872, Height=2592}
Size                 : {Width=3872, Height=2592}
Width                : 3872
Height               : 2592
HorizontalResolution : 72
VerticalResolution   : 72
Flags                : 77960
RawFormat            : [ImageFormat: b96b3cae-0728-11d3-9d7b-0000f81ef32e]
PixelFormat          : Format24bppRgb
Palette              : System.Drawing.Imaging.ColorPalette
FrameDimensionsList  : {7462dc86-6180-4c7e-8e3f-ee7333a7a483}
PropertyIdList       : {271, 272, 274, 282...}
PropertyItems        : {271, 272, 274, 282...}

 

So what else can I do with new ?

It turns out that I was writing database code in about the most long winded way possible. It went like this

  1. Create a connection object
  2. Set it's connectionString property
  3. Open it
  4. Create a command object
  5. Set it's connection to be the connection object you made in 1-3
  6. Set's CommandText string property
  7. Create an data adapter object
  8. Set it's SelectCommand object, to the Command object you made in 4-6
  9. Create a dataset object
  10. Fill the the dataset using the data adapter

A little poking round looking at the New methods for these objects shows me I can just create an adapter and give it the connection string SQL command text.  Now the process becomes

  1. Create a data adapter specifying the connection and SQL
  2. Create a dataset object
  3. Fill the the dataset using the adapter

Simpler, more elegant. I can't help feeling rather more PowerShell. Even if I need VB to get there.

Technorati tags: Microsoft, powershell