VMware Cloud Community
Hemijimi
Contributor
Contributor
Jump to solution

Using an array for server list

I am trying to connect to my local ESXi hosts using an array that holds the host names. First I log into the vCenter and create the array, then try and connect using the array. I keep getting Invalid URI: The hostname could not be parsed.  What am I missing here? I do know that when I dump this to a txt file there seems to be white space at the end of each server name.

Connect-VIServer (MyvCenter) -User (My Username)-Password (My Password)

$esxiServers = get-vmhost | select-object Name

 

ForEach ($server in $esxiServers)

 

}

Connect-VIServer $server -User (My Username) -Password (My Password)

}

I am planning on using an encrypted password later. I am just trying to get this to work. 

Reply
0 Kudos
1 Solution

Accepted Solutions
vXav
Expert
Expert
Jump to solution

Actually I'm not sure why we are trying to use a txt file.

$Creds = Get-credentials 

$esxlist = (get-vmhost).name

$esxlist | foreach-object { 

     write-host $_ 

     connect-viserver $_ credential $creds 

     #Whatever command you need to run against each host 

     disconnect-viserver -confirm:$false

}



And if you want to export your host list


(get-vmhost).name >> esxlist.txt

View solution in original post

Reply
0 Kudos
10 Replies
jpsider
Expert
Expert
Jump to solution

This worked for me. The first time I did have to say "Y" for multiple connections.

$servers = @("10.10.0.XXX","10.10.0.XXX")

foreach ($server in $servers) {

  write-host $server

  connect-viserver $server -username XXXXXXXX -password "XXXXXXXX"

}

What is it that you are ultimately trying to solve?

Reply
0 Kudos
Hemijimi
Contributor
Contributor
Jump to solution

This is a larger (couple of hundred) locked down environment. I need to log into each host and grab a user list for a monthly audit review. I can create a txt file with the names and pull from there, but I wanted to make it so that if we made a change to the environment, like add a host or change the IP that I would still catch all the hosts.  

Reply
0 Kudos
jpsider
Expert
Expert
Jump to solution

Gotcha, when you export your list after running this command:

$esxiServers = get-vmhost | select-object Name

How are you saving that text file?

The reason I ask is that when you run that command you get an object back, not just a name.

How are you importing the .txt file after that?


In other words can I see more of your script?



Reply
0 Kudos
vXav
Expert
Expert
Jump to solution

Why not something like that. It's essentially what you wrote in your previous reply.

$Creds = Get-credentials

Get-content List-hosts.txt | foreach-object {

     write-host $_

     connect-viserver $_ credential $creds

     #Whatever command you need to run against each host

}


Just get the List-hosts.txt right.

Reply
0 Kudos
Hemijimi
Contributor
Contributor
Jump to solution

That does work, but I am trying to make this so I don't have to manage a server list. I can dump a list to a to a file, but then I get spaces at the beginning/end of the file and after each server name and it will not connect. I get "Invalid URI: hostname could not be parsed."  If I go into the file and delete the added spaces at the end of each server name, then I am okay and it will work. I tried to automate that with some success. I can get rid of the first three lines that are header information and I can get rid of the lines at the end of the file, but I can't seem to get rid of the spaces after each server entry?

Attached is what I had initially thought might work, but this does add spaces at the end of each server entry when it is dumped to a file that I can't seem to get rid of?

Reply
0 Kudos
jpsider
Expert
Expert
Jump to solution

ah, So when creating your text file, why not extract the name of the Server?

$servers = get-vmhost

foreach ($server in $servers) {

  write-host $server

  write-host $server.Name

}

Note that $server is an object, when you write that to text file you will get an array style data.

$server.Name is just a string.

Reply
0 Kudos
vXav
Expert
Expert
Jump to solution

Actually I'm not sure why we are trying to use a txt file.

$Creds = Get-credentials 

$esxlist = (get-vmhost).name

$esxlist | foreach-object { 

     write-host $_ 

     connect-viserver $_ credential $creds 

     #Whatever command you need to run against each host 

     disconnect-viserver -confirm:$false

}



And if you want to export your host list


(get-vmhost).name >> esxlist.txt

Reply
0 Kudos
Hemijimi
Contributor
Contributor
Jump to solution

I agree vXav, but when I tried to run the code it lists the server name and then asks me for the server name for every entry in the array. See attached.

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Credential parameter is missing a dash

$Creds = Get-credentials   

 

$esxlist = (get-vmhost).name 

$esxlist | foreach-object {   

     write-host $_   

     connect-viserver $_ -Credential $creds   

     #Whatever command you need to run against each host   

     disconnect-viserver -confirm:$false 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Hemijimi
Contributor
Contributor
Jump to solution

Thanks everyone for your help. With what LucD found and leaving the (s) off of the get-credentials, it works great.

Thanks again 

Reply
0 Kudos