Saturday, March 23, 2013

Fun with the .Net Framework

Okay, admittedly, there are a slew of blogs and web pages that cover exactly the same thing.

I just do it a bit differently.

Here's an example of what I mean:

Someone makes a big deal about the below information:

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show("Hello World")


I do this:

#This line creates a WScript.Shell COM object I will use to set the current directory. 
$ws = new-object -comobject WScript.Shell

#This line loads SCOM 2007 SDK Operations Manager Framework
$iret =[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager")
#This line creates a pointer to a specific ManagementPack I want to unseal
$mp = new-object Microsoft.EnterpriseManagement.Configuration.ManagementPack("C:\Program Files\System Center Operations Manager 2007\System.Library.mp")

#This line creates a pointer to the XmlWriter I will use to write out the XML with
$xmlWriter = new-object Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter($ws.CurrentDirectory)

#This line writes the ManagementPack out in xml Format and tells me where the file is
$filename = $xmlWriter.WriteManagementPack($mp)

#This line displays a messagebox with the newly created filename and path
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show($filename)


I could have just as easily used: Write-Host $filename and been done with it.

But what is really going on here with: [System.Windows.Forms.MessageBox]::Show($filename)?

Well, for one thing, take a look at the image below:


The object, System.Windows.Forms.MessageBox, is created and is told to invoke a method called show.

Since this method takes many optional parameters, we use the one which enables us to use just one.




No comments:

Post a Comment