【问题标题】:Execute Powershell script in C# without UserName and Password在没有用户名和密码的 C# 中执行 Powershell 脚本
【发布时间】:2012-01-30 18:37:49
【问题描述】:

如何在不使用用户名和密码的情况下在 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。 dll 但在 .Net4.0 中不再存在 参考链接: http://msdn.microsoft.com/en-us/library/system.management.automation(v=vs.85).aspx

请帮帮我。

【问题讨论】:

  • 我找到了我的查询的答案。要执行 POWERSHELL 脚本,必须执行 SYSTEM32 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 的策略命令,如果您的系统是 64 位,则还必须为 64 位 POWERSHELL 执行相同的策略。也可以。 C:\Windows\SysWOW64\WindowsPowerShell\v1.0\POWERSHELL.EXE COMMAND : Set-ExecutionPolicy Unrestricted

标签: c# powershell process


【解决方案1】:

您在评论中写的内容会起作用,但我想为您提供另一个从 C# 代码运行 powershell 脚本的方向。

您可以使用 System.Management.Automation.Runspaces 命名空间,它公开了运行 powershell 命令并从 C# 检索结果(并使用返回的 objects 而不仅仅是输出文本流)的真正简单和更优雅的方式。尝试阅读它。

【讨论】:

  • 下载 PowerShell SDK,microsoft.com/download/en/details.aspx?id=2560 它有 C# 中如何调用 PowerShell 的示例。
  • 此 Dll 在 .Net 4.0 中不可用。
  • @Shanhar,Phil :您的方式也没有错,但为此我需要安装 Powershell SDK。但是如果我没有安装 SDK 的任何权利,那么它需要作为 Process 运行。我的脚本在 TFS 2010 上运行,我对 TFS 服务器配置一无所知。所以我们在不同的场景中都是正确的
【解决方案2】:

我找到了我的查询的答案。 执行 POWERSHELL 脚本必须执行策略命令 系统32 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 如果您的系统是 64 位的,那么也对 64 位 POWERSHELL.EXE 执行相同的策略。 C:\Windows\SysWOW64\WindowsPowerShell\v1.0\POWERSHELL.EXE

    /// COMMAND : `Set-ExecutionPolicy Unrestricted`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多