Devoted to Virtualization Technologies

Site to share knowledge about VMware and related Technologies

PowerShell

I am not a powershell expert but,I have to use them occasionally to get work done. The purpose of this page, is to keep those handy powershell scripts in a organized manner. So, when ever we need to use them, they are just a click away Wink

“Most of these one-liners are collected over time. They are downloaded from the internet, or copied from VMTN forum. Before, you run any script, you will have to connect to vCenter Server OR to the ESX/ESXi hosts.”

This will be a living document and I will keep on adding more scripts to the list.

This is the first script that you need to run to connect to vCenter Server OR ESX/ESXi server

 connect vi-server "Name of the server/IP Address" 

Various one liner scripts

1 List all VMs
2 Get one VM
3 Get VMs that match
4 List VMs in a Datacenter
5 List VMs in a cluster
6 Get 2 CPU or larger VMs
7 Get 2 GB RAM or larger VMs
8 Getting information about what you can do with powershell
9 Save a report of VM CPU/MEM configuration information
10 List VMs with connected CD-ROMs
11 Disconnect all connected CD-ROMs
12 Migrate a template to another host
13 Report on Storage
14 Storage VMotion ALL VMs from one LUN to another (All at the same time) COULD BE BAD
15 Storage VMotion ALL VMs from one LUN to another (One at a time)
16 Set all limited CPU reservations to unlimited
17 Set all limited memory reservations to unlimited
18 Configure a new network on a host
19 Configure a new network on a cluster
20 Generate a diagnostic log bundle
21 List the entries in vmkwarning
22 Find something in a log
23 What logs are available?
24 Find VMs with snapshots (Pretty slow – could be optimized)
25 Find VMs with that are poweredon but not from the entire environment, exclude few clusters
26 Get the number of ESX host’s
27 Answer Virtual Machine question in a cluster
28 Set the vlanid for a portgorup with in a cluster
29 How to set the ntp settings across the cluster
30 How to start the ntp service across the cluster
31 How to create a new portgroup across the cluster


1.List all VMs

 Get-VM 

2 Get one VM

 Get-VM ubuntu01 

3 Get VMs that match

 Get-VM ubuntu* 

4 List VMs in a Datacenter

 Get-Datacenter dc001 | Get-VM 

5 List VMs in a cluster

 Get-Cluster cluster001 | Get-VM 

6 Get 2 CPU or larger VMs

 Get-VM | Where-Object {$_.NumCPU -ge 2} 

7 Get 2 GB RAM or larger VMs

 Get-VM | Where-Object {$_.MemoryMB -ge 2048} 

8 Getting information about what you can do with powershell

 Get-VICommand
Get-Command
Get-Help Get-VM
Get-Member
Get-VM | Get-Member 

9 Save a report of VM CPU/MEM configuration information

 Get-VM | Select-Object Name, NumCPU, MemoryMB | Export-Csv "VM_CPU_Memory_20100720.csv" -NoTypeInformation 

10 List VMs with connected CD-ROMs

 Get-VM | Where-Object {$_ | Get-CDDrive | Where-Object {$_.ConnectionState.Connected}} 

11 Disconnect all connected CD-ROMs

 Get-VM | Get-CDDrive | Where-Object {$_.ConnectionState.Connected} | Set-CDDrive -Connected $false -Confirm:$false 

12 Migrate a template to another host

 Get-Template ubuntu | Set-Template -ToVM | Move-VM -Destination (Get-VMHost esx001.lab.local) | Set-VM -ToTemplate -Confirm:$false 

13 Report on Storage

 Get-Datastore | Select-Object Name, FreeSpaceMB, CapacityMB | Export-Csv "Datastores_20100720.csv" -NoTypeInformation 

14 Storage VMotion ALL VMs from one LUN to another (All at the same time) COULD BE BAD

 Get-Datastore "cla_nonrep_01" | Get-VM | Move-VM -Datastore (Get-Datastore "cla_nonrep_00") 

15 Storage VMotion ALL VMs from one LUN to another (One at a time)

 Get-Datastore "cla_nonrep_01" | Get-VM | ForEach-Object {Move-VM -VM $_ -Datastore (Get-Datastore "cla_nonrep_00")} 

16 Set all limited CPU reservations to unlimted

 Get-VM | Get-VMResourceConfiguration | Where-Object { $_.CpuLimitMhz -ne -1} | Set-VMResourceConfiguration -CpuLimitMhz $null 

17 Set all limited memory reservations to unlimited

 Get-VM | Get-VMResourceConfiguration | Where-Object { $_.MemLimitMB -ne -1} | Set-VMResourceConfiguration -MemLimitMB $null 

18 Configure a new network on a host

 Get-VirtualSwitch -VMHost esx003.lab.local -Name vSwitch0 | New-VirtualPortGroup -Name "VGT Test" -VLanId 4095 

19 Configure a new network on a cluster

 Get-VirtualSwitch -VMHost (Get-Cluster cluster001 | Get-VMHost) -Name vSwitch0 | New-VirtualPortGroup -Name "VGT Test" -VLanId 4095 

20 Generate a diagnostic log bundle

 Get-Log -VMHost esx001.lab.local -Bundle -DestinationPath c:\hostlogs 

21 List the entries in vmkwarning

 (Get-Log -VMHost esx001.lab.local -Key vmkwarning).Entries 

22 Find something in a log

 (Get-Log -VMHost esx001.lab.local -Key vmkernel).Entries | Select-String "Error" 

23 What logs are available?]

 Get-LogType -VMHost esx001.lab.local 

24 Find VMs with snapshots (Pretty slow – could be optimized)

 Get-VM | Where-Object {$_ | Get-Snapshot} 

25 Find VMs with that are poweredon but not from the entire environment, exclude few clusters

 Get-Cluster | where {"lab1","lab2","lab3"-notcontains$_.Name} | get-vm | where-object {$_.Powerstate -eq"Poweredon"} | measure-object 

26 Get the number of ESX host’s in the environment

  Get-VMhost | measure-object 

27 Answer virtual machine question in a cluster

 get-vmquestion| set-vmquestion -DefaultOption 

28 How to set the vlanid for a portgroup within a cluster

 get-cluster cluster-name | get-vmhost | get-VirtualPortGroup -name "VM network" | Set-VirtualPortGroup -name "VM Network" -vlanid 16 

29 How to set ntp server across the cluster

 get-cluster cluster-name | get-vmhost | Add-VMHostNtpServer -Ntpserver ntp1.server.com,ntp2.server.com 

30 How to start ntp service across the cluster

 get-cluster cluster-name | get-vmhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService 

31 How to create a new portgroup across the cluster

 get-cluster cluster-name | get-vmhost | get-virtualswitch -Name "vSwitch0" | New-virtualPortgroup -Name "x.y.z" -vlanid 123 

Leave a Reply

  • Modified logo BlogWithIntegrity.com
  • Recent Post

  • Tag Coud

  • Archives