1

I have to call PS commands from a VB.NET program. This works fine if I'm calling standard PS commands, but if I have to call a command that lives in a 3rd party module I can't seem to make it work. At the PS console I can type Import-Module MyModule and then I can call the commands in that module. I tried the following but it doesn't work, I still can't access my command from within the module:

Dim PowerShell As Management.Automation.PowerShell = PowerShell.Create()
Dim PowerShellCommand As New PSCommand()
Dim PowerShellCommandResults As Object

PowerShellCommand.AddScript("Import-Module MyModule")
PowerShellCommand.AddScript("Get-MyCommand | Out-String")
PowerShell.Commands = PowerShellCommand
PowerShellCommandResults = PowerShell.Invoke()

How can I do this with the above code example? I don't want to change everything to the Runspace class unless I have to.

4
  • Looks like the answer: stackoverflow.com/questions/6266108/… Commented May 18, 2012 at 19:36
  • That answer uses the Runspace class. I already know I can do it using that class, my goal is to do it without that class. Commented May 18, 2012 at 19:42
  • Do you get an error? Are there any results in PowerShellCommandResults? Commented May 21, 2012 at 17:45
  • There are no errors (if there were I would have posted them) and PowerShellCommandResults contains nothing. However, again, if I call a standard system command then PowerShellCommandResults contains an array of ps objects. Apparently the call to import-module is not working. But it DOES work at the command line, or if I use the Runspace class in my code. Commented May 21, 2012 at 18:04

1 Answer 1

-1

The simple code look like as below and it does work:

Dim command As New PSCommand()
command.AddScript("<Powershell command here>")
Dim powershell As Management.Automation.PowerShell = powershell.Create()
powershell.Commands = command
Dim results = powershell.Invoke()

I have explained the following in other thread which one your can choose: Powershell via VB.NET. Which method and why?

If you provide more about the error info I might can help.

Added more Info with full example:

I have just created a very Simple C# DLL and called it from VB.NET as below:

C# DLL Code: (Consider it a 3rd party module)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;

namespace CSVtoXML
{
  public class Actions
  {
    public Actions()
    {

    }

    public static string writeHello(string name)
    {
        return "Hello " + name;

    }
 }
}

If you want to test this 3rd party DLL in PS command window just use the following:

PS C:\2012>  [Reflection.Assembly]::LoadFile("C:\2012\CSVtoXML.dll")

GAC    Version        Location
---    -------        --------
False  v2.0.50727     C:\2012\CSVtoXML.dll


PS C:\2012> [CSVtoXML.Actions]::writeHello("Avkash")
 Hello Avkash
PS C:\2012>

Now I am using the same steps by loading 3rd party module in VB.NET application as below:

Module Module1
 Sub Main()
    Dim PowerShell As Management.Automation.PowerShell = PowerShell.Create()
    Dim PowerShellCommand As New PSCommand
    PowerShellCommand.AddScript("[Reflection.Assembly]::LoadFile('C:\2012\CSVtoXML.dll')")
    PowerShellCommand.AddScript("[CSVtoXML.Actions]::writeHello('Avkash')")
    PowerShell.Commands = PowerShellCommand
    Dim results = PowerShell.Invoke()
    MsgBox(results.Item(0).ToString())
 End Sub
End Module

Here is the output in debug window to prove that the code does work as expected:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

I sure read your question and suggest the above code will work. Unless you tell me what did not work, i cant help. You need to tell me explain the problem with error message to dig further, just saying it "did not work" does not help.. thanks for -ve though. Next
My question clearly explains that I need to call a function in a 3rd party module by FIRST calling import-module. The example that I give does not work, seemingly because the call to import-module does not work. Showing me how to load a dll and call a function by namespace and classname is hardly what I'm looking for.
Thanks for the explanation. Appreciate it. I will get some time to see why Import-module does not work with VB.net and let u know..
To be clear... import-module works fine if I use the Runspace class in my code. For educational reasons I don't want to use the Runspace class, I want to understand why my original code above does not work. I suspect it has to do with calling add-script twice before the invoke, but if I do add-script invoke, add-script invoke, that also does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.