繁体   English   中英

用不同的凭证调用Powershell脚本并传递参数

[英]Call a Powershell script with different credentials and pass parameters

我有一些问题:如何使用下面的代码实现此活动,因为我找不到在运行空间中传递用户ID和密码的方法。

  1. 如何使用不同的凭据调用PowerShell脚本?
  2. 调用PowerShell脚本时如何传递参数?
  3. 我想获取输出。

我使用了下面的代码,但无法通过凭据。

private static string RunScript(string scriptFile, string servername, string volume, string size,string userID,string userpassword)
{
    // Validate parameters
    StringBuilder stringBuilder = new StringBuilder();

    try
    {
        if (string.IsNullOrEmpty(scriptFile)) 
        { throw new ArgumentNullException("scriptFile"); }

        // if (parameters == null)
        // { throw new ArgumentNullException("parameters"); }

        RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

        using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
        {
            runspace.Open();
            RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
            scriptInvoker.Invoke("Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted");

            Pipeline pipeline = runspace.CreatePipeline();

            Command scriptCommand = new Command(scriptFile);

            CommandParameter Param1 = new CommandParameter("-Server  ",servername);
            CommandParameter Param2 = new CommandParameter("-Volumeletter", volume);
            CommandParameter Param3 = new CommandParameter("-deltasize", size);

            scriptCommand.Parameters.Add(Param1);
            scriptCommand.Parameters.Add(Param2);
            scriptCommand.Parameters.Add(Param3);

            pipeline.Commands.Add(scriptCommand);

            log.Info("scriptCommand " + scriptCommand.ToString());
            log.Info("scriptCommandValue Param[command] " + scriptCommand.Parameters[0].Value.ToString());
            log.Info("scriptCommandValue Param[password] " + scriptCommand.Parameters[1].Value.ToString());
            log.Info("scriptCommandValue Param[usename] " + scriptCommand.Parameters[2].Value.ToString());

            Collection<PSObject> psObjects;

            psObjects = pipeline.Invoke();

            foreach (PSObject obj in psObjects)
            {
                stringBuilder.AppendLine(obj.ToString());
            }

            // Console.WriteLine(stringBuilder.ToString());
            log.Info("output string : " + stringBuilder.ToString());
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return stringBuilder.ToString();
}

//尝试使用此示例对我有用

function getUser($abc) {

        <#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\Script\cred.txt#>

        $password = get-content C:\Script\cred.txt| convertto-securestring
        $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username",$password
        $output = start-process powershell -Credential $credentials -ArgumentList '-noexit','-File', 'C:\script\script2.ps1', $abc
        return $output
    }

    [string]$abc = $args

    getUser($abc)

    Write-host Output is $output

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM