Powershell and the Customisation of Numerical Results
In the event of running a powershell script and you are returned with values with an unreadable number of decimal places or in the incorrect format (not Currency, Percentage or Hex etc), you can force powershell to output the correct format of this number using some simple “conversion’ techniques.
Using the “{0:XX}” -f pre cursor to convert the chosen variable, you can tell powershell that you want the variable output in the chosen format with the selected number of decimals
If we set the variable $var = 123.1234 and then use the following precursors to decide wether we want to displayu a Number with fixed decimals (N), Currency (C) or Percentage (P) we will get the output shown:
$var =123.1234
|
1 |
"{0:N2}" -f $var |
123.12
|
1 |
"{0:C3}" -f $var |
$123.123
|
1 |
"{0:P1}" -f $var |
123.1%
In the event of wanting to use the X (Hex) or D (Decimal) convertors you will need to ensure that you variable is a whole number.
$var = 123
|
1 |
"{0:X0}" -f $var |
7B
|
1 |
"{0:D5}" -f $var |
00123
Finding CPU / Memory Maximum
I have been asked many times to try and work out the maximum performance of a VM from a CPU & Memory perspective for a number of reasons. I sat down and started plugin my way through to GUI to find the relevant information and then find out how to get the same from PowerGUI.
The below script is what I came up with which displays results as shown in the output below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$results = @() foreach ($cluster in get-cluster){ foreach ($vm in $cluster | get-vm | ? {$_.PowerState -like "*on"}){ $result = "" | select Cluster, VM, CPUCount, CPUUsage, MEMSize, MEMUsage $result.Cluster = $cluster.Name $stats = Get-Stat -Entity $vm -Start (get-date).AddMonths(-1) -IntervalMins 120 ` | ? {$_.Value -le 100} | sort -Descending Value $result.VM = $vm.Name $result.CPUCount = $vm.NumCpu $result.MEMSize = $vm.MemoryMB $result.CPUUsage = ($stats | ? {$_.MetricId -like "cpu.usage.average"} ` | select -First 1).tostring() + " %" $result.MEMUsage = ($stats | ? {$_.MetricId -like "mem.usage.average"} ` | select -First 1).tostring() + " %" $result $results += $result } } |
The script returns the Clusters and the VM’s in each Cluster and then outputs the relevant details; returning values for every 120 mins (2 hours) which is the default period for data saved for 1 month period (specified in script).
The output for one VM is shown below, which is the same info as we can see in the results taken from Virtual Center for the same VM.
| Cluster | VSI Cluster 01 |
| VM | VMName01 |
| CPUCount | 2 |
| CPUUsage | 91.88% |
| MEMSize | 4096 |
| MEMUsage | 24.55% |
Smart Auditor “Unable to play back this recorded session file”
I recently went to view a session in the Citrix Smart Auditor application, and got the following pop up.
I thought I have been able to view these sessions on my old PC so I went to the bible (google) and found the web link at the bottom of this article.
The fix to my particular problem was to up the ‘SmartAudPlayer.exe.config’ file located in the path ‘c:\program files\citrix\smart auditor\player\bin’ and change the version of the player to the version I was seeing listed in the pop-up.
<add key=”Windows” value=”xx.xxx.xxxx”
This resolution worked a treat for me, giving me access to all my smart auditor sessions.
Identifying VM Snapshots
Below you will see some code to identify VM snapshots in your Virtual Center environment.
Here you will see the VM that the snapshot is from, the Name, Cluster, Datastore, PowerState, Size and the date that the snapshot was taken. This information is extremely useful for finding all the snapshots in your VC environment to identify what can be deleted to save some space.
|
1 2 3 4 5 6 7 8 9 10 11 |
Get-VM | Get-Snapshot | %{ $vm = $_ $clus = $vm.VM | get-cluster $ds = $vm.VM | Get-Datastore $vm | select VM, Name, @{N="Cluster";E={$clus.Name}}, @{N="Datastore";E={$ds.Name }}, @{N="PowerState";E={ $vm.PowerState}}, @{N="Size";E={$vm.SizeMB}}, @{N="Created";E={$vm.Created}} } | ft -AutoSize |
Code written in PowerCLI.
VM Block Size & Max. Storage Size (vSphere 4)
I was working on a virtual environment recently and come across a problem with the maximum storage size for a VMDK. Upon looking further into this, I found that the block size for the datastores across our environment were mixed between, 1MB, 2MB and 4MB.
After some investigation, I found some great sites detailing the differences, best practices and recommendations. Below I will try to summarize this information for all interested.
If you run the below powerCLI script you will get a list of your Datastores, the driver version and the block size assigned…
|
1 2 3 |
Get-Datastore | Get-View | Select-Object Name, @{N="Version";E={$_.Info.Vmfs.Version}}, @{N="BlocksizeMB";E={$_.Info.Vmfs.BlockSizeMB}} |
Results…
| Name | Version | BlocksizeMB |
| ABC-0211-0028-TIER1-500GB-0080 | 3.31 | 2 |
| ABC-0212-0028-TIER1-1TB-0052 | 3.33 | 4 |
| ABC-0213-0028-TIER1-500GB-0057 | 3.31 | 2 |
| ABC-0214-0028-TIER1-500GB-0055 | 3.31 | 1 |
| ABC-0215-3518-TIER2-1TB-0017 | 3.46 | 1 |
| ABC-0215-3528-TIER1-1TB-0025 | 5.54 | 4 |
… from this script you can easily determine if the blocks sizes are different in your environments.
As stated above these blocks sizes determine the maximum size of your VMDK files and therefore default drives in the guest OS. This may also affect your Storage vMotion where you want to move a VMDK from one datastore to another. If the destination datastore has a smaller block size and the image is larger than the maximum volume size (above) then you will receive an error and the svMotion will not work. The block size and maximum volume size is shown below (Table 1)
| Block Size | Max Volume Size (VMFS-3) |
| 1MB | 256GB |
| 2MB | 512GB |
| 4MB | 1TB |
| 8MB | 2TB |
In the event where you may want to resize all or all of your datastores, all data must be removed and the datatore itself must be reformatted with the new block size.
Having the larger block size will waste some disk space, but due to the number of files for each VM (~10-15) wasting a small amount of disk space (~100MB) will far outweigh the advantaged of having Storage vMotion working effectively throughout your VMware environment.
Issues also come into play when Snapshots are left on disk for a period of time and the size of the image grows larger than the datastores maximum file size. Although the typical scenario suggests that snapshots shouldn’t be left on disk for this to occur, this can still be an issue.
In Table 2, below, the version of the file system, and from which ESX version they were formatted is shown. Where environments have been upgraded over time, you may find that you have mixed file system versions. For versions above 3.21 (ESX 3.0.0) no additional work is necessary if mixed version are found. The information presented here is only for information purposes only.
| FS Version | Formatted with ESX version |
| 3.21 | ESX 3.0.0 |
| 3.31 | ESX 3.5.0 |
| 3.33 | ESX 4.0 |
| 3.46 | ESX 4.1 |
| 5.54 | ESX 5.0 |
After looking at all the problems above, the best solution to this potential problem, is to analyse all the current and future disk sizes you require and create a block size that will accommodate for these. If for instance you have a single server that will require a single disk image of 750GB, and all others only require 200GB, it would be advisable to create the datastore with a 4MB block size to allows for any potential storage vMotion you may require in the future.
Further information on these two topics can be found on the VMware knowledge base at the following URL’s:
http://bit.ly/nuHHeZ – Block Size Limitations
http://bit.ly/pgyx80 – Snapshot Size Limitations



