나는 runspace에서 매개 변수를 사용하여 Powershell 파일을 실행하려고 C#으로 시도하고 있습니다. 불행히도 나는 다음과 같은 결과를 얻는다 :C# Runspace Powershell (대화 형)
A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.
나는 무엇을 할 수 있을까?
현재 C# 코드. 이 명령은 PS 파일에 있고 json 문자열을 리턴해야하는 명령을 실행해야합니다.
public string ExecuteCommandDirect(int psId, string psMaster, string psFile)
{
String FullPsFilePath = @"C:\CloudPS\" + psFile + ".ps1";
String PsParameters = FullPsFilePath + " -psId " + psId + " -psMaster " + psMaster + " -NonInteractive";
// Create Powershell Runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
// Create pipeline and add commands
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(PsParameters);
// Execute Script
Collection<PSObject> results = new Collection<PSObject>();
try
{
results = pipeline.Invoke();
}
catch (Exception ex)
{
results.Add(new PSObject((object)ex.Message));
}
// Close runspace
runspace.Close();
//Script results to string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
Debug.WriteLine(obj);
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
PS 코드 : 당신이 제몫을 pshost의 존재를 필요로 쓰기 호스트와 같은 중요하지 않은 cmdlet을 사용하는 스크립트를 실행해야하는 경우
param([int]$psId = 0, [string]$psMaster = 'localhost');
$date = Get-Date -Format 'h:m:s' | ConvertTo-Json;
Write-Host $date;
exit;
당신이 사용하고있는 코드를 게시 할 수 :
원래 cmdlet을 사용해야 할 경우, 이것을 사용? – DanM7
안녕하세요, 코드를 추가했습니다. – user2702653
'Write-Host'를 제거하려고하면'$ date' 만 남겨 두십시오. 'exit'도 필요하지 않습니다. –