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.

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.