VMware

This Question is Answered

1 "helpful" answer available (6 pts)
2 Replies Last post: Nov 5, 2009 11:49 PM by d_hristov  

Disconnecting iSCSI target via script? posted: Nov 5, 2009 2:37 PM

Click to view bobcampling's profile Novice 6 posts since
Sep 24, 2006

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


Re: Disconnecting iSCSI target via script?

1. Nov 5, 2009 11:34 PM in response to: bobcampling
Click to view d_hristov's profile Enthusiast 16 posts since
Mar 5, 2008
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

Re: Disconnecting iSCSI target via script?

2. Nov 6, 2009 7:41 AM in response to: d_hristov
Click to view d_hristov's profile Enthusiast 16 posts since
Mar 5, 2008
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.*" "*"

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities