Category Archives: PowerShell

General PowerShell Stuff

How to Retrieve vSAN License Key from an ESXi Cluster using PowerCLI

vSAN is a software-defined storage solution from VMware that is integrated with the vSphere platform. To use vSAN, you need to have a valid license key assigned to your ESXi hosts. In this blog post, we’ll show you how to retrieve the vSAN license key from an ESXi cluster using PowerCLI.


#Assuming already established connection to your VCSA.

$clust = Read-Host "Enter Cluster Name: "
$cluster = Get-Cluster $clust 

$serviceinstance = Get-View ServiceInstance
$LicManRef=$serviceinstance.Content.LicenseManager
$LicManView=Get-View $LicManRef
$licenseassetmanager = Get-View $LicManView.LicenseAssignmentManager
$script:licInfo = $licenseassetmanager.GetType().GetMethod("QueryAssignedLicenses").Invoke($licenseassetmanager,@($dd1.MoRef.Value))
$lkey = $licInfo.assignedlicense.LicenseKey 


foreach ($obj in $licInfo){
    if ($obj.EntityDisplayName -eq $cluster){
    $key = $obj.assignedlicense.LicenseKey
    Write-Host "$cluster has vSAN $key"
    }


Create vMotion TCP/IP stack when making vMotion VMKernel interface with PowerCli

This series of commands will set the default gateway on the vMotion kernel to use dedicated Default Gateway for layer 3 vMotion.

After you connect to the target VCSA (vCenter) update the variables



# Update the Variables Below
$esxi = "ESXi hostname"
$vdsswitchname = "DVS Switch Name"
$vmotionportgroup = "Portgroup to use for VMotion"
$vmotionIP = "IP address of the Vmotion Kernel"
$vmotiongateway = "The Vmotion Default Gatway"
$VmotionSubnetMask = "VMotion Subnet Mask"
# End of Variables

$vds = Get-VDSwitch $vdsswitchname
$hostobject = get-vmhost $esxi
$vmostack = Get-VMHostNetworkStack -vmhost $hostobject | ? {$_.ID -eq "vmotion"}
New-VMHostNetworkAdapter -VirtualSwitch $vds -VMHost $hostobject -PortGroup  -ip $vmotionIP -SubnetMask $VmotionSubnetMask -NetworkStack $vmostack -Confirm:$false
$vmostackconfig = New-Object VMware.Vim.HostNetworkConfig
$vmostackconfig.NetStackSpec = New-Object VMware.Vim.HostNetworkConfigNetStackSpec[] (1)
$vmostackconfig.NetStackSpec[0] = New-Object VMware.Vim.HostNetworkConfigNetStackSpec
$vmostackconfig.NetStackSpec[0].NetStackInstance = New-Object VMware.Vim.HostNetStackInstance
$vmostackconfig.NetStackSpec[0].NetStackInstance.RequestedMaxNumberOfConnections = 11000
$vmostackconfig.NetStackSpec[0].NetStackInstance.CongestionControlAlgorithm = 'newreno'
$vmostackconfig.NetStackSpec[0].NetStackInstance.IpRouteConfig = New-Object VMware.Vim.HostIpRouteConfig
$vmostackconfig.NetStackSpec[0].NetStackInstance.IpRouteConfig.DefaultGateway = $vmotiongateway
$vmostackconfig.NetStackSpec[0].NetStackInstance.Key = 'vmotion'
$vmostackconfig.NetStackSpec[0].Operation = 'edit'
$changmode = 'modify'
$_this = Get-View -Id $hostobject.NetworkInfo.Id
$_this.UpdateNetworkConfig($vmostackconfig, $changmode)

Uninstall VIBs on ESXi via PowerCLI

I ran across a need to have to uninstall a VIB from ESXI once installed. I Didnt want to login to each ESXi via SSH to preform the ESXCLI command so this is how you can do it via PowerCLI.

the ESXi command line would be:


esxcli software vib remove -n=usbcore-usb --dry-run

PowerCLI Code


$VMhost = "ESXi-Hostname"
$VIBNAME = "usbcore-usb"

$esxcliRemoveVibArgs = $esxcli.software.vib.remove.CreateArgs()
$esxcli = Get-EsxCli -VMHost $VMhost -V2
$esxcliRemoveVibArgs.dryrun = $true  #Change this to False to actually perform the uninstall)

$vib = $esxcli.software.vib.list.Invoke() | where{$_.Name -match $VIBNAME }

$esxcliRemoveVibArgs.vibname = $vib.Name 
$esxcli.software.vib.remove.Invoke($esxcliRemoveVibArgs)
	

Function to Mounting ISO in windows with File-Open Browser-Popup with Powershell

This is very simple function that you can add to your ISE profile that allows a file-open dialog box that looks specifically for ISO files and uses the Mount-DiskImage Command.


Function Mount-ISO {

$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    InitialDirectory = [Environment]::GetFolderPath('Desktop')
    Filter = 'ISO-Images (*.ISO)|*.iso'
}

$null = $FileBrowser.ShowDialog()

Mount-DiskImage -ImagePath $FileBrowser.FileName
}