简单的代码看起来像下面和它的工作:
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()
我已经解释了在其他线程下你可以选择其中之一: Powershell via VB.NET. Which method and why?
如果您提供更多关于错误我可能会提供帮助。
增加了更多的信息有完整的例子:
我刚刚创建了一个非常简单的C#DLL并把它称为从VB.NET如下:
C#DLL代码:(认为这是一个第三方模块)
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;
}
}
}
如果你想测试在PS命令窗口这个第三方DLL只使用以下命令:
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>
现在我使用加载第三方模块相同的步骤如下VB.NET应用:
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
下面是在调试窗口输出证明代码不会作为工作预计:
貌似答案:http://stackoverflow.com/questions/6266108/powershell-how-to-import-module-in-a-runspace –
那ANS wer使用Runspace类。我已经知道我可以用这个班级来完成,我的目标是在没有班级的情况下完成。 –
你有错误吗? “PowerShellCommandResults”中是否有任何结果? – Richard