【发布时间】:2010-07-28 12:43:24
【问题描述】:
我需要在 vb.net windows 应用程序中运行脚本。
我的脚本在后台运行良好;
Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
MyRunSpace.Open()
Using MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
MyPipeline.Commands.AddScript("import-module -name " & moduleName &
vbCrLf &
"(get-module -name " & moduleName & ").version")
Dim results = MyPipeline.Invoke()
'Do something with the results
End Using
MyRunSpace.Close()
End Using
但是,我现在需要能够运行 powershell(而不是在后台),例如。出现提示时;
Set-ExecutionPolicy unrestricted
我目前正在研究 Microsoft.PowerShell.ConsoleHost 命名空间,看看我是否可以使用类似的东西;
Dim config = RunspaceConfiguration.Create
ConsoleShell.Start(config, "Windows PowerShell", "", New String() {""})
谁能给我建议???
编辑:我对此有点捏造;
Public Function RunPowershellViaShell(ByVal scriptText As String) As Integer
Dim execProcess As New System.Diagnostics.Process
Dim psScriptTextArg = "-NoExit -Command ""& get-module -list"""
'Dim psScriptTextArg = "-NoExit -Command ""& set-executionPolicy unrestricted"""
'Dim psScriptTextArg = ""-NoExit -Command """ & scriptText & """"
execProcess.StartInfo.WorkingDirectory = Environment.SystemDirectory & "\WindowsPowershell\v1.0\"
execProcess.StartInfo.FileName = "powershell.exe"
execProcess.StartInfo.Arguments = psScriptTextArg
execProcess.StartInfo.UseShellExecute = True
Return execProcess.Start
End Function
但是一定有更好的方法吗??
【问题讨论】:
标签: vb.net powershell powershell-2.0 windows