2009
05.13

I got handed a list of around 1000 servers today, and asked if any of them were part of our VI environment.

Rather than work through it by hand, I wrote the following :

# Check if Servers on Text List exist on VMware
# Assumes that the VM object name matches the server's DNS name
 
# Set this to the text file containing the list of servers, one per line
$strServerList = "C:\path\to\textFile.txt"
 
# Create empty array for servers which are found
$arrFoundServers = @()
 
# Assign all of the VM objects to a variable
$objVMs = Get-VM
 
# Read the list of servers, and assign it to a variable
$strServersOnList = (Get-Content $serverlist)
 
# Loop through each VM
forEach ($objVM in $objVMs){
	# Loop through each server on the list
	forEach ($strServer in $strServersOnList){
		# If the current VM object name matches the current item on the list
		if ($objVM.Name -Like $strServer){
			# Add it to the array of found machines
			$arrFoundServers += $objVM
		}
	}
}
 
# Display the list of found machines
$arrFoundServers

If your VM Object names do not match the DNS names on the list, then this won’t work, but I suppose you could combine this with some logic from the script I wrote to find mismatches.

No Comment.

Add Your Comment