Invoke-command error! What am I missing?

This topic contains 3 replies, has 2 voices, and was last updated by  Cody Everingham 1 year, 7 months ago.

  • Author
    Posts
  • #34421

    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

    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

    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

    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.