[PowerCLI] Show ESX cluster CPU/Mem averages

As a follow-up on my earlier [PowerCLI] Report ESX vCPU vs pCPU & Memory ratio script, I created a script that only shows average CPU and Memory usages per Cluster.
This brief cluster overview is (for example) usable for management reporting.

See [PowerCLI] Report ESX vCPU vs pCPU & Memory ratio for more information about how to read the output.

<#
.SYNOPSIS
  Shows average cluster resources usage
 
.DESCRIPTION
  The script shows information about vCPU to pCPU ratio and memory usage/commitment. Excludes hosts in Maintenance.
  Based on CheckResources.ps1 script.
 
.PARAMETER <Parameter_Name>
  None
.INPUTS
  None 
.OUTPUTS
  Table output in the 5 columns.
 
.NOTES
  Version:        1.0
  Author:         Daniel Tromp	
  Creation Date:  23-06-2014
  Purpose/Change: Get CPU Ratio and Memory usage/commitment
  
.EXAMPLE
  ./CheckClusterAverages.ps1
#>
$array = @();
Foreach ($Cluster in (Get-Cluster |Sort Name)){
     $ClusCPUratio  = 0
     $ClusNumvCPUs  = 0
     $ClusNumpCPUs  = 0
     $ClusNumvMem   = 0
     $ClusGrantMem  = 0  
     $ClusUsageMem  = 0
     $ClusMemUsable = 0
     $ClusMemUsed   = 0
     Foreach ($ESXHost in ($Cluster |Get-VMHost -State Connected |Sort Name)){
         $HostNumvCPUs = 0
	     $HostNumvMem  = 0
         Foreach ($VM in ($ESXHost |Get-VM)){
	         $HostNumvCPUs += ($VM).NumCpu
	         $HostNumvMem  += ($VM).MemoryGB
         }	
         $ClusNumvCPUs  += $HostNumvCPUs
         $ClusNumpCPUs  += ($ESXHost).NumCpu
	     $ClusMemUsable += ($ESXHost | Get-View).Hardware.MemorySize/1GB
	     $ClusNumvMem   += $HostNumvMem
	     $ClusMemUsed   += ($ESXHost).MemoryUsageGB
     }
     $ClusCPUratio   = "{0:N0}" -f ($ClusNumvCPUs / $ClusNumpCPUs)
     $ClusGrantMem   = "{0:N0}" -f ($ClusNumvMem/$ClusMemUsable*100)
     $ClusUsageMem   = "{0:N0}" -f ($ClusMemUsed/$ClusMemUsable*100)
	
     $HostVar = @{Cluster=$($Cluster.name); HostCount=($Cluster|Get-VMHost -State Connected).Count; AvgCPURatio=$ClusCPUratio + ":1"; AvgMemGranted=$ClusGrantMem + "%"; AvgMemUsed=$ClusUsageMem + "%"} 
     $array +=  New-Object PSObject -Property $HostVar
}
$array | Select-Object Cluster,HostCount,AvgCPURatio,AvgMemGranted,AvgMemUsed | Format-Table -AutoSize

Output:
CheckClusterAverages

[PowerShell] Freeware script editor comparison

Having a PowerShell script editor that helps you in with your task at hand improves your speed and quality enormously. This is for PowerShell beginners and even for expert users. I myself started with MS Notepad as my PowerShell editor, it’s fast but there are zero features that help you. Soon I switched over to Notepad++, a fast and simple editor with syntax support/highlighting for PowerShell.

Nowadays I also need debug support for the big and complex scripts, there are many varieties of script editors that have debugging. I choose PowerGUI (Quest/Dell) as my script editor with debug capability, also because of the PowerPacks that are created by their community. PowerPacks are pre-built scripts with customizable interfaces, for example the VMware Community PowerPack gives a management console for your daily VMware tasks.
Not to forget the Microsoft very own Windows PowerShell ISE, I’m not a huge fan. PowerShell ISE is fast and has debugging, still it lacks the community hooks and has less features than PowerGUI.

MS Notepad, used when a better editor is not available:
MSNotepad
Notepad++, replaces MS Notepad and gives great inside in to any script:
NotepadPlusPlus
PowerShell ISE, fast but missing features:
PowerShellISE
PowerGUI, great for editing big and complex scripts:
PowerGUI

More PowerShell script editors, this is my selection.

[PowerCLI] Change DefaultPSP and SATP/PSP for every lun on every host

