• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1033
  • Last Modified:

Problems running powershell script

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.
0
dillonconsulting
Asked:
dillonconsulting
1 Solution
 
XaelianCommented:
Can you attach the Powershell script so we can modify it to run perfectly?
0
 
AkulshCommented:
Use this tool c:\windows\system32\sconfig  to enable remote management.

See this article for more details:
http://blogs.msdn.com/b/virtual_pc_guy/archive/2010/11/11/configuring-remote-management-of-hyper-v-server-in-a-workgroup.aspx
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
dillonconsultingAuthor Commented:
Here is the script
New-Mailbox -Name "777777" -UserPrincipalName "777777@ourdomain.com" -database "database" -Org "ou=Email Accounts,DC=ourdomain,DC=com" -Password (ConvertTo-SecureString "sup123" -AsPlainText -Force) -RetentionPolicy "ArcMail Retention Policy"
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
dillonconsultingAuthor Commented:
It is on a server here is the code that runs the script

    Public Shared Function RunScriptRemote(ByVal scriptText As String) As String
        Try
                  'Powershell script as following
                  'New-Mailbox -Name "123457" -UserPrincipalName  "123457@xxxx.ca" -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
                ssRunasPassword.AppendChar(a)
            Next
            Dim credentials As PSCredential = New PSCredential(runasUsername, ssRunasPassword)

            Dim Conninfo = New WSManConnectionInfo(New Uri("http://xxxxx.xxxxx.ca/PowerShell/?serializationLevel=Full"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", 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

            command.AddCommand("get-mailbox")
            powershell.Commands = command
            powershell.Commands.AddScript(scriptText)
            ' open it
            myRunSpace.Open()

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

            ' close the runspace
            myRunSpace.Close()

            myRunSpace.Dispose()

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

            For Each obj As PSObject In results
                MyStringBuilder.AppendLine(obj.ToString())
            Next

            ' 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
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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?
0
 
dillonconsultingAuthor Commented:
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?

Thanks
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
dillonconsultingAuthor Commented:
We adjusted the Remoting rights and it is working now
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now