8

I'm trying to establish a connection to a remote server that is not on Domain environment. When running the Set-Item alone on PowerShell console without the script it works for some reason on the script it doesn't.

$passwd = convertto-securestring -String <Password> -AsPlainText -Force 
$cred = New-Object -Typename System.Management.Automation.PSCredential -ArgumentList "developer", $passwd
$server = Read-Host -Prompt 'Input your server IP'

Set-Item wsman:\localhost\client\TrustedHosts -Value $server -Force

$session = new-pssession -computername $server -credential $cred

When I run the above code, I receive the message

Set-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by the parameter. Specified method is not supported.

1 Answer 1

9

When you are passing the input here, it should be a string value and a single object, if there is a comma or something, then the $server no more remains as string. To do a forceful conversion, you can convert the $server explicitly to String type by type casting:

[String]$server = Read-Host -Prompt 'Input your server IP'

Further,

I would like you to hardcode the server name in the Set-Item and see if you are getting the error. You will not get it mostly.

Set-Item wsman:\localhost\client\TrustedHosts -Value 'YourServerHostname' -Force

So, validate while taking the input that what kind of input you are getting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.