2012-01-27 70 views
1

如何在C#中執行Powershell腳本而不使用用戶名和密碼?在C#中執行PowerShell腳本,無需用戶名和密碼

PowerShell腳本:

#Script:This Script generate the Package (Zip file) dynamically for unittest 
#DirectoryName is Out Directory of Unittest execution 
#location is Out\Resource Directory 
#locationdetail is Array of files Directory for generate the Package 
#FileName is name of Package generated through this script 
#option is use for switch case implementation,currently not implemented 

param( 
     [string]$DirectoryName="." 
    , [string]$location="" 
    , [string]$FileName="Test.zip" 
    , [int] $option 
) 

[System.Reflection.Assembly]::LoadFrom($DirectoryName+"\\"+"Ionic.Zip.dll"); 
    $locationdetail = "Package\\Resource", "Package\\ResourceBad", "Package\\ResourceEmptymetada","Package\\ResourceEmptydatafile","Package\\EmptyTest" 
    $directoryToZip="" 

    foreach ($element in $locationdetail) 
    { 
    If($element -eq "Package\\Resource") 
    { 
     $directoryToZip= $location+"\\"+"Package\\Resource" 
     $FileName ="Package (Good Data).zip"   
    } 
    If($element -eq "Package\\ResourceBad") 
    { 
     $directoryToZip= $location+"\\"+"Package\\ResourceBad" 
     $FileName ="Package (Bad Data).zip"  
    } 
    If($element -eq "Package\\ResourceEmptymetada") 
    { 
     $directoryToZip= $location+"\\"+"Package\\ResourceEmptymetada" 
     $FileName ="Package (Bad with ManifestEmpty).zip"    
    } 
    If($element -eq "Package\\ResourceEmptydatafile") 
    { 
     $directoryToZip= $location+"\\"+"Package\\ResourceEmptydatafile" 
     $FileName ="Package (Bad with Datafile Empty).zip"  
    } 
    If($element -eq "Package\\EmptyTest") 
    { 
     $directoryToZip= $location+"\\"+"Package\\EmptyTest" 
     $FileName ="EmptyTest.zip"  
    } 
    $zipfile = new-object Ionic.Zip.ZipFile 
    $e= $zipfile.AddDirectory($directoryToZip) 
    $zipfile.Save($location+"\\"+$FileName) 
    $zipfile.Dispose() 
    $directoryToZip="" 
    } 

和我的C#代碼

private static void StartPowerShell(string args,string TempScript) 
    { 

     string powerShellPath ="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"; 
     string commandLine = @" -Sta -Command ""& {{ try {{ {0} }} catch {{throw}} }} """.InvariantCultureFormat(args); 

     var info = new ProcessStartInfo(); 
     info.FileName = powerShellPath; 
     info.Arguments = commandLine; 
     info.WorkingDirectory = Environment.CurrentDirectory; 
     info.UseShellExecute = false; 
     info.CreateNoWindow = false; 
     info.RedirectStandardError = true; 
     info.RedirectStandardOutput = true; 

     //Pass the Username and Password here in ProcessStartInfo 
     info.UserName = "Saroop"; 
     string pw = "saroop"; 
     System.Security.SecureString password = new System.Security.SecureString(); 
     foreach (var item in pw) 
     { 
      password.AppendChar(item); 
     } 
     info.Password = password; 
     info.RedirectStandardError = true; 
     info.RedirectStandardOutput = true; 
     Process p = new Process(); 
     p.StartInfo = info; 
     bool flag = p.Start(); 
     string error = p.StandardError.ReadToEnd(); 

     p.WaitForExit(); 
    } 

請指引我的人。我嘗試使用runas命令以用戶(管理員)身份啓動Powershell.exe,但它也要求輸入用戶名和密碼。 我嘗試System.Management.Automation。 http://msdn.microsoft.com/en-us/library/system.management.automation(v=vs.85).aspx

:DLL,但它沒有更多的在.Net4.0 參考鏈接存在

請幫幫我。

+0

我發現我的查詢的答案。 用於執行PowerShell腳本必須執行政策命令 SYSTEM32 ** C:\ WINDOWS \ SYSTEM32 \ WindowsPowerShell \ V1.0 \ powershell.exe ** ,如果你的系統是64BIT隨後也EXECUTE同樣的政策對於64位POWERSHELL .EXE TOO。 ** C:\ Windows \ SysWOW64 \ WindowsPowerShell \ v1.0 \ POWERSHELL.EXE ** **'命令:Set-ExecutionPolicy Unrestricted' ** – 2012-01-27 13:40:50

回答

1

我找到了我的查詢的答案。 用於執行PowerShell腳本必須執行政策命令 SYSTEM32 C:\ WINDOWS \ SYSTEM32 \ WindowsPowerShell \ V1.0 \ powershell.exe ,如果你的系統是64BIT隨後也EXECUTE 64位POWERSHELL.EXE TOO相同的策略。 C:\ WINDOWS \ Syswow64資料\ WindowsPowerShell \ V1.0 \ POWERSHELL.EXE

/// COMMAND : `Set-ExecutionPolicy Unrestricted` 
1

您在評論中寫道什麼會的作品,但我想給你一個方向運行,從C#代碼PowerShell腳本。

您可以使用System.Management.Automation.Runspaces命名空間,其暴露很容易和更優雅的方式來運行PowerShell命令和檢索C#(和使用返回對象,而不僅僅是輸出文本流)的結果。嘗試閱讀有關它。

+0

下載PowerShell SDK http://www.microsoft.com /download/en/details.aspx?id=2560它有C#中的示例如何調用PowerShell。 – 2012-01-30 06:31:32

+0

這個DLL不能在.Net 4.0中使用。 – 2012-01-30 07:45:05

+0

@ Shanhar,Phil:你的方式也沒有錯,但爲此我需要安裝Powershell SDK。但是如果我沒有任何安裝SDK的權利,那麼它需要運行,因爲Process.My腳本在TFS 2010上運行,我不知道TFS服務器配置的任何信息。因此,我們在不同的場景中都是正確的 – 2012-01-30 11:57:28

相關問題