PowerShell: Calling Static Methods, Object Instantiation and PowerTab

Hi Folks,

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

image

 

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:

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

You can of course run endless number of static methods:

Try this one:

[System.Console]::get_backgroundcolor()

Intellisense

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:

http://thepowershellguy.com/blogs/posh/pages/powertab.aspx

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:

Set-ExecutionPolicy unrestricted

Later after the install you can set it back to normal:

Set-ExecutionPolicy Restricted

You can read more about signing with this command:

get-help about_signing

 

image

Once I run this, I double click the setup from PowerTab, you will be presented with this dialog box:

image

I press enter to kick off the install.

I answer the various questions during the install, usually the default will suffice:

image

Here is the final screen, once PowerTab is installed:

image

When I close and reopen PowerShell, I now have PowerTab for Intellisense, this can be useful for seeing .NET assemblies using the TAB key.

image

YAY! Now check it out when I press the TAB key when looking for a .NET class to script:

image

I can now easily write some f^%& cool scripts, so who’s your Daddy?

Object Instantiation

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:

Get-Help *

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

NAME

New-Object

SYNOPSIS

Creates an instance of a .Net or COM object.

SYNTAX

New-Object [-typeName] <string> [[-argumentList] <Object[]>] [<CommonParameters>]

New-Object [-comObject] <string> [-strict] [<CommonParameters>]

DETAILED DESCRIPTION

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.

RELATED LINKS

Compare-Object

Select-Object

Sort-Object

ForEach-Object

Group-Object

Measure-Object

Tee-Object

Where-Object

REMARKS

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:

————myXML.xml———————-

<Person>
<Name>Romiko</Name>
<Surname>Van De Dronker</Surname>
</Person>

———————————————–

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:

image

$myInstance.Load("c:\myXML.xml")

Now we can display the XML from the object

$myInstance.Person

Here is the output:

image

You can even see other properties like:

$myInstance.get_InnerXml()

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

image

Conclusion

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.

Cheers

Advertisement
  • Uncategorized

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s