401 Unauthorized

Copper Contributor

I'm using the following for powershell to connect to Exchange 2016 and it works most of the time but sometimes i keep on getting 401 unauthorized.  I would try running it on the server itself or from one Exchange to another with same results.   I wait a day, it'll start to work again.  Is this a bad syntax account issue? or is there a better way?  

 

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchangeserver1/PowerShell/ -Authentication Kerberos -ErrorAction SilentlyContinue -WarningAction SilentlyContinue


Import-PSSession -Session $Session -DisableNameChecking -AllowClobber| out-null

2 Replies
are you creating alot of new ps sessions or just a few? is exchangeserver1 a servername or is that an A record for a load balancer? are you on a terminal server that gets rebooted / logged out of often or are you connecting to a stale terminal server connection? Some of my initial thoughts.
I'm creating 1 session to run some mailbox cleanup via EWS API.
exchangeserver1 = actual server name
I'm rdp'd into the actual exchange server (tried going to server1 to server1 and server1 to server2).
Only this script under this account is behaving this way with compliance rights to delete emails from various mailboxes. Other scripts using other accounts (with less rights) are working as expected.