Checking which VM has mac address of “X” using get-view

This time i wanted to show how can we check which vm is using mac address X. For example some ntwk admin can ask you which vm has this mac address. So i will show two ways of doing this, there are many many more besides those.
First is 1 liner:

get-view -viewtype virtualmachine -property name,config.hardware.device |%{$vmname=$; $($_.config.hardware.device | ?{ $_ -is [Vmware.vim.virtualethernetCard]}) | select *,@{N="vm name";E={$vmname}} |?{$_.MacAddress -eq '00:50:56:66:77:88'}  }

’00:50:56:66:77:88′ – is the mac i am looking for. You can define your own output using this one liner, i wanted to see the whole device + the vm name that holds this device.
First we get all virtual machines then for each virtual machine we will be printing out its devices, but only those devices which are network cards, then selecting * (all properties) + $vmname where the macaddress equals our mac that we are looking for. I stored my vmname in $vmname after first pipe.
Ok, now the same thing but i guess in more readable way:

foreach($vmview in get-view -viewtype virtualmachine -property name,config.hardware.device)
  foreach ($netcard in ($vmview.config.hardware.device|?{$_ -is [Vmware.vim.virtualethernetCard]})) 
     if ($netcard.macaddress -eq "00:50:56:66:77:88") 
	    "Found mac in vm: $($"

So we do : get-view for all vms. Each of this vm view object will be stored in $vmview, and for each $vmview we will be displaying its network cards.
$netcard is variable the holds the virtual machine network device configuration.
$vmview.config.hardware.device -> this holds all devices
More about this
[Vmware.vim.virtualethernetCard] -> this is the type of network card , so we are only getting network cards.
So we check then if the macaddress of this network cards matches our mac, if yes, print out message

I saw many people doing this in different ways, for now i saw i think 5 different approaches to check this.
The virtual machine can be powered off/on, guest operating system can be installed or not, it does not make any difference using this approach as we are going though virtual machine config.

If you want to narrow down the area where your vms are you can modify this line
foreach($vmview in get-view -viewtype virtualmachine)
So when you want to check this in some cluster or datacenter , you can add -Searchroot to get-view, and give the moref
How do you get the moref of that particulard datacenter or cluster ? you can do it in different ways as well:
get-cluster “your cluster”|select id,name
get-view -viewtype clusterComputeResource | select name,moref
So if you would give in get-view -viewtype virtualmachine -Searchroot (get-cluster “cluster A”).id -> then this would give you vms only that reside in cluster A

Monitoring virtual center server services

If you want to monitor from your management box work of virtual center server, check if services are working, if connection is up, you could use this script.
You can still modify it in different ways. For example if you want to monitor more services, add them in $ServicesToCheck.
I am using an ‘if’ there, as i am not using default services for all vc servers. Email message is build by expanding $emailbody variable. I don’t use smtp authentication here, if you need it, you have to add part with obtaining credentials and passing them to send-mailmessage command.

#if $status will be false, then it will trigger send-mailmessage
#vctomcat           VMware VirtualCenter Management Web...
#vmware-ufad-vci    VMware vSphere Update Manager Service
#vpxd               VMware VirtualCenter Server
#vimQueryService    vCenter Inventory Service
#checking if log file is not too big, if it is , delete 
if(Test-Path $logfile){if ((Get-ChildItem $logfile).length -gt 100KB) { rm $logfile }}
$subject="Virtual center server is down. Investigation is needed!"
$emailbody="Hello,`n`nVirtual center server or its component is down, please investigate!!!`n`n"

Monitoring Script
foreach ($vc in $vcservers)
	$emailbody+="`n`nVirtual Center server: ${vc}`n"
		if ($testcon=Test-Connection $vc -ErrorAction SilentlyContinue){
			$emailbody+="`n Test-connection from mgmt box to $VC is OK.`n"
			if($vc -eq "server1"){$ServicesToCheck="vctomcat","vpxd","vimQueryService"}
			if($vc -eq "serverN"){$ServicesToCheck="vmware-ufad-vci","vctomcat","vpxd"}
				foreach($service in $ServicesToCheck){
					if($qservice=Get-Service $service -ComputerName $vc -ErrorAction SilentlyContinue){
						if($qservice.status -ne 'Running'){
							$emailbody+="`nService $($ is not running`n"
							$emailbody+="`nService $($ is running`n"
					}else {
						$emailbody+="`nCould not query service: ${service}`n"	
		}else {
			$emailbody+="`n Test-connection from mgmt box to $VC has failed, please check whether VC server is up and network is working.`n"
Send-MailMessage -Subject $subject -From $SMTPfrom -SmtpServer $SMTPserver -To $SMTPto -Body  $emailbody
"VC IS DOWN at $(get-date)" >> "c:\scripts\vc_monitoring\vcmonitoring.log"

Make sure that the user from which this script will run, has proper rights on target VC server. If he is missing rights then the get-service will not work.

how to check which vms have snapshots in a very dirty but quick way?

I have written more advanced scripts for printing, reporting stuff about snapshots than this one, but here it is just about to see which vm has snapshot. That is all i want by running this command. And i want to see the results FAST!, i mean NOW!, or like YESTERDAY! 😉

Get-View -ViewType VirtualMachine -Filter @{"snapshot" = ""} | select name

That’s it ;], nothing more. It will just output the vSphere .net view object for vms that have snapshots on it. Once again, it is not fancy, it does not send e-mail, it does not do coffee while running, it just shows you vms with snapshots on it. You can build your own “EXTRA” around this if you want. I just wanted to see very quickly if i have snaps or not. If you have any better idea / quicker how to view snapshots on vm, please post a comment!