Changing the default Path Selection Policy (PSP) on every host can be time consuming challenge, after that you also need to change the PSP on every lun on every host.
In the past you needed to SSH login to every host and change the settings “manually”, but there is also a PowerCLI way to do that. Using PowerCLI cmdlet Get-EsxCli you can execute the esxcli command from PowerCLI directly to the host. Using foreach to loop through all the hosts and check and change with Get-EsxCli the setting on every host and lun.
You can simply change the $VMW_SATP and $VMW_PSP for your required Storage Array Type Plugin (SATP) and Path Selection Policy (PSP), the script below is configured for IBM SVC-based systems (SVC, V7000, Actifio).

Code:

$VMW_SATP="VMW_SATP_SVC"
$VMW_PSP="VMW_PSP_RR"

$allVMhost = Get-VMHost | sort Name
foreach ($vmhost in $allVMhost){
	$esxcli = $vmhost | Get-EsxCli
	#Check VMW_SATP
	if ($esxcli.storage.nmp.satp.list() | where-object {$_.Name -eq $VMW_SATP -and $_.DefaultPSP -eq $VMW_PSP}){write-host "Default $VMW_SATP $VMW_PSP $vmhost correct"}
	else{$esxcli.storage.nmp.satp.set($null,$VMW_PSP,$VMW_SATP)}
    #VMW_SATP
	
	#Check VMW_PSP
	$psprrlun = $esxcli.storage.nmp.device.list() | where-object {$_.StorageArrayType -eq $VMW_SATP -and $_.PathSelectionPolicy -ne $VMW_PSP} | Select Device
	if ($psprrlun -eq $null){write-host "LUN setting $VMW_SATP $VMW_PSP $vmhost correct"}
	else{foreach ($lun in $psprrlun | foreach { $_.Device }){$esxcli.storage.nmp.device.set($null, $lun, $VMW_PSP)}}
	#VMW_PSP
}

Output:
ChangeSATP-PSP

[PowerCLI] Report ESX vCPU vs pCPU & Memory ratio

Here is a script that will show you 3 things per host:
1. vCPU:pCPU ratio,
2. Host Memory Granted,
3. Host Memory Used.

It will count all the vCPUs on a host and the (physical) pCPUs, the vCPU differed by pCPU is the ratio. The vCPU:pCPU should in my opinion be:
High CPU load environment <4:1 Medium CPU load environment ~4:1 Low CPU load environment >4:1

The “Host Memory Granted” percentage value is the sum of all VM granted memory compared to the total host memory, you can also call this Worst-Case usage. The “Host Memory Used” percentage is the real value of the host used memory.
“Host Memory Granted” should be <90% unless you know what you are doing. "Host Memory Used" should be <80% depending on the cluster size (see Duncan’s HA Deep Dive).

$TotalNumvCPUs = 0
Foreach ($Cluster in (Get-Cluster |Sort Name)){
  $HostNumvCPUs = 0
  $HostNumvMem  = 0
  $array = @();
  Foreach ($ESXHost in ($Cluster |Get-VMHost |Sort Name)){
    Foreach ($VM in ($ESXHost |Get-VM)){ # or only powered on, Foreach ($VM in ($ESXHost |Get-VM| Where-Object {$_.PowerState -eq "PoweredOn"})){
	  $HostNumvCPUs += ($VM).NumCpu
	  $HostNumvMem  += "{0:N0}" -f ($VM).MemoryGB
    }
	$HostCPUratio   = "{0:N0}" -f ($HostNumvCPUs / ($ESXHost).NumCpu)
	$HostMemUsable  = "{0:N0}" -f (($ESXHost | Get-View).Hardware.MemorySize/1GB)
	$HostGrantedMem = "{0:N0}" -f ($HostNumvMem/$HostMemUsable*100)
	$HostUSageMem   = "{0:N0}" -f (($ESXHost).MemoryUsageGB/$HostMemUsable*100)
	
	$HostVar = @{cluster=$($Cluster.name); host=$($ESXHost.name); HostCPURatio=$HostCPUratio + ":1"; HostMemGranted=$HostGrantedMem + "%"; HostMemUsed=$HostUSageMem + "%"} 
	$Newobject = New-Object PSObject -Property $HostVar
	$array += $Newobject
        $TotalNumvCPUs += $HostNumvCPUs
	$TotalNumvMem  += $HostNumvMem
        $HostNumvCPUs = 0
	$HostNumvMem  = 0
  }
  $array | Select-Object cluster,host,HostCPURatio,HostMemGranted,HostMemUSed | Format-Table -AutoSize
  $TotalNumvCPUs = 0
  $TotalNumvMem  = 0
}

