기본적으로 매개 변수를 가져 와서 해당 매개 변수를 사용하는 공유 드라이브를 만드는 원격 서버에서 powershell 스크립트 파일을 호출하려고합니다. 자격 증명은 모두 정확하지만이를 실행할 때마다이 오류가 반환C에서 다른 자격 증명을 사용하여 원격 powershell 스크립트 호출 #
When the runspace is set to use the current thread the apartment state in the invocation settings must match that of the current thread
그것은 내가 자격 증명을 제거 한 번으로 자격 증명을 함께 할 수있는 뭔가가를, 내 로컬 컴퓨터에 잘 실행됩니다. 누구든지이 문제에 관해 밝힐 수 있습니까?
PSCredential credential = new PSCredential(_exchangeUsername, password);
// Set exchange connection details
WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(_exchangeConnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;
string cmdArg = @"\\servername\\c$\\scripts\\HomeDrive.ps1 "+userID;
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
try
{
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(cmdArg);
pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
Collection<PSObject> results = pipeline.Invoke();
var error = pipeline.Error.ReadToEnd();
// Check for powershell command errors
if (error.Count > 0)
{
throw new Exception(errorMessage.ToString());
}
// Check for powershell command results
if (results.Count <= 0)
{
throw new Exception(String.Format("Error. No results after command invoked.", userID));
}
runspace.Close();
}
catch (Exception ex)
{
runspace.Close();
throw new ApplicationException(String.Format("Error ", userID), ex);
}
}
그것은'runspace.ApartmentState = 시스템을 좋아하지 않는다 (나는 생각한다) 내가 생성자의 가장 자세한 정보를 사용하고있는 곳이 잘 작동 내 자신의 코드에서 조각입니다. Threading.ApartmentState.STA', 현재 스레드가 STA 스레드가 아니기 때문에. 이 줄을 제거하거나 위의 줄을 제거해야합니다 ... –
줄 중 하나를 제거하면 "지정 방법이 지원되지 않습니다."라는 오류가 발생합니다. – user2360891
지원되지 않는 방법은 무엇입니까? –