Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Using the .NET Framework from PowerShell

2008-12-13

If you want to call a static function (Shared in Visual Basic) in the .NET Framework, you type in the full name of the class (including the namespaces) followed by double colon (::) and the name of the function you want to call.

The function Load in the Assembly class can be used to load .NET assemblys from the GAC, so if you want to use the .NET Framework, it’s a good start to load the parts of the .NET Framework you need.

[System.Reflection.Assembly]::Load(“System.Windows.Forms, Version=2.0.50727.1433, Culture=neutral, PublicKeyToken=b77a5c561934e089”)

Notice that the string argument that is passed to the Load function is fairly complex. There is a chicken version of Load available where you only have to specify the path to the namespace you want to load. The name of that function is LoadWithPartialName, but it is not recommended that you use that function, because it is obsolete. If you are looking for the parameters that needs to be included in the string passed to the Loadfunction, check the properties window of the assembly in your GAC (usually C:\Windows\assembly).

When this is done, you can access the methods of the classes in System.Windows.Forms. This example will show a message box:

[System.Windows.Forms.MessageBox]::Show(“Hello”)

To create instances from classes in any loaded part of .NET Framework, use the New-Object cmdlet.

$x=New-Object System.Windows.Forms.SaveFileDialog

To use New-Object to create COM objects, just add the -comobject parameter to New-Object. If you have Excel installed, this will load and view it. First type:

$x=New-Object -comobject Excel.Application

…then:

$x.Visible=$True

Categories: PowerShell

Tags: .NET Framework

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips