VMware Cloud Community
CITITECHS
Contributor
Contributor
Jump to solution

Checking if host are connected to Virtual Center not from a cmdlet but from a file

Sometimes I have to perform work on a list of host that are part of Virtual Center. But are aren't organized by clusters or folders or any cmdlet that I can call them by.

So at times I run scripts based on user generated host list below is an example piece of code of how I do work against the host list.

$list = Get-Content "C:\Program Files\PowerGUI\goodhost1.txt"

ForEach ($server in $list) {( $luns = Get-ScsiLun -Server $vcs -VmHost $server

Sometimes these host list can come from a database. Sometimes the database has the hostname misspelled. So before doing any work on the host I would ping them and generate a list of ping able host and non ping able host. Usually the ones that weren't ping able had typo's in the hostname, and I would easily correct those and put them into a master host list.

So for the most part my script runs when I have a master host list . But when the host is not connected in Virtual Center my script breaks. Then I have to go and modify my master host list and take it out the host's the script already ran on including the one that wasn't connected and rerun the script. Kind of a pain.

So what am looking for is a code example or an idea on how to check if my host list against virtual center to see if its already connected.

When working with cmdlets I use something like this

get-vmhost | where { $_.State -eq "Connected" }

Could I do something similar ? With my own list.

Thanks

0 Kudos
1 Solution

Accepted Solutions
hugopeeters
Hot Shot
Hot Shot
Jump to solution

Oh sure!

Get-VMHost | Where {((Get-Content "D:\scripts\hostsfile.txt") -contains $_.Name) -and ($_.State -eq "Connected")}

Hugo

PeetersOnline.nl

View solution in original post

0 Kudos
3 Replies
hugopeeters
Hot Shot
Hot Shot
Jump to solution

Oh sure!

Get-VMHost | Where {((Get-Content "D:\scripts\hostsfile.txt") -contains $_.Name) -and ($_.State -eq "Connected")}

Hugo

PeetersOnline.nl

0 Kudos
CITITECHS
Contributor
Contributor
Jump to solution

Thanks Hugo for the help. That was what I was looking for.

I also saw your video on

Already using your scripts. I like the Compare-Cluster.

0 Kudos
hugopeeters
Hot Shot
Hot Shot
Jump to solution

You're welcome.

Very nice to hear that!

Greetings, Hugo

0 Kudos