Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions Microsoft.AVS.VMFS/Microsoft.AVS.VMFS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,9 @@ function Sync-ClusterVMHostStorage {
.PARAMETER VMHostName
Name of the VMHost (ESXi server). If not specified, all hosts in the cluster will be updated.

.PARAMETER Force
Force removing iSCSI target without checking if it is in use

.EXAMPLE
Remove-VMHostStaticIScsiTargets -ClusterName "myCluster" -ISCSIAddress "192.168.1.10,192.168.1.11"

Expand All @@ -746,7 +749,7 @@ function Remove-VMHostStaticIScsiTargets {

[Parameter(
Mandatory=$false,
HelpMessage = 'VMHost name')]
HelpMessage = 'VMHost name')]
[String]
$VMHostName,

Expand All @@ -755,16 +758,21 @@ function Remove-VMHostStaticIScsiTargets {
HelpMessage = 'IP Address of static iSCSI target to remove. Multiple addresses can be seperated by ","')]
[ValidateNotNull()]
[String]
$iSCSIAddress
$iSCSIAddress,

[Parameter(
Mandatory = $false,
HelpMessage = 'Force to remove iSCSI target without checking if it is in use')]
[bool]
$Force = $false
)

$Cluster = Get-Cluster -Name $ClusterName -ErrorAction Ignore
if (-not $Cluster) {
throw "Cluster $ClusterName does not exist."
}

$iSCSIAddressList = $iSCSIAddress.Split(",")
$DatastoreDisks = Get-Datastore | Select-Object -ExpandProperty ExtensionData | Select-Object -ExpandProperty Info | Select-Object -ExpandProperty Vmfs | Select-Object -ExpandProperty Extent
$iSCSIAddressList = $iSCSIAddress.Split(",")
$TargetsChanged = $False

$VMHosts = $null
Expand All @@ -783,15 +791,18 @@ function Remove-VMHostStaticIScsiTargets {
foreach ($HBA in $HBAs) {
$DeviceIds = ($HBA | Get-ScsiLun).CanonicalName

# Find if any of the devices is used as backing for a datastore
$IsDeviceInUse = $False
foreach ($DeviceId in $DeviceIds) {
if ($DatastoreDisks.DiskName -contains $DeviceId) {
$IsDeviceInUse = $True
break
if (-not $Force) {
# Find if any of the devices is used as backing for a datastore
$DatastoreDisks = (Get-Datastore).ExtensionData.Info.Vmfs.Extent
foreach ($DeviceId in $DeviceIds) {
if ($DatastoreDisks.DiskName -contains $DeviceId) {
$IsDeviceInUse = $True
break
}
}
}
if ($IsDeviceInUse) {
if ($IsDeviceInUse -and -not $Force) {
Write-Warning "Datastore disk $DeviceId for host $($HBA.VMHost.Name) is in use, skipping iSCSI target removal"
}
else {
Expand Down