I am trying use Read-Host -Prompt to fill out multiple server names and user account names, and then pass those into the invoke-command to complete the task - which is to make someone local admin. I am unable to figure out why PowerShell throws me this error: "One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects instead of strings.".
In addition, I am also getting the error:
"Add-LocalGroupMember : Object reference not set to an instance of an object."
Code example below:
if ($OptPrompt -eq 1) {
$prompt1 = Read-Host -Prompt 'Test Accounts'
$prompt1.Split(",")
$prompt1_1 = Read-Host -Prompt 'Test Servers'
$prompt1_1.Split(",")
Invoke-Command -ComputerName $prompt1_1 -ScriptBlock {Add-LocalGroupMember -Group 'Administrators' -Member $Using:prompt1}
}
More info:
Servers are online
Servers have "-" in them, for example, "Server-22-test"
Servers are responding to ping (hostname and IP address)
Using Invoke-Command on its own works, but not inside the function
There is no firewall or security blocking the account(s) in question
Thank you for your help.
$prompt1_1.Split(",")does not update$prompt1_1. It only outputs the split method result. You need$prompt1_1 = $prompt1_1.Split(",")first. The same applies for$prompt1 = $prompt1.Split(",")$testAccounts = $testAccounts.Split(",")