0

こんにちは、Exchange 2010 を管理するためのいくつかの powershell スクリプトとコマンドを呼び出す vb.net プログラムを作成しています。現在、System.Diagnostics.process を使用してスクリプトを 1 つずつ呼び出しています。作業コードの例

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

事前にThx

4

0 に答える 0