VMware Cloud Community
bobcampling
Contributor
Contributor
Jump to solution

Disconnecting iSCSI target via script?

Anyone know a way to script (or command line) a disconnect (remove) of a iSCSI target?

0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

Here's how to turn that into a function:

function RemoveIScsiTarget ($vmHostList, $address, $port) {
        foreach ($vmHost in $vmHostList) {
		$storageSystem =  $vmHost | Get-VMHostStorage | Get-View
		$storageSystem.UpdateViewData("storageDeviceInfo.hostBusAdapter")
		$iScsiDevice = "vmhba33"
		$iScsi = $storageSystem.StorageDeviceInfo.HostBusAdapter | where { $_.GetType().Name -eq "HostInternetScsiHba" }
		$sendTarget = $iScsi | foreach { $_.ConfiguredSendTarget } | where { $_.Address -like $address -and $_.Port -like $port }
		$sendTarget = $iScsi | foreach { $_.ConfiguredStaticTarget  } | where { $_.Address -like $address -and $_.Port -like $port }
		
		if ($sendTargets -ne $null -and $sendTargets.Count -gt 0) {
			$storageSystem.RemoveInternetScsiSendTargets($iScsiDevice, $sendTarget)
		}
		if ($staticTargets -ne $null -and $staticTargets.Count -gt 0) {
			$storageSystem.RemoveInternetScsiStaticTargets($iScsiDevice, $staticTarget)
		}
	}
}

And here's an example how to remove iScsi targets from all hosts in your VC:

RemoveIScsiTarget (Get-VMHost) "10.10.10.*" "*"

View solution in original post

0 Kudos
2 Replies
admin
Immortal
Immortal
Jump to solution

Hi,

Currently there isn't a cmdlet that removes iScsi targets, but here's how to do this:

># Retrieve your host (you can further modify the code to work with multiple hosts, not just one)

>$vmHost = Get-VMHost 10.20.30.40

>

>\# The address and port of the target you want to remove

>$address = "10.10.10.10"

>$port = 3260

>\# The iScsi device from which you want to remove the targets

>$iScsiDevice = "vmhba33"

>\# Retrieve the information about the host's bus adapters

>$storageSystem = $vmHost | Get-VMHostStorage | Get-View

>$storageSystem.UpdateViewData("storageDeviceInfo.hostBusAdapter")

>\# Retrieve the actual iScsi device

>$iScsi = $storageSystem.StorageDeviceInfo.HostBusAdapter | where { $_.Device -eq $iScsiDevice }

>\# Collect the send and static targets to remove

>$sendTarget = $iScsi.ConfiguredSendTarget | where { $_.Address -eq $address -and $_.Port -eq $port }

>$staticTarget = $iScsi.ConfiguredStaticTarget | where { $_.Address -eq $address -and $_.Port -eq $port }

>\# Remove the send and static targets

>if ($sendTargets -ne $null -and $sendTargets.Count -gt 0) {

>$storageSystem.RemoveInternetScsiSendTargets($iScsiDevice, $sendTarget)

>}

>if ($staticTargets -ne $null -and $staticTargets.Count -gt 0) {

>$storageSystem.RemoveInternetScsiStaticTargets($iScsiDevice, $staticTarget)

>}

Hope this helps.

Regards,

Dimitar

admin
Immortal
Immortal
Jump to solution

Here's how to turn that into a function:

function RemoveIScsiTarget ($vmHostList, $address, $port) {
        foreach ($vmHost in $vmHostList) {
		$storageSystem =  $vmHost | Get-VMHostStorage | Get-View
		$storageSystem.UpdateViewData("storageDeviceInfo.hostBusAdapter")
		$iScsiDevice = "vmhba33"
		$iScsi = $storageSystem.StorageDeviceInfo.HostBusAdapter | where { $_.GetType().Name -eq "HostInternetScsiHba" }
		$sendTarget = $iScsi | foreach { $_.ConfiguredSendTarget } | where { $_.Address -like $address -and $_.Port -like $port }
		$sendTarget = $iScsi | foreach { $_.ConfiguredStaticTarget  } | where { $_.Address -like $address -and $_.Port -like $port }
		
		if ($sendTargets -ne $null -and $sendTargets.Count -gt 0) {
			$storageSystem.RemoveInternetScsiSendTargets($iScsiDevice, $sendTarget)
		}
		if ($staticTargets -ne $null -and $staticTargets.Count -gt 0) {
			$storageSystem.RemoveInternetScsiStaticTargets($iScsiDevice, $staticTarget)
		}
	}
}

And here's an example how to remove iScsi targets from all hosts in your VC:

RemoveIScsiTarget (Get-VMHost) "10.10.10.*" "*"

0 Kudos