Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert 'System.Object[]' to the type 'System.String'

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.

like image 202
Dor Shamay Avatar asked Jun 22 '26 16:06

Dor Shamay


1 Answers

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.