Scripts

Welcome to my scripts collection. On this page, I’ll publish some scripts from time to time which I’ve created, or found and tailored to my needs, during my work to solve problems. They are mostly VMware-related, but not only.

Disclaimer

Some of the scripts I’ve created by myself after some time of tinkering and try & error. Some scripts I’ve found somewhere on the internet. Usually, I’m not using the whole script I’ve found, but at least parts of it because of a specific function or feature. Unfortunately, and you might know that by yourself, when you’re in a hurry, or when you get into the tunnel vision of troubleshooting, you can’t document everything directly. And maybe, after solving the problems, you’ve closed all browser tabs or whatever, and documentation is getting more complex.

For the future, I will try to do my best to credit people in the scripts when I’ve used their scripts (or parts of it). That might be not the case with all scripts I’ll publish here because they are already “old” or isn’t even worth publishing. But the good gems of scripts I’ll publish, and hopefully I can add credits to the scripts.

If you find a script here, and if you should recognize your code here, please drop me a mail so I can add the credits to the specific script. Thank you!


 

Table of Contents


VMware Scripts

VM related scripts

Get a list of VMs which have tags and assigned storage policies

The script generates a list of VMs, their assigned tags and storage policy, as well as the ESXi hosts and the fault domain of the hosts. I’ve got some VMs with specific storage-related tags on VMware Cloud on AWS running. On a stretched cluster, it makes sense to have the VMs on that part of the cluster, where also the VMDK files are stored. Depending on the fault domain, there are VM tags (host-affinity) and vSAN storage policies. Many thanks to Wolfgang Taitl for his helping hands and eyes!

 

<#

.SYNOPSIS
  Script for generating a CSV document with information about VMs, ESXi nodes, tags and policies.
.DESCRIPTION
  This script gathers all necessary information from vCenter to generate a CSV file.
  The file contains a list which contains each a column for VM Name, ESXi Host, Assigned Site Tag, Assigned Storage Policy, Host Fault Domain.
  
  Preferred and non-preferred are terms often used in vSAN environments, where a stretched cluster has been deployed.
  One part of that stretched cluster is the preferred site, the other one the non-preferred site.

  With the CSV file, you can check if there are VMs which are causing cross traffic. That means that the VM runs in the preferred site (CPU / memory),
  and the disk files (VMDK) are running in the non-preferred site.

.INPUTS
  $viserver = "vcenter.yourdomain.com"   => vCenter FQDN
  $viuser = "admin@vcenter.local"        => vCenter admin user
  $vipassword = "VMware1!"               => vCenter admin password

  Optional input:
  $ListName = (Get-Folder Workloads | Get-VM)     => replace this with "Get-VM" if you don't have a specific folder
  
.OUTPUTS
  Output file generated here: C:\temp\StoragePolicies.csv

.NOTES
  Version:        1.0
  Author:         Karl Widmer
  Creation Date:  23.10.2020
  Purpose/Change: Initial script development, tests
  Helping hands:  Wolfgang Taitl (https://twitter.com/vNote42)

#>

#Connect to vCenter
$viserver = "vcenter.yourdomain.com"
$viuser = "admin@vcenter.local"
$vipassword = "VMware1!"

Write-host "Connecting to $viserver..." -foregroundcolor "yellow"
Connect-VIServer $viserver -user $viuser -password $vipassword
Write-host "Connected to $viserver." -foregroundcolor "green"

#-----------------------------------------------------------[Functions]------------------------------------------------------------

# Create custom function to transpose data from columns to rows
function Transpose-Data{
    param(
        [String[]]$Names,
        [Object[][]]$Data
    )
    for($i = 0;; ++$i){
        $Props = [ordered]@{}
        for($j = 0; $j -lt $Data.Length; ++$j){
            if($i -lt $Data[$j].Length){
                $Props.Add($Names[$j], $Data[$j][$i])
            }
        }
        if(!$Props.get_Count()){
            break
        }
        [PSCustomObject]$Props
    }
}

#-----------------------------------------------------------[Execution]------------------------------------------------------------

Write-host "Getting the VM list..." -foregroundcolor "green"
# Create an array and fill it with information
$ArrayStorage = @()
# List of VM names
$ListName = (Get-Folder VMFolder | Get-VM)

Write-Host "Collecting the VM Hosts..." -ForegroundColor "green"
# List of VM hosts where the VMs are running on
$ListVMHosts = @()
foreach($VMHost in ($ListName | Select-Object VMHost)) {
    $ListVMHosts += $VMHost.VMHost.Name
}

Write-host "Collecting the tags..." -foregroundcolor "green"
# List of tags assigned to the VM
$ListTags = @()
foreach($Tag in ($ListName | Select-Object @{Name="Tag";Expression={(Get-TagAssignment -Entity $_).Tag.Name}})) {
    $ListTags += $Tag.Tag
}

Write-host "Gathering the assigned storage policies..." -foregroundcolor "green"
# Assigned storage policy
$ListPolicies = @()
foreach($Policy in ($ListName | Get-SpbmEntityConfiguration | Select-Object StoragePolicy)) {
    $ListPolicies += $Policy.StoragePolicy.Name
}

Write-host "Gathering the fault domain ..." -foregroundcolor "green"
# Assigned storage policy
$ListFaultDomain = @()
foreach($VMHostEntry in $ListVMHosts) {
    $ListFaultDomain += (Get-VsanFaultDomain -VMHost $VMHostEntry).Name 
}

Write-host "Doing some magic..." -foregroundcolor "green"
# Transpose data from from columns to rows, see custum function above, and export to variable
$a=Transpose-Data 'VM Name', 'ESXi Host', 'Assigned Site Tag', 'Assigned Storage Policy', 'Host Fault Domain' $ListName, $ListVMHosts, $ListTags, $ListPolicies, $ListFaultDomain

# Export variable to CSV file
$a | export-csv -NoTypeInformation -Delimiter ";" -path C:\temp\StoragePolicies.csv -Force

Write-host "Done!" -foregroundcolor "green"

Write-host "Disconnecting from all vCenter servers..." -foregroundcolor "yellow"
Disconnect-VIServer * -confirm:$false
Write-host "Disconnected from all vCenter servers. Bye!" -foregroundcolor "green"