2017-12-25 24 views
0

以下のコードを使用してこのアクティビティを達成する方法:runspaceにuseridとパスワードを渡すメソッドが見つかりません。異なる資格情報とパラメータを渡してPowershellスクリプトを呼び出す

  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(); 
} 
+1

の可能性のある重複した[PowerShellの - スクリプト1呼び出すスクリプト2 - スクリプト1に、スクリプト2から値を返す方法](HTTPS:/ /stackoverflow.com/questions/18534500/powershell-script-1-calls-script-2-how-to-return-value-from-script-2-to-scri) – SelakaN

答えて

0

//それは私のために働いたこのサンプルを使用してみてください

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 
関連する問題