Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
<# .SYNOPSIS Perform steps to upgrade all ESXi hosts in a cluster. .DESCRIPTION Use this script to upgrade all ESXi hosts in a cluster with an offline ZIP file. This script assumes availability of the following modules: - VMware-PowerCli .PARAMETER - Cluster: name of the cluster to migrate. .EXAMPLE .\Upgrade-Cluster.ps1 -Cluster <clustername> .NOTES Script name: Upgrade-Cluster.ps1 Author: Jeroen Buren DateCreated: 07-03-2020 #> [CmdletBinding(SupportsShouldProcess=$true)] Param( [parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter the name of the cluster to upgrade.")] [string]$ClusterName ) #################################################################################### # # # Functions # # # #################################################################################### function Get-ScriptDirectory { $Invocation = (Get-Variable MyInvocation -Scope 1).Value Split-Path $Invocation.MyCommand.Path } function Log { param( [Parameter(Mandatory=$true)][String]$Message, [Parameter(Mandatory=$true)][ValidateSet("Info","Debug","Warn","Error")][String]$Type, [Parameter(Mandatory=$true)][ValidateSet("Console","LogFile","Both")][String]$OutputMode ) $dateTimeString = Get-Date -Format "yyyy-MM-dd HH:mm:sszz" $output = ($dateTimeString + " " + $type.ToUpper() + " " + $message) if ($outputMode -eq "Console" -OR $outputMode -eq "Both") { Write-Host $output } if ($outputMode -eq "LogFile" -OR $outputMode -eq "Both") { try { Add-Content $logFile -Value $output -ErrorAction Stop } catch { Log ("Failed to write to log file: """ + $logFile + """.") -OutputMode Console -Type Error Log ("[" + $_.Exception.GetType().FullName + "] " + $_.Exception.Message) -OutputMode Console -Type Error } } } #################################################################################### # # # Variables # # # #################################################################################### # Logfile Set-Variable logFile ((Get-ScriptDirectory) + "\Upgrade-Cluster-65.log") -Option Constant -ErrorAction SilentlyContinue $vCenter = "vCenter" $vib = "cisco-vem-v320-esx" $zip = "VMware-VMvisor-Installer-6.5.0.update03-15256549.x86_64-DellEMC_Customized-A05.zip" $profile = "DellEMC-ESXi-6.5U3-15256549-A05" #################################################################################### # # # Main Code # # # #################################################################################### Log -Message "Script started" -Type Info -OutputMode LogFile # Connect vCenter Server Connect-VIserver -Server $vCenter | Out-Null Log -Message "Connected to vCenter Server" -Type Info -OutputMode Both # Get cluster object $cluster = Get-Cluster $clusterName # Get two datastores with most free space Log -Message "Selecting datastores" -Type Info -OutputMode Both $datastore1 = ($cluster | Get-Datastore | Where-Object {$_.Name.Substring(8,3) -eq "DC1"} | Sort-Object FreeSpaceGB -Descending)[0] $ds1url = ($datastore1.ExtensionData.Info.Url).Trim("ds:/") $datastore2 = ($cluster | Get-Datastore | Where-Object {$_.Name.Substring(8,3) -eq "DC2"} | Sort-Object FreeSpaceGB -Descending)[0] $ds2url = ($datastore2.ExtensionData.Info.Url).Trim("ds:/") $datastore1 | New-DatastoreDrive -Name DC1 $datastore2 | New-DatastoreDrive -Name DC2 # Copy the offline ZIP file to the datastores Log -Message "Copying offline ZIP file to $($datastore1.Name)" -Type Info -OutputMode Both Copy-DatastoreItem -Item D:\Scripts\VMware\$zip -Destination DC1:\ Log -Message "Copying offline ZIP file to $($datastore2.Name)" -Type Info -OutputMode Both Copy-DatastoreItem -Item D:\Scripts\VMware\$zip -Destination DC2:\ # Create the depot variables $depot1 = "/$ds1url/$zip" $depot2 = "/$ds2url/$zip" # Disconnect all ISOs that may be attached to VMs and set the CD Drive state to "disconnected" Log -Message "Disconnecting CD drives" -Type Info -OutputMode Both $cluster | Get-VM | Get-CDDrive | Where-Object {($_.IsoPath -ne $null) -or ($_.ConnectionState.Connected -eq "true")} | Set-CDDrive -NoMedia -Connected:$false -Confirm:$false # Upgrade hosts Log -Message "Beginning upgrade of cluster" -Type Info -OutputMode Both foreach ($h in $cluster | Get-VMHost | Where {$_.Version -ne "6.5.0"} | Sort-Object Name) { # Enter maintenance mode Log -Message "Host $($h.Name) is entering Maintenance Mode" -Type Info -OutputMode Both Set-VMHost -VMHost $h -State Maintenance | Out-Null # Wait for the host to enter Maintenance Mode do {Start-Sleep -Seconds 5} while ((Get-VMHost -Name $h.Name).ConnectionState -ne 'Maintenance') # Start removing the VIB Log -Message "Removing VIB from host" -Type Info -OutputMode Both $esxcli = Get-EsxCli -VMHost $h -V2 $param = $esxcli.software.vib.remove.CreateArgs() $param.dryrun = $false $param.vibname = $vib $param.force = $false $esxcli.software.vib.remove.Invoke($param) | Out-Null # Update the host Log -Message "Upgrading host $($h.Name)" -Type Info -OutputMode Both $param = $esxcli.software.profile.update.CreateArgs() $param.dryrun = $false if ($h.Name.Substring(4,1) -eq "0") {$param.depot = $depot1} if ($h.Name.Substring(4,1) -eq "1") {$param.depot = $depot2} $param.profile = $profile $esxcli.software.profile.update.Invoke($param) | Out-Null # Reboot host and wait for it to come back Log -Message "Host $($h.Name) is rebooting" -Type Info -OutputMode Both Restart-VMHost -VMHost $h -Confirm:$false | Out-Null # Wait for the host to enter NotConnected state do {Start-Sleep -Seconds 5} while ((Get-VMHost -Name $h.Name).ConnectionState -ne 'NotResponding') # Wait for the host to get back do {Start-Sleep -Seconds 5} while ((Get-VMHost -Name $h.Name).ConnectionState -ne 'Maintenance') # Exit maintenance mode Log -Message "Host $($h.Name) is exiting Maintenance Mode" -Type Info -OutputMode Both Set-VMHost -VMHost $h -State Connected | Out-Null # Wait for the host to leave Maintenance Mode do {Start-Sleep -Seconds 5} while ((Get-VMHost -Name $h.Name).ConnectionState -ne 'Connected') # Next host... } # Delete offline ZIP files from datastores Log -Message "Removing offline ZIP file from $($datastoreams.Name)" -Type Info -OutputMode Both Get-ChildItem -Path "DC1:/$zip" | Remove-Item -Confirm:$false Log -Message "Removing offline ZIP file from $($datastorewsn.Name)" -Type Info -OutputMode Both Get-ChildItem -Path "DC2:/$zip" | Remove-Item -Confirm:$false # Remove PS drives Remove-PSDrive -Name DC1 -Confirm:$false Remove-PSDrive -Name DC2 -Confirm:$false # Disconnect from vCenter Servers Disconnect-VIServer -Server $vCenter -Confirm:$false Log -Message "Disconnected from vCenter" -Type Info -OutputMode Both Log -Message "Script Ended" -Type Info -OutputMode LogFile
Optional Paste Settings
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
bank.py
Python | 22 min ago
My Log File
HTML 5 | 51 min ago
application.yml
C# | 1 hour ago
HelperBot
Lua | 2 hours ago
Untitled
Bash | 2 hours ago
ffxi_xinput.ahk
Autohotkey | 2 hours ago
BaRoN klipper conf...
INI file | 3 hours ago
infini stam
Lua | 3 hours ago
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!