Search This Blog

2013-04-15

PowerCli Commands

Connecting and Disconnecting

Disconnect all server connections
Disconnect-VIServer -Server * -Force

Connect to Server and save credentials for future logins
Connect-VIServer "RPC-G4-17VM" -User root -Password "i manage" -SaveCredentials

Removing VMs

Remove and Delete all VMs from Inventory and Disk
Get-VM | Remove-VM -DeletePermanently -Confirm:$false

Reconfiguring Virtual Hardware

Adding vSwitches

Get-VMHost "virtual_host" | New-VirtualSwitch -name "vswitch_name" -nic "vmnic#"

Note that vmnic naming convention is heavily dependant on the order in which you insert the NICs. udev will take care of the naming and it is based on PCI bus channel naming conventions.

If you want to ensure you get the correct ordering:

Only insert additional NICs after installation and first boot.
OR
Reconfigure manually using: http://www.virten.net/2012/09/esxi-change-vmnic-vmhba-numbering/

Adding PortGroups

Get-VMHost "" | Get-VirtualSwitch -name "" | New-VirtualPortGroup -Name ""

Setting the BIOS to Boot From Network (PXE Boot)

Reconfiguring the order in which devices boot without actually having to go into the BIOs
Configure All VMs to boot off of network
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec 
$spec.extraConfig += New-Object VMware.Vim.OptionValue 
$spec.extraConfig[0].key = "bios.bootOrder" 
$spec.extraConfig[0].value = "ethernet0" 
get-vm|%{(Get-View $_.ID).ReconfigVM_Task($spec)}

Configure a Specific VM to boot off of network
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec 
$spec.extraConfig += New-Object VMware.Vim.OptionValue 
$spec.extraConfig[0].key = "bios.bootOrder" 
$spec.extraConfig[0].value = "ethernet0" 
(Get-View (Get-VM -Name TPC-G4-17-$i).ID).ReconfigVM_Task($spec)

Resource Allocation and Reservations

Changing Memory Allocation
Get-VM | Set-VM -MemoryMB 2048

Setting Memory Reservations
Get-VM | Get-VMResourceConfiguration | Set-VMResourceConfiguration -MemReservationMB  

Matching and Changing Limits
Match all machines that have a memory limit and then remove the limit by setting it to null.
Get-VM | Get-VMResourceConfiguration | Where-Object {$_.MemLimitMB -ne ‘-1‘} | Set-VMResourceConfiguration -MemLimitMB $null 

Alternatively if no value needs to be checked (You just want to set them all Null)
Get-VM | Get-VMResourceConfiguration | Set-VMResourceConfiguration -MemLimitMB $null -CpuLimitMhz $null

Management

VM Power

Stop All VMs on the connected host(s)
get-vm | stop-vm

Start All VMs on the connected host(s)
get-vm | start-vm 

Gathering VM Stats and Information

Gathering MAC Addresses

Get all mac addresses for all network adapters on the host(s) you are connected to
Get-VM | `Select-Object -Property Name,@{N="MacAdresses";E={$_.NetworkAdapters | ForEach-Object {$_.MacAddress} }},VMHost 

Find MAC address and network adapter tied to a specific MAC
Get-vm | Select Name, @{N=“Network“;E={$_ | Get-networkAdapter | ? {$_.macaddress -eq “00:50:56:A1:50:43“} }} |Where {$_.Network-ne “”}

Passing File Input

Checking RAM Reservation Based on File Contents
Get-Content -Path VMs.txt | ForEach-Object {
  Get-VM -Name  $_ | `
  Get-VMResourceConfiguration | `
  Set-VMResourceConfiguration -MemReservationMB 1024
}

For Loops

Create 30 VMs each with at least 3 digits, if the digits are not 3 characters long pad with leading zeroes as follows TPC-RK-RU-00#
1..30 | foreach {
  $i="{0:000}" -f $_
  Get-VMHost "RPC-G4-17VM" | Get-VirtualSwitch -name "vSwitch2" | New-VirtualPortGroup -Name TPC-G4-17-${i}_port1
  New-VM -VMHost RPC-G4-17VM -Version v7 -Name TPC-G4-17-$i -Datastore datastore1 -DiskStorageFormat EagerZeroedThick -MemoryGB 1 -DiskMB 8192 -NumCpu 1 -GuestId freebsdGuest -NetworkName Control_port1, TPC-G4-17-${i}_port1, TPC-G4-17-${i}_port2, TPC-G4-17-${i}_port3, TPC-G4-17-${i}_port4
}

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete