Skip to content

Instantly share code, notes, and snippets.

@virtuallywired
Created January 3, 2024 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtuallywired/c82f71568afd4b4187e678c2060998dd to your computer and use it in GitHub Desktop.
Save virtuallywired/c82f71568afd4b4187e678c2060998dd to your computer and use it in GitHub Desktop.
Powershell script which uploads and executes the customize bash script that freezes and prepairs the ESXi virtual Appliance for instant cloning
# Witten by Nicholas Mangraviti #VirtuallyWired
# Credit to William Lam for the customize.sg script - https://williamlam.com/2018/05/leveraging-instant-clone-in-vsphere-6-7-for-extremely-fast-nested-esxi-provisioning.html
# Requires Posh-SSH Module, this script will attempt to install it if not already installed.
# Note, this script is not the full instant cloning script this Powershell script which uploads and executes the customize bash script that freezes and prepairs the ESXi virtual Appliance for instant cloning.
# Tested with vSphere 6.7, 7.0 & 8.0
# Update Nested ESXi Template IP Below
$SourceESXiIP = "172.18.9.19"
# Get ESXi Host Credentials
$HostCred = Get-Credential
$CustomizeScriptUrl = 'https://raw.githubusercontent.com/lamw/instantclone-community-customization-scripts/master/esxi65-67/customize.sh'
# --- This will install the Posh-SSH Module if not found.
Write-Host "Checking for Required Module Posh-SSH" -ForegroundColor Green
if (Get-Module -ListAvailable -Name Posh-SSH) {
Write-Host "Posh-SSH Module Already Installed" -ForegroundColor Green
}
else {
Write-Host "Posh-SSH Module Not Found, Attempting to Install" -ForegroundColor Yellow
Install-Module -Name Posh-SSH -Scope CurrentUser -Force -Confirm:$false
}
# --- Importing the Posh-SSH Module.
if (Get-Module -ListAvailable -Name Posh-SSH) {
Write-Host "Importing Posh-SSH Module" -ForegroundColor Green
Import-Module -Name Posh-SSH -Force
}
Else {
Write-host "Something Went Wrong, Stopping Script" -ForegroundColor Red
Break
}
# Download customize.sh Script from repo
Invoke-WebRequest -Uri $CustomizeScriptUrl -OutFile '.\customize.sh'
# Open SSH Session to Nested ESXi Template
$SSH = New-SSHSession -ComputerName $SourceESXiIP -Credential $HostCred -AcceptKey -Force -WarningAction SilentlyContinue
Write-Host "Started New SSH Session: $($SSH.Connected)" -ForegroundColor Green
# Copy the customize.sh script to the Host
Set-SCPItem -ComputerName $SourceESXiIP -Credential $HostCred -Path '.\customize.sh' -Destination '/tmp/' -AcceptKey -Force -WarningAction SilentlyContinue
$Checkfile = Invoke-SSHCommand -SSHSession $SSH -Command 'ls /tmp/'
# Update permissions on cutomize.sh to be executable
If ($Checkfile.Output -contains "customize.sh") {
Write-Host "Customize.sh Script uploaded Successfully" -ForegroundColor Green
Invoke-SSHCommand -SSHSession $SSH -Command 'chmod +x /tmp/customize.sh' | Out-Null
Invoke-SSHCommand -SSHSession $SSH -Command '/tmp/customize.sh' -TimeOut 1 -ErrorAction SilentlyContinue | Out-Null
}
Start-Sleep -Seconds 5
Write-Host "ESXi Virtual Appliance is Frozen and ready for Instant Cloning" -ForegroundColor Cyan
# Close all SSH Sessions
$SessionIDs = (Get-SSHSession).SessionId
If (Get-SSHSession) {
Foreach ($SessionID in $SessionIDs) {
Remove-SSHSession -SessionId $SessionID | Out-Null
Write-Host "Closing SSH Session ID: $($SessionID)" -ForegroundColor Gray
}
If (!(Get-SSHSession)) {
Write-Host "Succesfully Closed All SSH Sessions" -ForegroundColor Gray
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment