0
몇 가지 질문이 있습니다. 아래 코드를 사용하여이 활동을 달성하는 방법 : runspace에서 사용자 ID와 암호를 전달하는 방법을 찾을 수 없습니다.다른 자격 증명을 가진 Powershell 스크립트를 호출하고 매개 변수를 전달하십시오.
- 다른 자격 증명으로 PowerShell 스크립트를 호출하는 방법은 무엇입니까?
- PowerShell 스크립트를 호출 할 때 인수를 전달하는 방법은 무엇입니까?
- 결과를 얻고 싶습니다.
아래 코드를 사용했지만 자격 증명을 전달할 수 없습니다.
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();
}
의 사용 가능한 복제 [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