visual studio - Enable mail box from C# powershell -


the code enable mail box on prem exchange server works when run in debug mode visual studio fails when deployed on iis on same instance.the commands work powershell console throws exception cannot connect server access denied.

could 1 pls me this?

pfb snippet works fine when run visual studio debug mode , fails when run code deployed on iis

 string connectionuri = strconuri;   string loginpassword = pwd;            securestring secpassword = new securestring();            foreach (char c in loginpassword)            {                secpassword.appendchar(c);            }            pscredential credential = new pscredential(usercred, secpassword);              runspace runspace = system.management.automation.runspaces.runspacefactory.createrunspace();             powershell powershell = powershell.create();             pscommand command = new pscommand();             command.addcommand("new-pssession");             command.addparameter("configurationname", "microsoft.exchange");             command.addparameter("connectionuri", new uri(connectionuri));             command.addparameter("credential", credential);             command.addparameter("authentication", "basic");         //    command.addcommand("set-executionpolicy remotesigned");             powershell.commands = command;               runspace.open();             powershell.runspace = runspace;             collection<system.management.automation.psobject>                  result = powershell.invoke();              if (powershell.streams.error.count > 0 || result.count != 1)             {                  throw new exception("failed");             }                   powershell = powershell.create();                 command = new pscommand();                 command.addcommand("invoke-command");                  const string scriptblock = "get-user {0} | enable-remotemailbox -remoteroutingaddress {1};";                string scriptblockstr = string.format(scriptblock, getuser, mailboxuser);                 command.addparameter("scriptblock", system.management.automation.scriptblock.create(scriptblockstr));                 command.addparameter("session", result[0]);                 powershell.commands = command;                 powershell.runspace = runspace;                 var mailboxes = powershell.invoke(); 

as not know how ms exchange server configured , how application triggered not easy troubleshoot that.

at first keep noted depending on configuration use ports 5985 (http winrm), 5986 (https winrm) or 443 or 80 (as explained microsoft here) or port might have configured. when use new-pssession computername 5985/5986 used. if connectionuri port 80 or 443 used (see here more infos).

with in mind here basic troubleshooting steps check:

  1. check if using https or http connectionuri , if there difference between them , if 1 working expected
  2. the best way use https (via computername or connectionuri) eliminate "security implementations" issues https forced , breaks if try bypass https. configure https winrm (via 5986) use (check here more details):

winrm quickconfig -transport:https

because if using winrm , if https not transport, target remote computer must configured in list of trusted host computers (see below , here more infos).

  1. if use https (either via computername or connectionuri) make sure server solution running on trusting connection. if using connectionuri, enter url in browser on non exchange server , check ssl certificate issue. make sure use full qualified domain name here.
  2. if configured on non exchange server proxy make sure connection bypassing proxy (see here) avoid connection tried done via proxy isn´t way have.
  3. check telnet if port affected server towards ms exchange server open (see port information @ top posting)
  4. depending on configuration might need use different authentications (e.g. negotiate, basic, kerberos, ...). make sure use correct authentication here situation. keep noted kerberos works in domain context, means non exchange server must added same activedirectory domain! test connection can use powershell (via run different user) , might bypass ssl validation checks (skipcacheck, skipcncheck, skiprevocationcheck) during connection check. see below examples (more info's here) check http , https options:

$usercredential = get-credential $session = new-pssession -configurationname microsoft.exchange -connectionuri http://<fqdn of exchange mailbox server>/powershell/ -authentication negotiate -credential $usercredential -skipcacheck -skipcncheck -skiprevocationcheck import-pssession $session

or if know authentication should used use:

$usercredential = get-credential $session = new-pssession -configurationname microsoft.exchange -connectionuri http://<fqdn of exchange mailbox server>/powershell/ -authentication kerberos -credential $usercredential -skipcacheck -skipcncheck -skiprevocationcheck import-pssession $session

or

$usercredential = get-credential $session = new-pssession -configurationname microsoft.exchange -connectionuri https://<fqdn of exchange mailbox server>/powershell/ -authentication basic -credential $usercredential -skipcacheck -skipcncheck -skiprevocationcheck import-pssession $session

  1. is remote powershell enabled taskuser using?

set-user yourtaskuser -remotepowershellenabled $true

  1. make sure ms exchange powershell directory configured required , (for example expected authentication methods configured). start with:

get-powershellvirtualdirectory "exchange2010\powershell (default web site)"

  1. in cases, able work remote computers in other domains. however, if remote computer not in trusted domain, remote computer might not able authenticate credentials. enable authentication, need add remote computer list of trusted hosts local computer in winrm (see here). so, type:

    winrm s winrm/config/client '@{trustedhosts="remotecomputer"}'

  2. check if authentication (= basic) changed or if allowunencrypted set true. both isn´t default setup , if done might cause unexpected issues (beside fact limits security).

  3. you can use test-wsman check if basic and/or kerberos authentication working expected (via http or https , winrm port configured/use). here examples:

test-wsman -computername https://server2008:5986 -auth basic -cred b\my_user_name

and/or

test-wsman -computername https://server2008:5986 -auth kerberos

  1. when using winrm make sure taskuser added local os winrmremotewmiusers group, because per default winrm restricted users in group or users in local administration group (see here).

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -