Category Archives: PowerShell

General PowerShell Stuff

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
}

Merge multiple CVS files into a single CVS file

This simple powershell script will merge multiple CVS files into a single. Run this script from working directory where the CVS files are located:


$CSVFiles = Get-ChildItem -Filter *.csv | Select-Object -ExpandProperty FullName

$OutputFile = "c:\temp\merged.csv"

$Output = @();
foreach($CSV in $CSVFiles) {
    if(Test-Path $CSV) {
        $FileName = [System.IO.Path]::GetFileName($CSV)
        $temp = Import-CSV -Path $CSV | select *, @{Expression={$FileName};Label="FileName"}
        $Output += $temp
    } else {
        Write-Warning "$CSV : No such file found"
    }
}

$Output | Export-Csv -Path $OutputFile -NoTypeInformation