저는 C#의 새로운 powershell 스크립트입니다. 나는 ConsoleSettings.psc1configfile, 매개 변수로 powershell 스크립트를 호출하는 방법 #
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -psconsolefile "D:\e\ConsoleSettings.psc1" -noexit -command ". 'D:\e\ps.ps1'"
settingfile PowerShell 스크립트 파일 ps.ps1 및 PowerShell을
실행을 갖고 "
은 Get-RST -SearchRoot 'ERD/사용자 -PasswordNeverExpires 얻을 : $ false로 을 - 60 PasswordNotChangedFor 제대로
내 함수 결과를 -enabled.
을 이제 내가 C#에서이 결과를 얻으려면. 내 코드는;
private void button1_Click(object sender, EventArgs e)
{
RunScript(LoadScript(@"d:\e\ps.ps1"));
}
private string RunScript(string scriptText)
{
PSConsoleLoadException x = null; ;
RunspaceConfiguration rsconfig = RunspaceConfiguration.Create(@"d:\e\ConsoleSettings.psc1", out x);
Runspace runspace = RunspaceFactory.CreateRunspace(rsconfig);
runspace.Open();
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Get-RST -SearchRoot 'erd/user' -PasswordNeverExpires:$false -PasswordNotChangedFor 60 -enabled");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
private string LoadScript(string filename)
{
try
{
using (StreamReader sr = new StreamReader(filename))
{
StringBuilder fileContents = new StringBuilder();
string curLine;
while ((curLine = sr.ReadLine()) != null)
{
fileContents.Append(curLine + "\n");
}
return fileContents.ToString();
}
}
catch (Exception e)
{
string errorText = "The file could not be read:";
errorText += e.Message + "\n";
return errorText;
}
}
그리고 내가 오류가 있습니다 용어를 "GET-RST -SearchRoot 'ERD/사용자 -PasswordNeverExpires을 : $ 거짓 -PasswordNotChangedFor 60 -enabled"cmdlet에, 함수의 이름으로 인식되지 않습니다 , 스크립트 파일 또는 작동 가능한 프로그램. 이름의 철자를 확인하거나 경로가 포함되어 있으면 경로가 올바른지 확인하고 다시 시도하십시오. 이 문제를 해결하는 방법
, 또는 같은 파워 쉘의에서 configFile와 스크립트, 매개 변수를 호출하는 방법 (-SearchRoot 'ERD/사용자 -PasswordNeverExpires-RST를 가져옵니다 : $ -PasswordNotChangedFor 60 사용이 가능한 false)를 C#에서도와주세요 ...
'File.ReadAllText (파일 이름)' – SLaks
내가 호출 단계에서 문제를 가지고, 내가이 loadscript 함수 반환 값을 명확하게 understund 해달라고,하지만 같은 문제가 계속 '컬렉션 결과를 = pipeline.Invoke를 (반환); ' –