I hope somebody can help me, I have the following function to do remote calls to a powershell server:
private static string DoCall()
{
string resultPs = string.Empty;
string serverName = "myserver.com";
string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
System.Uri serverUri = new Uri(String.Format("http://{0}/powershell?serializationLevel=Full", serverName));
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(serverUri, SHELL_URI, (PSCredential)null);
//connectionInfo = new WSManConnectionInfo(serverUri, SHELL_URI, PSCredential.Empty);
//connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
try
{
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
PowerShell powershell = PowerShell.Create();
powershell.Runspace = runspace;
runspace.Open();
powershell.AddScript("Get-DatabaseAvailabilityGroup");
Collection<PSObject> results = powershell.Invoke();
if (powershell.Streams.Error.Count > 0)
{
foreach (ErrorRecord err in powershell.Streams.Error)
{
resultPs += (err.ErrorDetails.Message != null ? err.ErrorDetails.Message : "");
}
}
foreach (PSObject result in results)
{
foreach (PSPropertyInfo propertyInfo in result.Properties)
{
resultPs += "Property:" + propertyInfo.Name + " Value:" + propertyInfo.Value;
}
}
powershell.Runspace.Close();
}
}
catch (Exception ex)
{
resultPs = ex.Message + " " + ex.StackTrace + " innerException: " + (ex.InnerException != null ? (ex.InnerException.Message ?? "") : "");
}
return resultPs;
}
}
If I run this code within Visual Studio in a console application, my credentials are used and the exchange call is done as expected, however, if I publish this code in a WCF service and set the application pool to use my credentials I receive the following exception
"An internal error occurred. \0 at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager.Initialize(Uri connectionUri, WSManConnectionInfo connectionInfo)\r\n at System.Management.Automation.Remoting.Client.WSManClientSessionTransportManager..ctor(Guid runspacePoolInstanceId, WSManConnectionInfo connectionInfo, PSRemotingCryptoHelper cryptoHelper)\r\n at System.Management.Automation.Remoting.ClientRemoteSessionDSHandlerImpl..ctor(ClientRemoteSession session, PSRemotingCryptoHelper cryptoHelper, RunspaceConnectionInfo connectionInfo, URIDirectionReported uriRedirectionHandler)\r\n at System.Management.Automation.Remoting.ClientRemoteSessionImpl..ctor(RemoteRunspacePoolInternal rsPool, URIDirectionReported uriRedirectionHandler)\r\n at System.Management.Automation.Internal.ClientRunspacePoolDataStructureHandler..ctor(RemoteRunspacePoolInternal clientRunspacePool, TypeTable typeTable)\r\n at System.Management.Automation.Runspaces.Internal.RemoteRunspacePoolInternal..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo)\r\n at System.Management.Automation.Runspaces.RunspacePool..ctor(Int32 minRunspaces, Int32 maxRunspaces, TypeTable typeTable, PSHost host, PSPrimitiveDictionary applicationArguments, RunspaceConnectionInfo connectionInfo)\r\n at System.Management.Automation.RemoteRunspace..ctor(TypeTable typeTable, RunspaceConnectionInfo connectionInfo, PSHost host, PSPrimitiveDictionary applicationArguments)\r\n at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(RunspaceConnectionInfo connectionInfo, PSHost host, TypeTable typeTable, PSPrimitiveDictionary applicationArguments)\r\n at TestingRemote.Service.DoCall() in "
I have checked several blogs about passing the credentials but I haven't found someone with the same exact issue and that has solved mine. I know I can use an overloaded method for initializing the Credentials, but I want to use the ones in the app pool.