I have a new user/new email powershell script.
In the new email part i try to connect to exchange.server via a remote session to run a few commands to create the email part. Sometimes it runs without an issue, but every now and then i get the following error.
[exchange.domain.com] Connecting to remote server exchange.server.com failed with the following error message : The WinRM client sent a request to an HTTP server and got a response saying the requested HTTP URL was not available. This is usually returned by a HTTP server that does not support the WS-Management protocol. For more information, see the about_Remote_Troubleshooting Help topic.
I have scoured the internet for solutions, checked every thing i can check and then it runs without this error 99 times and randomly comes back with the error 10 times in a row. It doesnt matter if someone is connected to the server. The winrm check
I have tried Disconnect PSSESSION on the times it does work, no success i have changed the entire code block no success. I tried using UPN instead of SAMAccountName, no success.
I can run the block on the exchange server without a problem.
This is an example of the code i have currently
#Region create mail
# Prompt for admin credentials
$admin = Get-Credential -Message "Enter your admin credentials using your admin UPN"
# Variables
$exchangeServer = "exchange.domain.com"
$userUPN = $UserPrincipalName
$mailAddress = $UserPrincipalName
$user = $SamAccountName
# Error handling
try {
# Connect to the Exchange server
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$exchangeServer/PowerShell/" -Authentication Kerberos -Credential $admin
Import-PSSession $session -DisableNameChecking -AllowClobber
# Enable remote mailbox
Enable-RemoteMailbox -Identity $userUPN -RemoteRoutingAddress "${user}@company.onmicrosoft.com"
# Set remote mailbox settings with correct SMTP and disable Email Address Policy
Set-RemoteMailbox -Identity $mailAddress -PrimarySmtpAddress $UserPrincipalName -EmailAddressPolicyEnabled $false
# Display results or perform additional actions as needed
$remoteMailbox1 = Get-RemoteMailbox -Identity $userUPN
$remoteMailbox2 = Get-RemoteMailbox -Identity $mailAddress
# Output the results
Write-Output "Remote mailbox for ${userUPN}:"
Write-Output $remoteMailbox1
Write-Output "Remote mailbox for ${mailAddress}:"
Write-Output $remoteMailbox2
} catch {
Write-Error "An error occurred: $_"
} finally {
# Disconnect from the Exchange session
if ($session) {
Remove-PSSession $session
}
}
#Endregion
Start-Sleep -Seconds 5
Winrm config says max connections are 300 and the server trying to connect to exchange also is a trustedhost.
PS C:Windowssystem32> winrm get winrm/config
Config
MaxEnvelopeSizekb = 500
MaxTimeoutms = 60000
MaxBatchItems = 32000
MaxProviderRequests = 4294967295
Client
NetworkDelayms = 5000
URLPrefix = wsman
AllowUnencrypted = false
Auth
Basic = true
Digest = true
Kerberos = true
Negotiate = true
Certificate = true
CredSSP = false
DefaultPorts
HTTP = 5985
HTTPS = 5986
TrustedHosts = serverItryToConnecttowardsExchange.domain.com
Service
RootSDDL =
MaxConcurrentOperations = 4294967295
MaxConcurrentOperationsPerUser = 1500
EnumerationTimeoutms = 240000
MaxConnections = 300
MaxPacketRetrievalTimeSeconds = 120
AllowUnencrypted = true
Auth
Basic = false
Kerberos = true
Negotiate = true
Certificate = false
CredSSP = false
CbtHardeningLevel = Relaxed
DefaultPorts
HTTP = 5985
HTTPS = 5986
IPv4Filter = *
IPv6Filter = *
EnableCompatibilityHttpListener = false
EnableCompatibilityHttpsListener = false
CertificateThumbprint
AllowRemoteAccess = true
Winrs
AllowRemoteShellAccess = true
IdleTimeout = 7200000
MaxConcurrentUsers = 2147483647
MaxShellRunTime = 2147483647
MaxProcessesPerShell = 2147483647
MaxMemoryPerShellMB = 2147483647
MaxShellsPerUser = 2147483647
I have no idea what causes this error and google/chatgpt are of no help.
What i tried is adding a dc/remove ps session cause i thought it would remove and dc the sessions. It worked at first but was a placebo cause after 99 new users 10 users would give me the error again.
Also waiting a few moments or days between creating would resolve the issue.
FinFinnigan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.