2013-05-22 88 views
1

我試圖用C#GUI客戶端包裝已經制作的PowerShell腳本。從C#包裝powershell腳本#

腳本根據來自用戶的輸入行爲,並且它也根據它們輸出。

我想獲得powershell輸出,將它們顯示給我的C#客戶端,然後根據他的選擇輸入輸入。

這是我的腳本,下面的代碼是我想到的,但它不起作用。 (我只能得到最後的輸出,並且只有在我的PowerShell腳本中沒有讀主機時)。希望它有助於某些方面。

Write-Host 
Write-Host 'Hello World!' 
Write-Host "Good-bye World! `n" 

$option = Read-Host "Please enter your number" 

switch ($option){ 
    "1" 
    { 
    Write-Host "jack" 
    } 
    "2" 
    { 
    Write-Host "john" 
    } 
    "3" 
    { 
    Write-Host "joe" 
    } 
} 

public void WrapPowerShell() 
{ 
     string directory = Directory.GetCurrentDirectory(); 

     ProcessStartInfo startInfo = new ProcessStartInfo(); 
     startInfo.FileName = @"powershell.exe"; 
     startInfo.Arguments = string.Format(@"& '{0}'", directory + @"\hello.ps1"); 
     startInfo.RedirectStandardOutput = true; 
     startInfo.RedirectStandardError = true; 
     startInfo.UseShellExecute = false; 
     startInfo.CreateNoWindow = false; 
     Process process = new Process(); 
     process.StartInfo = startInfo; 
     process.Start(); 



     string output = process.StandardOutput.ReadToEnd(); 
     // Assert.IsTrue(output.Contains("StringToBeVerifiedInAUnitTest")); 

     string errors = process.StandardError.ReadToEnd(); 
    } 

非常感謝您的回答!

+0

我還沒有試過你的代碼,只是試着給出Powershell.exe完全顯式路徑,例如'C:\ Windows \ SysWOW64 \ WindowsPowerShell \ v1.0 \ powershell.exe'(在64位系統上) –

回答

0

這是我寫的幫助程序類,幫助我創建允許人們運行PowerShell腳本的ASP.NET Web應用程序。

PSHelper.cs

using System.Collections.ObjectModel; 
using System.Management.Automation; 
using System.Management.Automation.Runspaces; 
using System.Text; 

namespace admin_scripts { 
    public class PSHelper { 
    // See the _ps_user_lookup method for demonstration of using this method 
    public static Runspace new_runspace() { 
     InitialSessionState init_state = InitialSessionState.CreateDefault(); 
     init_state.ThreadOptions = PSThreadOptions.UseCurrentThread; 
     init_state.ImportPSModule(new[] { "ActiveDirectory", "C:\\ps_modules\\disable_user.psm1" }); 
            // Custom PS module containing functions related 
            // to disabling AD accounts at work. 
            // You would use your own module here, obviously. 
     return RunspaceFactory.CreateRunspace(init_state); 
    } 

    // This method is for dead-simple scripts that you only want text output from 
    // Not as flexible as the previous method, but it's good for the really simple cases 
    public static string run_simple_script(string body) { 
     Runspace runspace = new_runspace(); 
     runspace.Open(); 

     Pipeline pipeline = runspace.CreatePipeline(); 
     pipeline.Commands.AddScript(body); 
     pipeline.Commands.Add("Out-String"); 

     Collection<PSObject> output = pipeline.Invoke(); 
     runspace.Close(); 

     StringBuilder sb = new StringBuilder(); 
     foreach(PSObject line in output) { 
     sb.AppendLine(line.ToString()); 
     } 
     return sb.ToString(); 
    } 
    } 
} 

我會調用腳本從ASP.NET處理程序,像這樣:

private PSObject _ps_user_lookup(string id_param) { 
    Runspace runspace = PSHelper.new_runspace(); 
    runspace.Open(); 
    using(Pipeline pipe = runspace.CreatePipeline()) { 
    Command cmd = new Command("Get-ADUser"); 
    cmd.Parameters.Add(new CommandParameter("Identity", id_param)); 
    cmd.Parameters.Add(new CommandParameter("Properties", "*")); 
    cmd.Parameters.Add(new CommandParameter("Server", "REDACTED")); 
    pipe.Commands.Add(cmd); 
    return pipe.Invoke().FirstOrDefault(); 
    } 
} 

這種安排的真正的美是Web應用程序將運行該腳本使用Web用戶的域帳戶的憑據。希望對你有幫助。

+0

喜。非常感謝你的答案! :) – user2355293

+0

當我嘗試運行簡單的腳本,我得到了錯誤CmdletInvocationExeption處理 - 「不能調用此函數,因爲當前主機沒有實現它。」 – user2355293

+0

你知道是什麼原因造成的嗎? – user2355293