来自vb.net System.Diagnostics.process的多个powershell脚本和命令

时间:2013-05-23 18:55:10

标签: vb.net powershell

您好我正在使用vb.net程序,该程序将调用一些powershell脚本和命令来管理Exchange 2010.目前我正在使用System.Diagnostics.process一次调用一个脚本。这里是工作代码的例子

Public Sub execPowershell(ByVal File As String, ByVal Argument As String)



    Dim objProcess As System.Diagnostics.Process
    Try
        objProcess = New System.Diagnostics.Process()
        objProcess.StartInfo.FileName = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe "
        objProcess.StartInfo.Arguments = " -executionpolicy bypass " & File & " " & Argument
        objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
        objProcess.StartInfo.RedirectStandardError = True
        objProcess.StartInfo.UseShellExecute = False
        objProcess.StartInfo.WorkingDirectory = "c:\scripts"

        objProcess.Start()

        'Wait until the process passes back an exit code 
        'Dim toto As String = objProcess.StandardError.ReadToEnd()
        objProcess.WaitForExit()


    Catch
        'MessageBox.Show("Could not start process " & ProcessPath, "Error")
    End Try

End Sub

我想要做的是打开一个PowerShell提示符并由一个编写器运行多个命令。

喜欢这样的东西,但似乎powershell提示符没有收到命令。如果我做同样的dos提示“cmd”其工作

Public Sub execPowershelltest()


    Dim objProcess As New Process
    Dim startinfo As New ProcessStartInfo
    Try

        startinfo.FileName = "powershell" 'starts powershell window
        startinfo.RedirectStandardInput = True
        startinfo.RedirectStandardOutput = True
        startinfo.RedirectStandardError = True
        startinfo.UseShellExecute = False 'required to redirect


        startinfo.WindowStyle = ProcessWindowStyle.Normal

        startinfo.WorkingDirectory = "c:\windows"
        objProcess.StartInfo = startinfo
        objProcess.Start()

        Dim sw As StreamWriter = objProcess.StandardInput


        sw.WriteLine("md c:\toto")

        sw.WriteLine("md c:\powershellbysentence")

        sw.WriteLine("md c:\toto2")
        sw.Close()
        'Wait until the process passes back an exit code 
        'Dim toto As String = objProcess.StandardError.ReadToEnd()

        Dim toto As String = objProcess.StandardError.ReadToEnd
        objProcess.WaitForExit()


    Catch
        'MessageBox.Show("Could not start process " & ProcessPath, "Error")
    End Try

End Sub

提前谢谢

0 个答案:

没有答案