Virtually everything is poshable
Archive for August, 2009
PowerCLI: Mass provision datastore’s
Aug 28th
At the moment I am adding around 50 datastore’s to a host, now whilst I love using the great Virtual Infrastructure Client wizard to do this after adding two of the 50 i remembered what someone said (can’t remember who)….. If you do something more than once, script it.
So guess what I did….
The following script was used to add the datastore’s from a csv file whilst I happily worked on other things.
The csv file consisted of the following:
LUNid,Hostid
101,1
221,2
331,3
451,4
521,5
661,6
etc etc, the naming convention for my datastores is SAN_HOSThostid_LUNlunid so this was easily scripted as so:
$CSVFile = "C:\Temp\Datastores.csv"
$MYESXHost = "ESX01.virtu-al.local"
$VMHBA = "vmhba2:0:"
Connect-VIServer MYVISERVER
$CSV = Import-CSV $CSVFile
$ESXHost = Get-VMHost $MYESXHost
$ESXHost |Get-VMHostStorage -RescanAllHBA
Foreach ($Item in $CSV){
$HostID = $Item.HostId
$LunID = $Item.LunID
Write "Creating SAN_HOST$($HostID)_LUN$($LunID)…"
$ESXHost |New-Datastore -Vmfs -Name "SAN_HOST$($HostID)_LUN$($LunID)" -Path "$($VMHBA)$($HostID)"
}
The New-Datastore cmdlet also has a -BlockSizeMB parameter which can be used to specify the block size of VMFS in megabytes.
All in all it took me around 10 minutes to add 50 datastore’s which I think would have taken significantly longer through the GUI. Yet another reason to start learning PowerCLI !
PowerCLI: One-liners last 10 VMs created and removed
Aug 27th
I was asked if it was easy enough to get a list of the last 10 VMs created in a virtual Infrastructure, the answer is yes, its a one-liner !
To list the last 10 VMs created, cloned or imported use the following:
Get-VIEvent -maxsamples 10000 |where {$_.Gettype().Name-eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmBeingClonedEvent" -or $_.Gettype().Name-eq "VmBeingDeployedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage -First 10
Or if you would like to get the last 5 VMs removed from your VI use the following:
Get-VIEvent -maxsamples 10000 | where {$_.Gettype().Name -eq "VmRemovedEvent"} | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage -First 19
As requested, here is how you can get a list of the VM’s created over the last 14 days:
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14) | where {$_.Gettype().Name-eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmBeingClonedEvent" -or $_.Gettype().Name-eq "VmBeingDeployedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage
And a list of the VMs removed over the last 14 days:
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14) |where {$_.Gettype().Name-eq "VmRemovedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,FullformattedMessage
PowerCLI: Number of vCPUs in a cluster
Aug 20th
Inspired by Jason Boche’s article: http://www.boche.net/blog/index.php/2009/08/18/hidden-virtual-cpu-limit-restriction-in-esx-3-5/ and also William Lam’s Perl Script (http://communities.vmware.com/docs/DOC-10556). I decided to re-create this in PowerCLI.
If you haven’t seen William’s scripts, I strongly suggest you check out the Perl Sample code where he stores them here: http://communities.vmware.com/community/developer/codecentral/vsphere_perl, he has been coding mad recently and turning out some really good stuff.
The PowerCLI script gives the following output:
Cluster: Production
Host: vmgesx01.vmguru.com
Misc.RunningVCpuLimit: 128
Number of vCPU on host: 4
Host: vmgesx02.vmguru.com
Misc.RunningVCpuLimit: 128
Number of vCPU on host: 3
Host: vmgesx03.vmguru.com
Misc.RunningVCpuLimit: 128
Number of vCPU on host: 2
Host: vmgesx04.vmguru.com
Misc.RunningVCpuLimit: 128
Number of vCPU on host: 5
———-
Number of vCPU in Production: 14
———-
Daily Report – iPhone Version
Aug 20th
If you are using my Daily Report script and want a nice output I have just received a cool tip from NiTRo over at http://www.hypervizor.fr
He explained how to alter the script to get it looking nice on the iPhone, check out the before and after:
| Before | After |
PowerCLI: Daily Report V2
Aug 18th
Firstly I would like to thank everyone who took the time to comment on my previous Daily Report script, I really appreciate the feedback and have never had so many comments !
Now onto V2…
This one will take a while longer to run than the last script but as its a scheduled task we are not really worried about that, we are hardly going to sit there and watch it running !
I think you will agree the results are worth the wait:
What’s new
- Bug Fixes
- Active VMs count
- Inactive VMs count
- DRS Migrations count and list
- Correct NTP Server check for each host
- VMs stored on local datastores
- NTP Service check for each host
- vmkernel warning messages for each host
- VM CPU ready over x%
More >
PowerCLI: Reading host log files
Aug 17th
Recently I needed to check the vmkernel log file on a host for any errors relating to a disk issue I was having, I did this in the normal way of using putty to get to my server and then a cat /var/log/vmkernel. That is one way of doing it but did you know you could also do this through PowerCLI and add some automation into it ? Well you can….
There is a cmdlet called Get-Logtype, if we connect to a VirtualCenter and then run this you will see the following:
Get-LogType
PowerCLI: Processor Types
Aug 16th
Ever wanted to check the hosts in your vCenter to make sure they are all the same type, you can use the following one liner to do this:
Get-VMHost | Sort Name | Get-View | Select Name, @{N=“CPU“;E={$_.Hardware.CpuPkg[0].Description}} | Export-Csv c:\cpuinfo.csv
Sample Output:
| Name | CPU |
| tesesx01.mydomain.com | Intel(R) Xeon(R) CPU 5160 @ 3.00GHz |
| tesesx02.mydomain.com | Intel(R) Xeon(R) CPU 5160 @ 3.00GHz |
| tesesx03.mydomain.com | Intel(R) Xeon(R) CPU E5450 @ 3.00GHz |
| tesesx04.mydomain.com | Intel(R) Xeon(R) CPU E5450 @ 3.00GHz |
| tesesx05.mydomain.com | Intel(R) Xeon(R) CPU E5450 @ 3.00GHz |
| tesesx06.mydomain.com | Intel(R) Xeon(R) CPU E5450 @ 3.00GHz |
| tesesx07.mydomain.com | Intel(R) Xeon(R) CPU E5450 @ 3.00GHz |
Or if you want to add the cluster name in there as well:
Get-VMHost | Sort Name | Get-View | Select Name, @{N=“Cluster“;E={Get-Cluster -VMHost (Get-VMHost $_.Name)}},@{N=“CPU“;E={$_.Hardware.CpuPkg[0].Description}} | Export-Csv c:\cpuinfo.csv
Sample Output:
| Name | Cluster | CPU |
| tesesx01.mydomain.com | Test | Intel(R) Xeon(R) CPU 5160 @ 3.00GHz |
| tesesx02.mydomain.com | Test | Intel(R) Xeon(R) CPU 5160 @ 3.00GHz |
| tesesx03.mydomain.com | Production | Intel(R) Xeon(R) CPU E5450 @ 3.00GHz |
| tesesx04.mydomain.com | Production | Intel(R) Xeon(R) CPU E5450 @ 3.00GHz |
| tesesx05.mydomain.com | Production | Intel(R) Xeon(R) CPU E5450 @ 3.00GHz |
| tesesx06.mydomain.com | Production | Intel(R) Xeon(R) CPU E5450 @ 3.00GHz |
| tesesx07.mydomain.com | Production | Intel(R) Xeon(R) CPU E5450 @ 3.00GHz |
vSphere 4.0 Quickstart Guide
Aug 15th
For a while now I have been writing a book with a number of other people from the VMware community:
This book has been designed to be an affordable pocket size book, ideal for quick tips and how-to’s and of course it is full with PowerCLI examples for each of the tasks showing how PowerCLI is useful in every area of the virtual infrastructure.
The book is not available at the moment, we are still adding some final touches but please keep an eye out on my blog or if you are at VMWorld later this month you should be able to grab a copy.
For more information about this book check out Duncan’s blog post here
PowerCLI: Do you have the time ?
Aug 14th
Sorry for the lack of updates recently, I have been on vacation. I thought I would ease myself back in with a quick one-liner.
We had an issue on one of our esx hosts today and when I went to check the logs I noticed the time was out, on further checking the server the NTP service had stopped.
I wrote a quick one liner to double check all our Hosts were using the same NTP Server and that the service was started:
Get-VMHost |Sort Name|Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}}, @{N=“ServiceRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}}
This will give you a nice table letting you know the state of your hosts time, if you do happen to have a host with an incorrect NTP server then you can set it like this:
Add-VMHostNtpServer -VMHost MYHost -NtpServer ‘ntp.mydomain.com‘
Or if one of the services has stopped you can start it again with the following:
Get-VmHostService -VMHost MyHost | Where-Object {$_.key -eq “ntpd“} | Start-VMHostService
Example output from my one-liner:
| Name | NTPServer | ServiceRunning |
| testesx01.mydomain.com | ntp.mydomain.com | TRUE |
| testesx02.mydomain.com | ntp.mydomain.com | FALSE |







(3)
(13)
(0)