Udostępnij za pośrednictwem


A Simple Script to Locate a VM

For some of us finding a single VM in amongst several thousand can be a tiresome task. Of course you could opt to use the VMM Search feature, however if you don't have access to the console and you need a quick and painless way of locating a machine, this simple script should do the trick:

Simply copy and paste the below into notepad, save as a .ps1 and run it!

Import-Module virtualmachinemanager -ErrorAction SilentlyContinue

 

$VMName = Read-Host "Enter the virtual machine name"

 

$VM = Get-SCVirtualMachine -Name $VMName

 

$Cluster = Get-SCVMHostCluster | where {$_.Nodes -contains $VM.HostName}

 

If ($Cluster -eq $null){

 

Write-Host "Virtual Machine" $VMName "not found" -ForegroundColor Red

}

 

Else{

 

Write-Host "The virtual machine" $VMName "is currently hosted on" $VM.HostName -ForegroundColor Cyan

 

Write-Host "Host" $VM.HostName "is a member of" $Cluster.Name -ForegroundColor Cyan

 

}

You will be prompted for the VM Name, press enter and shortly after you will be given the Host and Cluster name where the VM is currently located.

I have also created another version that allows you to query multiple VMM Management servers: [View:~/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-96-43/FindMyVM_5F00_MultipleVMMServers.txt:550:0]

Since creating this article I have developed the script further to allow it to be run remotely from any machine. You are required to enter your VMM Server FQDN in the script before it will work in your environment. When running the script you will be prompted for credentials, which will be used to connect to the VMM Server and the name of the VM you want to find: [View:~/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-96-43/FindMyVM_5F00_Remotely.txt:550:0]