I have a below script for disabling specific paths on ESXi servers in the cluster. What I want to do is the script to do is for some reason if it is unable to disable to path is to continue. -ErrorAction SilentlyContinue doesn't seems to be working. If I enter a non-existent path in the host specific input file, the scripts just stop at that junture and doesn't continue with the next host.
#requires -version 4
<#
.SYNOPSIS
Disables Specific LUN paths fed in via Input files per ESXi host
.DESCRIPTION
Searches the vCenter Cluster for specific paths mentioned in per ESXi host input files and disables them.
.PARAMETER <Parameter_Name>
None
.INPUTS Server
Mandatory. The vCenter Server or ESXi Host the script will connect to, in the format of IP address or FQDN.
.INPUTS Paths to be disabled
Mandatory. The text input files prepared in advance per ESXi host in the cluster mentioning the runtime paths to disabled for desired LUNs.
.INPUTS Credentials
Mandatory. The user account credendials used to connect to the vCenter Server of ESXi Host.
.OUTPUTS Log File
The script log file stored in D:\script\Temp\Disable_Lun_Path.log
.NOTES
Version: 1.0
Author: Mahesh Gurav
Creation Date: 14-Feb-2021
Purpose/Change: Initial script development
Version: 2.0
Author: Mahesh Gurav
Creation Date: 15-Feb-2021
Purpose/Change: Added the if then else statement to check if the path is already disabled
.EXAMPLE
Execute the script from the path where it is stored
Disable_LUN_Path.ps1
#>
#---------------------------------------------------------[Script Parameters]------------------------------------------------------
#Param (
#Script parameters go here
#)
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
$ErrorActionPreference = 'SilentlyContinue'
#Import Modules & Snap-ins
Add-PSSnapin VMware.VimAutomation.Core
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Script Version
$sScriptVersion = '2.0'
#Log File Info
$sLogPath = 'd:\script\Temp'
$sLogName = 'Disable_LUN_Path.log'
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
#vCenter and ESXi Cluster Details to run the script against
$VMServer="vcenter01"
$VMCluster="Cluster-01"
#Location where ESXi host specific files containing paths to be disabled are stored
$InputDir="D:\script"
#-----------------------------------------------------------[Functions]------------------------------------------------------------
Function Connect-VMwareServer {
Param ([Parameter(Mandatory=$true)][string]$VMServer)
Begin {
Write-Host "Connecting to VMware environment [$VMServer]..."
}
Process {
Try {
$oCred = Get-Credential -Message 'Enter credentials to connect to vSphere Server or Host'
Connect-VIServer -Server $VMServer -Credential $oCred
}
Catch {
Write-Host -BackgroundColor Red "Error: $($_.Exception)"
Break
}
}
End {
If ($?) {
Write-Host 'Completed Successfully.'
Write-Host ' '
}
}
}
Function Disable-Lun-Path {
# Param ([Parameter(Mandatory=$true)][string]$LunPath)
Begin {
Write-LogInfo -LogPath $sLogFile -Message "Disabling LUN Paths as per the inpu files in $InputDir..."
}
Process {
Try {
$ESXiHosts = Get-Cluster $VMCluster | Get-VMHost
$j=0
ForEach ($ESXiHost in $ESXiHosts) {
$j++
Write-Progress -Activity "Disabling Paths on Host $ESXiHost" -Status ("Host: {0}" -f $ESXiHost.Name) -PercentComplete ($j/$ESXiHosts.count*100) -Id 0
$esxcli = Get-EsxCli -VMhost $ESXiHost -V2
$LunPaths = Get-Content -Path $InputDir\$ESXiHost.txt
$k=0
ForEach ($LunPath in $LunPaths) {
$k++
Write-Progress -Activity "Disabling LUN paths" -Status ("Path: {0}" -f $LunPath) -PercentComplete ($k/$LunPaths.count*100) -Id 1
#Write-Output "Disabling path $lunpath on ESXi Host $esxihost" 3>&1 >> $sLogFile
$arguments = $esxcli.storage.core.path.set.CreateArgs()
$arguments.path = "$LunPath"
$arguments.state = "off"
$esxcli.storage.core.path.set.invoke($arguments) || true
if ($?){
Write-Output "Disabled path $lunpath on ESXi Host $esxihost" >> $sLogFile
}
else {
Write-Output "Error Disabling path $lunpath on ESXi Host $esxihost, Kindly check the path status from vSphere Client" >> $sLogFile
}
# PAUSE for TWO Seconds before disabling next path
Start-Sleep -s 2
}
}
}
Catch {
Write-Host -BackgroundColor Red "Error: $($_.Exception)"
Break
}
}
End {
If ($?) {
Write-Host 'Completed Successfully.'
Write-Host ' '
}
}
}
#>
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Connect-VMwareServer -VMServer $VMServer
Write-Output "Started Processing at $(Get-Date)" >> $sLogFile
Disable-Lun-Path
Write-Output "Finished Processing at $(Get-Date)" >> $sLogFile
Disconnect-VIServer $vcenter -Force -Confirm:$false
Write-Output "All done, Disconnecting vCenter Server $vcenter"