Invoke-command error! What am I missing?

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

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

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: (:slight_smile: [Set-Item], InvalidOperationException
    • FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.SetItemCommand

Tim,

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

Thanks for the help!