簡單的代碼看起來像下面和它的工作:
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