Je dois exécuter des scripts depuis une application Windows vb.net.Exécution de scripts PowerShell à partir d'une application Windows .NET
J'ai les scripts en cours d'exécution en arrière-plan;
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
Cependant, je dois maintenant être en mesure d'avoir la course powershell (pas en arrière-plan), par exemple. Lorsque des invites se produisent
Set-ExecutionPolicy unrestricted
Je suis actuellement à la recherche dans l'espace de noms Microsoft.PowerShell.ConsoleHost pour voir si je peux utiliser quelque chose comme;
Dim config = RunspaceConfiguration.Create
ConsoleShell.Start(config, "Windows PowerShell", "", New String() {""})
Quelqu'un peut-il me conseiller s'il vous plaît ???
EDIT: Je l'ai un peu trafiqué avec ceci;
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
Mais il doit y avoir un meilleur moyen?