Saturday, April 14, 2012

PowerCLI: Start and Stop the VMs resides on a vCenter Folder

Starting all the VMs in the folder 'production1'

$vms = Get-VM -Location production1

ForEach($vm in $vms)                 # Start each VM in the folder 'production1'
{
start-vm -RunAsync -VM $vm -Confirm:$false
}

Stopping all the VMs in the folder 'production1'

$vms = Get-VM -Location production1
ForEach($vm in $vms)
# Try a graceful shutdown of VMGuest if vmware tools are installed, Otherwise just stop the VM
{
$vm_view = $vm | get-view
$vmtoolsstatus = $vm_view.summary.guest.toolsRunningStatus
Write-Host “VM $vm says tools status is $Vmtoolsstatus”
if ($vmtoolsstatus -eq “guestToolsRunning”)
{
Shutdown-VMGuest -VM $vm -Confirm:$false       # Shutting down VMGuest gracefully
}
else
{
Stop-VM -RunAsync -VM $vm -Confirm:$false      # Stopping the VM
}
}

Friday, April 13, 2012

PowerCLI script for listing VM partition information

This script will display the VM partition information of all the Virtual Machines (both Linux and Windows) in vCenter.

PS> ForEach ($VM in Get-VM ){($VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(MB)";E={[math]::Round($_.Capacity/ 1MB)}}, @{N="Free Space(MB)";E={[math]::Round($_.FreeSpace / 1MB)}}, @{N="Free Space %";E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}})}

<output not shown>


-------------------------------------------------------------------------------------------------------------------------------------------------------

This modified version of above script will display the VM partition information of the Virtual Machines that are hosted on ESXi host : "iss-esxi-vm01.xxxx.com"

PS> ForEach ($VM in get-vmhost iss-esxi-vm01.xxxx.com | get-vm){($VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(MB)";E={[math]::Round($_.Capacity/ 1MB)}}, @{N="Free Space(MB)";E={[math]::Round($_.FreeSpace / 1MB)}}, @{N="Free Space %";E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}})}

Name           : itc-esxi-dnx01.xxxx.com
DiskPath       : C:\
Capacity(MB)   : 51191
Free Space(MB) : 36503
Free Space %   : 71

Name           : iss-esxi-mail1.xxxx.com
DiskPath       : /
Capacity(MB)   : 1008
Free Space(MB) : 813
Free Space %   : 81

Name           : iss-esxi-mail1.xxxx.com
DiskPath       : /boot
Capacity(MB)   : 99
Free Space(MB) : 86
Free Space %   : 87

Name           : iss-esxi-mail1.xxxx.com
DiskPath       : /tmp
Capacity(MB)   : 1008
Free Space(MB) : 975
Free Space %   : 97

<output truncated>

Thursday, April 12, 2012

PowerCLI scripts for automating VMware vSphere - part 1

PS> get-vm iss-esx-scc1

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
iss-esx-scc1.itci... PoweredOn  2        8192

PS> get-vm iss-esx-scc1 | format-list

Name            : iss-esx-scc1.xxxx.com
PowerState      : PoweredOn
NumCpu          : 2
MemoryMB        : 8192
HardDisks       : {Hard disk 1, Hard disk 2}
CDDrives        : {CD/DVD drive 1}
FloppyDrives    : {Floppy drive 1}
NetworkAdapters : {Network adapter 1, Network adapter 2}
UsbDevices      : {}
Host            : iss-esx-vm01.xxxx.com
Id              : VirtualMachine-vm-178
Notes           :

PS> get-vm iss-esx-scc1 | format-list *

PowerState           : PoweredOn
Version                 : v7
Description            :
Notes                   :
Guest                   : iss-esx-scc1.xxxx.com:
NumCpu                  : 2
MemoryMB                : 8192
HardDisks               : {Hard disk 1, Hard disk 2}
NetworkAdapters         : {Network adapter 1, Network adapter 2}
UsbDevices              : {}
CDDrives                : {CD/DVD drive 1}
FloppyDrives            : {Floppy drive 1}
Host                    : iss-esx-vm01.xxxx.com
HostId                  : HostSystem-host-115
VMHostId                : HostSystem-host-115
VMHost                  : iss-esx-vm01.xxxx.com
VApp                    :
FolderId                : Folder-group-v22
Folder                  : vm
ResourcePoolId          : ResourcePool-resgroup-114
ResourcePool            : Resources
PersistentId            : 5256a506-9e03-ac9c-c997-b2b8936b1b0b
UsedSpaceGB             : 16.80688
ProvisionedSpaceGB      : 127.0002
DatastoreIdList         : {Datastore-datastore-148, Datastore-datastore-149}
HARestartPriority       :
HAIsolationResponse     :
DrsAutomationLevel      :
VMSwapfilePolicy        : Inherit
VMResourceConfiguration : CpuShares:Normal/2000 MemShares:Normal/81920
CustomFields            : {}
ExtensionData           : VMware.Vim.VirtualMachine
Id                      : VirtualMachine-vm-178
Name                    : iss-esx-scc1.xxxx.com
Uid                     : /VIServer=ardevaraju@iss-esx-vc02.xxxx.com:443/VirtualMachine=VirtualMachine-vm-178/

PS> get-vmhost | get-vm | where-object { $_ -like "*nim*" }

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
iss-esx-nimmonfp0... PoweredOn  2        4032
iss-esx-nim01.itc... PoweredOn  4        8192
iss-esx-nimsql1.i... PoweredOn  4        8192
iss-esx-nimthub01... PoweredOn  2        4096
iss-esx-nimfhub01... PoweredOn  2        4096

PS> get-vmhost | get-vm | where-object { $_.name -eq 'iss-esx-nim01' } | Get-NetworkAdapter

Name                 Type       NetworkName  MacAddress         WakeOnLan
                                                                  Enabled
----                 ----       -----------  ----------         ---------
Network adapter 1    e1000      ISS Network  00:50:56:b7:65:e3       True
Network adapter 2    e1000      VM Storag... 00:50:56:b7:3c:38       True

PS> get-vmhost | get-vm | where-object { $_.name -eq 'iss-esx-nim01' } | Get-Datastore

Name                               FreeSpaceMB      CapacityMB
----                               -----------      ----------
iss-esx-nim01:OSDISKn                    21704           81664
iss-esx-nim01:D_DISK                     19526           50944

PS > get-vmhost iss-esx-vm01 | get-vm | get-vmguest

State          IPAddress            OSFullName
-----          ---------            ----------
Running        {10.0.0.40, 10.15... Microsoft Windows Server 2008 R2 (64-bit)
Running        {172.17.1.200}       Microsoft Windows Server 2003 (32-bit)
NotRunning     {}
Running        {10.0.0.186, 10.1... Microsoft Windows Server 2008 R2 (64-bit)
NotRunning     {}
NotRunning     {}
NotRunning     {}
NotRunning     {}
NotRunning     {}
Running        {10.150.1.28, fe8... Red Hat Enterprise Linux 4 (32-bit)


Running        {10.150.4.157, fe... Red Hat Enterprise Linux 4 (32-bit)
Running        {10.150.1.35, fe8... Red Hat Enterprise Linux 5 (64-bit)
Running        {10.150.1.14}        Microsoft Windows Server 2008 (32-bit)
NotRunning     {}
Running        {10.0.0.180, 10.1... Microsoft Windows Server 2008 R2 (64-bit)
Running        {10.0.0.183, 10.1... Microsoft Windows Server 2008 R2 (64-bit)
Running        {10.150.1.16, 10.... Microsoft Windows Server 2008 R2 (64-bit)
Running        {169.254.9.208, 1... Microsoft Windows Server 2003 (32-bit)

PS> get-vmhost iss-esx-vm01 | get-vm | where {$_.Memorymb -lt 2048}

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
rfin-esx-mail1.it... PoweredOn  1        512
iss-esx-w2k3_32.h... PoweredOff 1        1024
iss-esx-w2k8_64.h... PoweredOff 2        1024
iss-esx-rhel5664-... PoweredOff 2        1024
hjbb-esx-ftp1.itc... PoweredOn  1        500
iss-esx-mman01.it... PoweredOn  2        512
iss-esx-sfm01.itc... PoweredOn  2        1024

PS> get-vmhost iss-esx-vm01 | get-vm | where {$_.Memorymb -gt 2048}

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
iss-esx-nimmonfp0... PoweredOn  2        4032
itc-esx-dnx01.itc... PoweredOn  2        4096
iss-esx-scc1.itci... PoweredOn  2        8192
iss-esx-mail1.itc... PoweredOn  2        4096
iss-esx-ad01.itci... PoweredOn  2        4096
iss-esx-mgr02.itc... PoweredOn  4        8192
iss-esx-nim01.itc... PoweredOn  4        8192
iss-esx-sdp01.itc... PoweredOn  4        4096
iss-esx-nimsql1.i... PoweredOn  4        8192

PS>

PowerCLI Banner: http://dl.dropbox.com/u/50666315/blog/Final-PowerCLI-4.1.1.pdf

Popular Posts

About Me

My photo
The intent of this blog is to share my work experience and spread some smart solutions on Linux to System Administrators. I'm hoping the solutions shared in this Blog would be helpful and come as a handy for Viewers. Brief about me: I have 18+ years work experience in System and Cloud Administration domain, primarily works on VMware Cloud Products (vSphere, vCloud Director, vRealize Automation, NSX Adv. Load Balancer, vROps).