PowerCLI: Getting vmhost uptime
I love a powershell challenge, and last week a colleague of mine asked me for assistance in getting the uptime of vmware hosts. My initial response did the trick: Get-View -ViewType hostsystem -Property name,runtime.boottime | Select-Object Name, @{N=”UptimeDays”; E={((((get-date) – ($_.runtime).BootTime).TotalDays).Tostring()).Substring(0,5)}} However, I wasn’t completely satisfied by the the output or the ease of use. So today I went back and rewrote the code and made a function of it. Instead of using the ToString and Substring methods I went for the built-in class Math, which has a method called Round. You can learn more about the Math class here: http://www.madwithpowershell.com/2013/10/math-in-powershell.html Anyways, here’s the function I came up with: function Get-VMHostUptime { [CmdletBinding()] Param ( [Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)][Alias(‘Name’)][string]$VMHosts, [string]$Cluster ) Process{ If ($VMHosts) { foreach ($VMHost in $VMHosts) {Get-View -ViewType hostsystem -Property name,runtime.boottime -Filter @{“name” = “$VMHost”} | Select-Object Name, @{N=”UptimeDays”; E={[math]::round((((Get-Date) – ($_.Runtime.BootTime)).TotalDays),1)}}, @{N=”UptimeHours”; E={[math]::round((((Get-Date) – ($_.Runtime.BootTime)).TotalHours),1)}}, @{N=”UptimeMinutes”; E={[math]::round((((Get-Date) – ($_.Runtime.BootTime)).TotalMinutes),1)}}} }…
PowerCLI: Getting the status of vmware tools on all VMs
I’m sure I don’t need to explain to you guys why VMware tools is a good idea to have installed on your VMs, and probably not why it’s a good idea to keep VMware tools updated. However, I haven’t found a good way to get a neat list of which VMs need to have their VMware tools upgraded. While working on my vCenter health check script I found that I had to make my own little script to get that list. And, in addition, I wanted the list to include the VM version. I ended up with creating a function to provide me with that list: function Get-VMToolsStatus { [CmdletBinding()] Param ( [ValidateSet(‘NeedUpgrade’,’NotInstalled’,’Unsupported’)][string]$Filter ) $VMs = Get-View -ViewType VirtualMachine -Property name,guest,config.version,runtime.PowerState $report = @() $progress = 1 foreach ($VM in $VMs) { Write-Progress -Activity “Checking vmware tools status” -Status “Working on $($VM.Name)” -PercentComplete ($progress/$VMs.count*100) -ErrorAction SilentlyContinue $object = New-Object PSObject…
PowerCLI: Getting datastore alarms
Next in the series on getting alarms is getting datastore alarms. Again, the code is pretty similar: $Datastores = Get-View -ViewType Datastore -Property Name,OverallStatus,TriggeredAlarmstate $FaultyDatastores = $Datastores | Where-Object {$_.TriggeredAlarmState -ne “{}”} $progress = 1 $report = @() if ($FaultyDatastores -ne $null) { foreach ($FaultyDatastore in $FaultyDatastores) { foreach ($TriggeredAlarm in $FaultyDatastore.TriggeredAlarmstate) { Write-Progress -Activity “Gathering alarms” -Status “Working on $($FaultyDatastore.Name)” -PercentComplete ($progress/$FaultyDatastores.count*100) -Id 1 -ErrorAction SilentlyContinue $entity = $TriggeredAlarm.Entity.ToString() $alarmID = $TriggeredAlarm.Alarm.ToString() $object = New-Object PSObject Add-Member -InputObject $object NoteProperty Datastore $FaultyDatastore.Name Add-Member -InputObject $object NoteProperty TriggeredAlarms (“$(Get-AlarmDefinition -Id $alarmID)”) $report += $object } $progress++ } } Write-Progress -Activity “Gathering alarms” -Status “All done” -Completed -Id 1 -ErrorAction SilentlyContinue $report | Where-Object {$_.TriggeredAlarms -ne “”} And the output is pretty similar: The function code is this: function Get-DatastoreAlarms { $Datastores = Get-View -ViewType Datastore -Property Name,OverallStatus,TriggeredAlarmstate $FaultyDatastores = $Datastores | Where-Object {$_.TriggeredAlarmState -ne “{}”} $progress = 1 $report =…
PowerCLI: Getting cluster alarms
The next step in my short series on getting vCenter alarms using PowerCLI is to get cluster alarms. Here’s the code: $Clusters = Get-View -ViewType ComputeResource -Property Name,OverallStatus,TriggeredAlarmstate $FaultyClusters = $Clusters | Where-Object {$_.TriggeredAlarmState -ne “{}”} $report = @() $progress = 1 if ($FaultyClusters -ne $NULL) { foreach ($FaultyCluster in $FaultyClusters) { foreach ($TriggeredAlarm in $FaultyCluster.TriggeredAlarmstate) { Write-Progress -Activity “Gathering alarms” -Status “Working on $($FaultyCluster.Name)” -PercentComplete ($progress/$FaultyClusters.count*100) -Id 1 -ErrorAction SilentlyContinue $entity = $TriggeredAlarm.Entity.ToString() $alarmID = $TriggeredAlarm.Alarm.ToString() if ($entity -like “ClusterComputeResource-*”) { $entityName = $FaultyCluster.Name $type = “Cluster” } elseif ($entity -like “HostSystem-host*”) { $entityName = (Get-View -ViewType HostSystem -Property Name | Where-Object {$_.MoRef -eq $entity}).Name $type = “VMHost” } elseif ($entity -like “VirtualMachine-vm*”) { $entityName = (Get-View -ViewType VirtualMachine -Property Name | Where-Object {$_.MoRef -eq $entity}).Name $type = “VM” } $object = New-Object PSObject Add-Member -InputObject $object NoteProperty Cluster $FaultyCluster.Name Add-Member -InputObject $object NoteProperty Entity $entityName Add-Member -InputObject $object…
PowerCLI: Getting host alarms
Yesterday I wrote a post about getting vm alarms through PowerCLI. Today it’s time for getting host alarms 🙂 The code is very similar to the one for getting vm alarms, since they both use get-view to grab all info: $VMHosts = Get-View -ViewType HostSystem -Property Name,OverallStatus,TriggeredAlarmstate $FaultyVMHosts = $VMHosts | Where-Object {$_.TriggeredAlarmState -ne “{}”} $progress = 1 $report = @() if ($FaultyVMHosts -ne $null) { foreach ($FaultyVMHost in $FaultyVMHosts) { foreach ($TriggeredAlarm in $FaultyVMHost.TriggeredAlarmstate) { Write-Progress -Activity “Gathering alarms” -Status “Working on $($FaultyVMHost.Name)” -PercentComplete ($progress/$FaultyVMHosts.count*100) -Id 1 -ErrorAction SilentlyContinue $alarmID = $TriggeredAlarm.Alarm.ToString() $object = New-Object PSObject Add-Member -InputObject $object NoteProperty VMHost $FaultyVMHost.Name Add-Member -InputObject $object NoteProperty TriggeredAlarms (“$(Get-AlarmDefinition -Id $alarmID)”) $report += $object } $progress++ } } Write-Progress -Activity “Gathering alarms” -Status “All done” -Completed -Id 1 -ErrorAction SilentlyContinue $report | Where-Object {$_.TriggeredAlarms -ne “”} The output will look something like this: And here’s the function code: function Get-VMHostAlarms…
PowerCLI: Getting vm alarms
A few weeks ago I started to put together a health check script for our vmware environments and the first thing I wanted to have in that report is a list of triggered alarms. To my surprise there was no native cmdlet to retrieve alarms using PowerCLI, instead I had to write a short script to retrieve alarms. So here’s the the script for retrieving vm alarms: $VMs = Get-View -ViewType VirtualMachine -Property Name,OverallStatus,TriggeredAlarmstate $FaultyVMs = $VMs | Where-Object {$_.OverallStatus -ne “Green”} $progress = 1 $report = @() if ($FaultyVMs -ne $null) { foreach ($FaultyVM in $FaultyVMs) { foreach ($TriggeredAlarm in $FaultyVM.TriggeredAlarmstate) { Write-Progress -Activity “Gathering alarms” -Status “Working on $($FaultyVM.Name)” -PercentComplete ($progress/$FaultyVMs.count*100) -Id 1 -ErrorAction SilentlyContinue $alarmID = $TriggeredAlarm.Alarm.ToString() $object = New-Object PSObject Add-Member -InputObject $object NoteProperty VM $FaultyVM.Name Add-Member -InputObject $object NoteProperty TriggeredAlarms (“$(Get-AlarmDefinition -Id $alarmID)”) $report += $object } $progress++ } } Write-Progress -Activity “Gathering VM alarms”…
PowerCLI: Mount NFS stores on multiple hosts
Ever had a bunch of vmware hosts that you want to add a NFS datastore on? It can be quite time consuming to say the least. In our environment we have a couple of NFS stores that should be available on all vmware hosts and I really don’t want to browse around all 50 or so hosts and check if they have them and then mount them if they’re not available. So, of course, I wrote a small powercli script to check if the NFS stores are mounted and then mount them if they’re not mounted: $vmhosts = Get-View -ViewType HostSystem -Filter @{“Runtime.ConnectionState” = “connected”} -Property name $progress = 1 $mounts = 1 foreach ($vmhost in $vmhosts) { Write-Progress -Activity “Checking $($vmhost.Name)” -PercentComplete ($progress/$vmhosts.count*100) if ((Get-Datastore -VMHost $vmhost.name | Where-Object {$_.name -eq “MGMT-ISO”}) -eq $NULL){ New-Datastore -vmhost $vmhost.name -Name MGMT-ISO -Nfs -NfsHost <nfsHost> -Path /MGMT-ISO $mounts++ } if ((Get-Datastore -VMHost…
Powershell: Listing activated clients on KMS server
If you are using a KMS server for activating servers and clients in your environment, you may have noticed that there’s really no obvious way to get a list of all the clients that have been activated by the KMS server. One way to get that overview is by using VAMT (http://technet.microsoft.com/en-us/library/hh824953.aspx), but since that tool is based on pulling info from clients and not from the KMS server it is not suitable for everyone. Thankfully, there’s PowerShell 🙂 Getting a list of all activated KMS clients through PowerShell is actually a simple one-liner: $(foreach ($entry in (Get-EventLog -Logname “Key Management Service”)) {$entry.ReplacementStrings[3]}) | sort-object -Unique What this does is look through the Key Management Service eventlog, grab only the client name and then remove all duplicates (since a client activates itself at regular intervals, there will be duplicates).
Powershell: Measuring latency on DNS queries
A little while ago we have some issues with one of our Exchange environments. The symptoms was that basically everything was slow, opening address books, owa, sending mails and so on. After some digging it occurred to me that this may be DNS related, so I used a one-liner in powershell to see how fast our DNS servers responded: Measure-Command {Resolve-DnsName google.com -DnsOnly -Type A -NoHostsFile -server x.x.x.x} And sure enough, we were seeing response times in the range of 5-7 seconds. Way more than enough to cause our problems. After rebooting the DNS servers ours problems disappeared. But, as always, I had to play around with created a powershell function. The function resolves a few records from locally defined DNS servers and some publicly available DNS servers (or a custom list if you want) and measures the average latency (in milliseconds). The results are put in a list and…
Activate Server 2012 (R2) by telephone
The option for activating Server 2012 by phone is kinda hidden, so I have to write this down before I forget it: Win+R – SLUI 4