VMware Cloud Community
Munster99
Enthusiast
Enthusiast
Jump to solution

Finding VMs without a VC

Hey All

Was wondering if you could help please ?

I recently lost my VC (which is a VM) and then realised i didnt know what host it was on to get it back up and running. Hence, to make sure it doesnt happen again I tried to create a script (a basic one at that) just to tell me what host the 'vc' is on should an issue occur again. The problem is the script keeps looping and erroring when it trys to connect to a host. The script is as below and any help would be greatly appreciated.

Munster


$rootcred = Get-Credential

$esxservers = Get-Content c:\scripts\prodservers.txt
$vm = Read-Host "Enter VM Name: "

foreach ($esx in $esxservers)
    {
    Connect-VIServer -Server $esx -cred $rootcred
        (Get-VM $vm)
        if($vm){
            Write-Host $vm "is located on: " $esx -ForegroundColor White
        }
        else{
            Write-Host $vm "is NOT located on: " $esx -ForegroundColor DarkGreen   
        }
     Disconnect-VIServer -Server $esx -Confirm:$false       
     }   

0 Kudos
1 Solution

Accepted Solutions
Lessi001
Enthusiast
Enthusiast
Jump to solution

Hi,

I had the same problem like you and i solved it with using DRS Host Group Manager. So vCenter Server and the Data Base Server is always running on a dedicated host.

I have published a How-to post on my blog http://www.vmworld.net/?p=1240, but it is written in German - so here a short explanation:

.) make a virtual machines DRS Group - member vCenter Server + DB Server

.) make a Host DRS Group - member ESXHost where you will have the vc to run

.) make a Rule Virtual Machines to Host where "VM Group" ->  "should run on hosts in group" -> Host Group

Important: use "should run on hosts in group" and NOT "must run ...." because so it is HA and DRS aware.

So will know every time where your vc is running...

Regards

Andi

There are 10 types of people in this world. Those who understand binary, and those who do not.

View solution in original post

0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

Perhaps try like this

$rootcred = Get-Credential
$esxservers = Get-Content c:\scripts\prodservers.txt 

$vmName
= Read-Host "Enter VM Name: " foreach ($esx in $esxservers) {   Connect-VIServer -Server $esx -cred $rootcred
 
$vm = Get-VM -Name $vmName
 
if($vm){     Write-Host $vm "is located on: " $esx -ForegroundColor White
  }  
else{     Write-Host $vm "is NOT located on: " $esx -ForegroundColor DarkGreen   }   Disconnect-VIServer -Server $esx -Confirm:$false }


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

Munster99
Enthusiast
Enthusiast
Jump to solution

Perfect LucD (helpful as ever)

Sorry to be a pain but I was wondering ...... When i do run this it returns two issues i would rather try and get rid of they are :

1 - in the PS prompt everytime it connects to a host it shows it on the console screen - is there a way to get rid of this ??

2 - When the VM is NOT found on a host - is there a way to suppress the error so it does not show up on the console screen (i.e just shows the message) ???

(Any pointers or tips would be greatly)

Munster

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try these adjustments

$rootcred = Get-Credential
$esxservers = Get-Content c:\scripts\prodservers.txt 

$vmName
= Read-Host "Enter VM Name: " foreach ($esx in $esxservers) {   Connect-VIServer -Server $esx -cred $rootcred | Out-Null
 
$vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
 
if($vm){     Write-Host $vm "is located on: " $esx -ForegroundColor White
  }  
else{     Write-Host $vm "is NOT located on: " $esx -ForegroundColor DarkGreen   }   Disconnect-VIServer -Server $esx -Confirm:$false }


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

Munster99
Enthusiast
Enthusiast
Jump to solution

WOW ! Nearly there LucD

The only niggling issue is when I do run it, it reports back saying 'the VM is NOT located ..... '  but it doesnt report back the '$vm' variable ???

Any ideas

(It should be straight forward - however I am running the PS v3 CTP)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This should solve that

$rootcred = Get-Credential
$esxservers = Get-Content c:\scripts\prodservers.txt 

$vmName
= Read-Host "Enter VM Name: " foreach ($esx in $esxservers) {   Connect-VIServer -Server $esx -cred $rootcred | Out-Null
 
$vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
 
if($vm){     Write-Host "$vm.Name is located on: $esx.Name" -ForegroundColor White
  }  
else{     Write-Host "$vm.Name is NOT located on: $esx.Name" -ForegroundColor DarkGreen   }   Disconnect-VIServer -Server $esx -Confirm:$false }


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

0 Kudos
Lessi001
Enthusiast
Enthusiast
Jump to solution

Hi,

I had the same problem like you and i solved it with using DRS Host Group Manager. So vCenter Server and the Data Base Server is always running on a dedicated host.

I have published a How-to post on my blog http://www.vmworld.net/?p=1240, but it is written in German - so here a short explanation:

.) make a virtual machines DRS Group - member vCenter Server + DB Server

.) make a Host DRS Group - member ESXHost where you will have the vc to run

.) make a Rule Virtual Machines to Host where "VM Group" ->  "should run on hosts in group" -> Host Group

Important: use "should run on hosts in group" and NOT "must run ...." because so it is HA and DRS aware.

So will know every time where your vc is running...

Regards

Andi

There are 10 types of people in this world. Those who understand binary, and those who do not.
0 Kudos
Munster99
Enthusiast
Enthusiast
Jump to solution

Thanks LucD

Tried what you gave but still reported problems. So i tried to 'tweak' it and low and behold the following worked : Smiley Happy (weird !). Anyways thanks a million for all of you help !

$rootcred = Get-Credential
$esxservers = Get-Content c:\scripts\prodservers.txt

$vmName = Read-Host "Enter VM Name: "
Write-Host


foreach ($esx in $esxservers)
{
  Connect-VIServer -Server $esx -cred $rootcred | Out-Null 
  $vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
  if($vm){
    Write-Host "$vmName IS located on: $esx" -ForegroundColor White
  }
  else{
    Write-Host "$vmName IS NOT located on: $esx" -ForegroundColor DarkGray
  }
  Disconnect-VIServer -Server $esx -Confirm:$false
}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Of course, stupid me, if the VM is not found there will be nothing in $vm and hence no Name property.


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

0 Kudos
Munster99
Enthusiast
Enthusiast
Jump to solution

Andy

Thanks a million for that I can really seeing myself use that for a load of other purposes I have in mind. The issue is, our prod estate consists of approximately 15 hosts which means even if we did set up DRS groups it will only show which 'group of hosts' the vc is situated on (unless i am wrong?!?!?). Meaning we would still have to search the hosts individually to find the VM. Hence this script.

Regards

Munster

0 Kudos
Lessi001
Enthusiast
Enthusiast
Jump to solution

Hi,

no, because a Host group can also contain only one host - so you will definitly know the host 😉

Important is that you use the rule "should run... " as I  have written.

Because so it is HA and DRS aware. If you use "must run" HA will not restart the VMs in cause of a hostfailure of the dedicated host.

Regards

Andi

There are 10 types of people in this world. Those who understand binary, and those who do not.
0 Kudos