Advertisement
Merzavets

Add iSCSI target on vmhosts

Jan 23rd, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Borrowed from http://www.vhersey.com/2013/12/enable-software-iscsi-and-add-sendtargets-with-powercli/
  2. #FQDNs or IP addresses of ESXi Hosts to Configure
  3. #Enclose each host in quotes and separate with a comma.
  4. #Example: $ESXiHosts = "192.168.1.1","192.168.1.2"
  5. $ESXiHosts = "esx0", "esx3", "esx4", "esx5", "esx6", "esx7", "esx8", "esx10"
  6.  
  7. # Prompt for ESXi Root Credentials
  8. # $esxcred = Get-Credential
  9.  
  10. # Connect to each host defined in $ESXiHosts
  11. # Connect-viserver -Server $ESXiHosts -Credential $ESXiCreds
  12.  
  13. # Set $targets to the SendTargets you want to add. Enclose each target in quotes and separate with a comma.
  14. # Example: $targets = "192.168.151.10", "192.168.151.11", "192.168.151.12", "192.168.151.13"
  15. $targets = "belomor"
  16.  
  17. foreach ($esx in $ESXiHosts) {
  18.  
  19.   # Enable Software iSCSI Adapter on each host
  20.   Write-Host "Enabling Software iSCSI Adapter on $esx ..."
  21.   Get-VMHostStorage -VMHost $esx | Set-VMHostStorage -SoftwareIScsiEnabled $True
  22.  
  23.   # Just a sleep to wait for the adapter to load
  24.   $slpsec = 19
  25.   Write-Host "Sleeping for $slpsec Seconds..." -ForegroundColor Green
  26.   Start-Sleep -Seconds $slpsec
  27.   Write-Host "OK Here we go..." -ForegroundColor Green
  28.   Write-Host "Adding iSCSI SendTargets..." -ForegroundColor Green
  29.  
  30.   $hba = get-vmhost $esx | Get-VMHostHba -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}
  31.  
  32.   foreach ($target in $targets) {
  33.  
  34.      # Check to see if the SendTarget exist, if not add it
  35.      if (Get-IScsiHbaTarget -IScsiHba $hba -Type Send | Where {$_.Address -cmatch $target}) {
  36.         Write-Host "The target $target does exist on $esx" -ForegroundColor Green
  37.      }
  38.      else {
  39.         Write-Host "The target $target doesn't exist on $esx" -ForegroundColor Red
  40.         Write-Host "Creating $target on $esx ..." -ForegroundColor Yellow
  41.         New-IScsiHbaTarget -IScsiHba $hba -Address $target        
  42.      }
  43.  
  44.   }
  45.  
  46. }
  47. # Write "`n All Done."
  48.  
  49.  Write "`n Done - Disconnecting from $ESXiHosts"
  50. Disconnect-VIServer -Server * -Force -Confirm:$false
  51.  
  52. Write-Host "Done! Now go manually add the iSCSI vmk bindings to the Software iSCSI Adapter and Rescan." -ForegroundColor Green
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement