1

Im trying to access the exchange server using powershell in c# Here is the code i am trying to use

            WSManConnectionInfo ConnectionInfo = new WSManConnectionInfo(new Uri(@"http://myexchangeserver.com"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", (PSCredential)null);
            ConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
            using (Runspace runspace = RunspaceFactory.CreateRunspace(ConnectionInfo))
            {
                using (PowerShell PS_Instance = PowerShell.Create())
                {
                    PS_Instance.AddCommand("Get-MailboxPermission -Identity UAT_TestGeneric");

                    runspace.Open();
                    PS_Instance.Runspace = runspace;
                    Collection<PSObject> results = PS_Instance.Invoke();

                    if (PS_Instance.Streams.Error.Count > 0)
                    {
                        //Couldnt Connect To Server
                    }
                    else
                    {
                        //Do Stuff
                    }
                }

i keep getting this error message

The WinRM client received an HTTP status code of 403 from the remote WS-Management service. For more information, see the about_Remote_Troubleshooting Help topic.

I have checked the exchange servers powershell IIS folder and it doesnt require SSL as indicated here https://support.microsoft.com/en-au/kb/2276957.

Is there any one out there that can assist? Thanks.

New Bee
  • 991
  • 1
  • 13
  • 24
  • You might want to try just connecting using Powershell first to test you have the correct prereqs and also check what Auth is correct etc https://technet.microsoft.com/en-us/library/dd297932(v=exchg.141).aspx – Glen Scales Nov 22 '16 at 06:42

2 Answers2

1

As an error 403 means nothing else as "access denied" I think that remote powershell isn´t enabled for the taskuser you are using. Try to enable that via (as written in the MS documentation here):

set-user <your user account> -RemotePowerShellEnabled $True

Another useful source to troubleshoot your issue is the MS documentation here.

BastianW
  • 2,628
  • 7
  • 29
  • 38
0

Incase anyone else sees this. I fixed this because the URI is wrong

WSManConnectionInfo ConnectionInfo = new WSManConnectionInfo(new Uri(@"http://myexchangeserver.com"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", (PSCredential)null);

The uri needs to be @"http://myexchangeserver.com/Powershell". So although powershell was setup corretly on the server you need to connect to the correct IIS folder path

New Bee
  • 991
  • 1
  • 13
  • 24