C#을 사용하여 .Net 응용 프로그램에서 PowerShell과의 연결을 만들려고했습니다. 세션을 만들려고 할 때 연결이 완료되면 빈 콜렉션을 반환합니다.C#을 사용하여 PowerShell 원격 세션 만들기
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
PSCredential remoteCredential = new PSCredential("userID", StringToSecureString("Password"));
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "Ip Address of server", 5985, "/wsman", shellUri, remoteCredential, 1 * 60 * 1000);
runspace = RunspaceFactory.CreateRunspace(connectionInfo);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddScript(@"$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://servername/poweshell -Credential " + remoteCredential);
//result count returned is 0
var result = ps.Invoke();
ps.Commands.Clear();
ps.AddCommand("Import-PSSession $Session");
ps.Invoke();
}
당신이 어떤 결과를 기대하고 있습니다? – PetSerAl