Invoke-command error! What am I missing?

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Cody Everingham Cody Everingham 10 months, 2 weeks ago.

  • Author
    Posts
  • #34421
    Profile photo of Cody Everingham
    Cody Everingham
    Participant

    Here's the code:

    Invoke-Command -ComputerName 10.9.4.140 -Credential 'mydomain\UserName' -ScriptBlock {
    $domain = "mydomain"
    $user = "domainuser"
    $password = Read-Host -Prompt "Enter password for $user" -AsSecureString
    $username = "$domain\$user"
    $credential = New-Object System.Management.Automation.PSCredential($username,$password)
    Add-Computer -DomainName $domain -Credential $credential
    }

    Here's the error:
    [10.9.4.140] Connecting to remote server 10.9.4.140 failed with the following error message : The WinRM client cannot process the request. Default
    authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts
    list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts.

    More info:

    It is Pingable
    It does have WinRM installed

  • #34422
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hi Cody,

    If you are referring to a computer by its IP address instead of its hostname, it is treated differently. You will need to set TrustedHosts to include the IP address. You can change this by doing the following on your own system.

    Set-Item -Path WSMAN:\localhost\Client\TrustedHosts -Value 10.9.4.140 -Force
  • #34427
    Profile photo of Cody Everingham
    Cody Everingham
    Participant

    Tim,

    Very helpful, I got it to work! However, right now I would have to go back and change the script every time I would want to target another computer. when I use:

    $targetIP = 10.9.4.140
    Set-Item -Path WSMAN:\localhost\Client\TrustedHosts -Value $targetIP -Force

    I get the issue:
    Set-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by the parameter. Specified method is not supported.
    At line:4 char:1
    + Set-Item -Path WSMAN:\localhost\Client\TrustedHosts -Value $targetIP -Force
    + CategoryInfo : NotSpecified: (:) [Set-Item], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.SetItemCommand

  • #34428
    Profile photo of Cody Everingham
    Cody Everingham
    Participant

    Tim,

    Never-mind, this was my bad. I needed to put that IP address in as a string.

    Thanks for the help!

You must be logged in to reply to this topic.