이러한 명령의 경우 파이프 라인을 만들고 스크립트에 입력하는 것이 좋습니다. 나는 이것의 좋은 모범을 발견했다. 이 코드와 그러한 프로젝트에 대한 자세한 내용은 here을 참조하십시오.
private string RunScript(string scriptText)
{
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
// add an extra command to transform the script
// output objects into nicely formatted strings
// remove this line to get the actual objects
// that the script returns. For example, the script
// "Get-Process" returns a collection
// of System.Diagnostics.Process instances.
pipeline.Commands.Add("Out-String");
// execute the script
Collection<psobject /> results = pipeline.Invoke();
// close the runspace
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
이 방법
깔끔하게 적절한 의견이 수행된다. 또한 Code Project 링크를 직접 다운로드하여 다운로드하여 시작할 수 있습니다!
감사합니다. –