Problems running powershell script

Posted on 2012-08-15
Last Modified: 2013-12-04
I am trying to run a powershell script from .Net framework 4.0 and I am getting this error.  I have checked and made sure that there are no firewalls and I need another solution.  Any help you can give me would be great!

Connecting to remote server failed with the following error message : The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. You can get more information about that by running the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.
Question by:dillonconsulting
    LVL 13

    Expert Comment

    Can you attach the Powershell script so we can modify it to run perfectly?
    LVL 3

    Expert Comment

    Use this tool c:\windows\system32\sconfig  to enable remote management.

    See this article for more details:
    LVL 67

    Expert Comment

    That message is caused by a missing or wrong WinRM configuration (as hinted in the error message). WinRM is used for executing PowerShell commands (a) remotely or (b) locally as a background task (with Start-Job).

    In most cases it is sufficient to run   winrm quickconfig   on the machine the script is targeted at. But there are some steps more to take if you are not using the same domain (or a domain at all).

    Showing us the script (or the relevant part of it) will help us to help you.

    Author Comment

    Here is the script
    New-Mailbox -Name "777777" -UserPrincipalName "" -database "database" -Org "ou=Email Accounts,DC=ourdomain,DC=com" -Password (ConvertTo-SecureString "sup123" -AsPlainText -Force) -RetentionPolicy "ArcMail Retention Policy"
    LVL 67

    Expert Comment

    Did you try that on a client or on a server with an Exchange role? If the former, try on a server, and if that works we can work out a way for you to execute it on a client.

    Author Comment

    It is on a server here is the code that runs the script

        Public Shared Function RunScriptRemote(ByVal scriptText As String) As String
                      'Powershell script as following
                      'New-Mailbox -Name "123457" -UserPrincipalName  "" -database xxxxx -OrganizationalUnit "ou=xxxxx,DC=xxxx,DC=ca" -Password (ConvertTo-SecureString "sup123" -AsPlainText -Force) -RetentionPolicy "ArcMail Retention Policy"
                      Dim runasUsername As String = "xxxxxxxx"
                Dim runasPassword As String = "xxxxxxxx"

                Dim ssRunasPassword As SecureString = New SecureString
                For Each a As Char In runasPassword
                Dim credentials As PSCredential = New PSCredential(runasUsername, ssRunasPassword)

                Dim Conninfo = New WSManConnectionInfo(New Uri(""), "", credentials)

                Conninfo.AuthenticationMechanism = AuthenticationMechanism.Basic
                '' create Powershell runspace
                Dim myRunSpace As Runspace
                myRunSpace = RunspaceFactory.CreateRunspace(Conninfo)

                Dim powershell As PowerShell
                powershell = powershell.Create()

                Dim command As PSCommand = New PSCommand

                powershell.Commands = command
                ' open it

                powershell.Runspace = myRunSpace
                Dim results As Collection(Of PSObject) = powershell.Invoke()

                ' close the runspace


                ' convert the script result into a single string
                Dim MyStringBuilder As New StringBuilder()

                For Each obj As PSObject In results

                ' return the results of the script that has
                ' now been converted to text
                Return MyStringBuilder.ToString()
            Catch ex As Exception
                Return ex.Message.ToString
            End Try

        End Function
    LVL 67

    Expert Comment

    The issue might be that you change the user context, and then try to execute the PS script. The standard authentication mechanisms allowed when using remote commands are very restricted.

    Does it work if you execute that on in an interactive PS session?

    Author Comment

    Yes. It is working in an interactive PS session.

    I did some configuration on Winrm services today. I run the script again. Now I got an error like "Connecting to remote server failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic. "

    Do you know what kind of rights that user can create the emailbox in exchange server?

    LVL 67

    Accepted Solution

    It's more about Remoting rights and config than Exchange privileges. Read the recommended help page (get-help about_Remoting_Troubleshooting), it explains the requirements for successful user authentication when using WinRM.

    Author Closing Comment

    We adjusted the Remoting rights and it is working now

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
    Set OWA language and time zone in Exchange for individuals, all users or per database.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now