Output:
CheckResources

[PowerCLI] Check Ballooning vCenter wide (quick)

I used the Check Ballooning script allot in the pasted, the script can be found on many blogs and sites. I simply adjusted the script so it would also show Compressed Memory.
Why add “Compressed Memory”?, it gives a extra dimension to the ballooning an swapped values.
Compressed Memory tells you how memory intensive the VM is, so:
Higher compression value then the ballooning or the swapped: Lower priority
Lower compression value then the ballooning or the swapped: Higher priority

The code I use:

$myCol = @()
foreach($vm in (Get-View -ViewType VirtualMachine | Where-Object `
  {$_.Summary.QuickStats.BalloonedMemory -ne "0"})){
    $Details = "" | Select-Object VM, BalloonedMemory ,SwappedMemory ,CompressedMemory
    $Details.VM = $vm.Name
    $Details.BalloonedMemory  = $vm.Summary.QuickStats.BalloonedMemory
    $Details.SwappedMemory    = $vm.Summary.QuickStats.SwappedMemory
    $Details.CompressedMemory = $vm.Summary.QuickStats.CompressedMemory
    $myCol += $Details
  }
$myCol

Based on the script of ICT-Freak.

Undeduplicatable/uncompressible data

Testing anything that uses deduplication and/or compression? Use data that is entirely unique.

You can create unique data with the /dev/random (or urandom) but it’s terribly slow if you need allot of data, 10-20MB/s(per thread). OpenSSL has a faster and better way to create random data, using OpenSSL will improve the speed up to 120MB/s(per thread).
Create a 1GB unique file with the following command(openssl required):
openssl rand -out 1GB-random-A -base64 $(( 2**30 * 3/4 ))

Want to fill 1TB of storage with random data, use the line below (openssl & bash required):
for i in {1..1000}; do openssl rand -out 1GB-random-%i -base64 $(( 2**30 * 3/4 )); done

Using this while testing an PureStorage storage array I was able to keep the dedup/compression ratio on a 1:1 with 10TB of pseudo data. (FYI same result on Nexenta)

4 unique 1GB files you can download and use directly:
[wpdm_file id=1] [wpdm_file id=2] [wpdm_file id=3] [wpdm_file id=4]

[PowerShell] AD enable UNIX Attributes

When using Active Directory and UNIX Attributes (User/Group Unique Identifiers) the first thing you need is the Identity Management for UNIX Components. Now you are ready to start using UNIX Attributes.

Adding UNIX Attributes to every user and group in your domain, using PowerShell.

Get every user that already has UNIX Attributes enabled, only change the and the to you specifics:

Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
Import-Module ActiveDirectory
$usuarios = Get-ADUser -Filter * -SearchBase "DC=;,DC=" -Properties:* | sort SamAccountName
foreach($usr in $usuarios){write-host $usr.SamAccountName,$usr.mssfu30nisdomain,$usr.uidnumber}

Setting the Unix IDs it a bit more difficult, you need to do 4 thing:
1. Get the last Unix User ID available
2. Enable NIS on the User account
3. Give the user a unique Unix User ID
4. Make the user member of a Unix Group

Set every user that didn’t receive a UNIX Attributes yet, only change the and the to you specifics:

Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
Import-Module ActiveDirectory
$NIS = Get-ADObject "CN=NISdomain,CN=ypservers,CN=ypServ30,CN=RpcServices,CN=System,DC=myDomain,DC=local" -Properties:* #Get NIS server information
$maxUid = $NIS.msSFU30MaxUidNumber #Get the last used User ID

$usuarios = Get-ADUser -Filter * -SearchBase "DC=myDomain,DC=local" -Properties:* #Get all users
foreach($usr in $usuarios)
{
  if ($usr.mssfu30nisdomain -eq $null){
  Set-ADUser -Identity "$($usr.SamAccountName)" -Replace @{mssfu30nisdomain = "NISdomain"} #Enable NIS
  Set-ADUser -Identity "$($usr.SamAccountName)" -Replace @{gidnumber="10000"} #Set Group ID
  $maxUid++ #Raise the User ID number
  Set-ADUser -Identity "$($usr.SamAccountName)" -Replace @{uidnumber=$maxUid} #Set User ID
  Write-Host -Backgroundcolor Green -Foregroundcolor Black $usr.SamAccountName changed #Write Changed Username to console
  }
  else{Write-Host -Backgroundcolor Yellow -Foregroundcolor Black $usr.SamAccountName unchanged} #Write Unchanged Username to console with a yellow background
}