Vmware Power Cli

Quickly Add Mulltiple VM nics 

we can automate the addition of vmnics to any of our virtual machines. The commandlet we use is the new-networkadapter commandlet. We can specify several parameters with this commandlet including the VM we want to add to, the networkname or port group to attach the network adapter to, the type (vmxnet3, etc) and we can specify we want the network adapter to startconnected.

#new-networkadapter -vm <vmname> -NetworkName "<Port group name>" -Type "VMXNET3" -startconnected

If we want to add a specific number of network adapters to a certain VM, we can do that with a for each loop with a count statement.  The below will add (4) new network adapters to a specific VM identified with the -vm parameter.

#Multiple vmnics on a specified VM
foreach ($i in 1..4){
new-networkadapter -vm <vmname> -NetworkName "<Port group name>" -Type "VMXNET3" -startconnected
}
}

Multiple -4 vmnics, list of  VMs : We can nest our foreach loop inside another foreach loop and usea variable to read in the contents of a VM list.

********************************************************************************

#Multiple vmnics on multiple VMs $vms=get-content c:vms.txt foreach ($vm in $vms){ foreach ($i in 1..4){ new-networkadapter -vm $vm -NetworkName "Port group name" -Type "VMXNET3" -startconnected } }

**********************************************************************************************************

Get-VIEvent cmdlet - ESXI Host login failures  and can be queried as such.

foreach ($esxhost in Get-VMHost)
{
$esxhost.Name
Get-VIEvent -Entity $esxhost -Types Error -MaxSamples 10000 -Start (Get-Date).AddDays(–7) | where-object {$_.FullFormattedMessage -like "*cannot login*"} | select CreatedTime, FullFormattedMessage
}

***********************************************************************************
Quick query to list VMs restarted in an HA event

One of our ESXi servers had purple screened and we wanted to quickly see what VMs on the host machine had been affected. 

Almost immediately, HA (High Availability) had kicked in and restarted all the VMs on different hosts. 


This single PowerShell line grabbed warning events from the past 24 hours and matched the word “restarted” in them and displayed in a nice list format.  With this list in hand, we could quickly start spot checking the VMs

#Get-VIEvent -MaxSamples 100000 -Start (Get-Date).AddDays(-1) -Type Warning | Where {$_.FullFormattedMessage -match “restarted”} |select CreatedTime,FullFormattedMessage | sort CreatedTime –Descending

********************************************************************************

List VMs Running On Hosts

Connect the ESXi host via SSH and issue this command to list all VMs:

vim-cmd vmsvc/getallvms

Get VM State

To get the current state of the VM, issue the command:

vim-cmd vmsvc/power.getstate VMID

Shutdown the VM using this command:

vim-cmd vmsvc/power.shutdown VMID

If the VM fails to shutdown, you can run:

vim-cmd vmsvc/power.off VMID

 **********************************************************************************



Comments

Popular Posts