How can i resolve this inconsequent error on the mail server?

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.

New contributor

FinFinnigan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật