In this discussion, we will look at how we can call static methods with PowerShell.
Call Static Methods
Allot of the System namespaces are already loaded in PowerShell. So for example the following command can be used to get the current datetime.
So we issue the following command:
$MyDate = [System.DateTime]::Now
Notice the above is similar to using reflection in .NET to load an assembly:
[System.Reflection.Assembly]::LoadFrom("..\mydll") or calling other methods like assembly loads:
You can of course run endless number of static methods:
Try this one:
We need a way to enumerate .NET assemblies, like we do in Visual Studio!
Before we get started, I like to use PowerTab for intellisense. You can download it here:
Once I download it and before I run the setup, I need to trust the script, I do it the lazy way and trust all scripts, then afterwards will set it back to default policy:
Later after the install you can set it back to normal:
You can read more about signing with this command:
Once I run this, I double click the setup from PowerTab, you will be presented with this dialog box:
I press enter to kick off the install.
I answer the various questions during the install, usually the default will suffice:
Here is the final screen, once PowerTab is installed:
When I close and reopen PowerShell, I now have PowerTab for Intellisense, this can be useful for seeing .NET assemblies using the TAB key.
YAY! Now check it out when I press the TAB key when looking for a .NET class to script:
I can now easily write some f^%& cool scripts, so who’s your Daddy?
Lets try an instantiate a class. So lets assume we have no clue how to do this, use this command to get the help we need:
This will display a list of help topics, lets see if we can find something on object instantiation. I see new_object, this seems like something we will need:
PS C:\Documents and Settings\romiko> Get-Help new-object
Creates an instance of a .Net or COM object.
New-Object [-typeName] <string> [[-argumentList] <Object>] [<CommonParameters>]
New-Object [-comObject] <string> [-strict] [<CommonParameters>]
Creates an instance of a .Net or COM object. You specify either the type of a .Net class or a Programmatic Identifi
er (ProgID) of a COM object. By default, you type the fully-qualified name of a .Net class and the cmdlet returns a
reference to an instance of that class. To create an instance of a COM object, use the ComObject parameter and spe
cify the ProgID of the object as its value.
For more information, type: "get-help New-Object -detailed".
For technical information, type: "get-help New-Object -full".
PS C:\Documents and Settings\romiko>
Ok, so from here let us try and instantiate a .NET Object.
Imagine we have a text file with XML:
<Surname>Van De Dronker</Surname>
Lets say we want to load this into the DOM as an instance of the XMLDocument class.
First we create an instance:
$myInstance = new-object System.Xml.XmlDocument
This is what we can do is load the XML from a file:
Now we can display the XML from the object
Here is the output:
You can even see other properties like:
returns a string
<Person><Name>Romiko</Name><Surname>Van De Dronker</Surname></Person>
Other useful things you can do is see members like when you reflect objects:
[System.Xml.XmlDocument] | get-member
Well, I hope this gave you an insight into what we can do with PowerShell, we can pretty much do what we want with the system, and there is also allot of help features and even intellisense, if you take the time to download and install PowerTab, it helps allot when coding against assemblies, you can even use reflection here and load other assemblies and dll’s!
Have fun with it, and I see you folks next time, when we Geek it up with BizTalk/SharePoint and PowerShell.