Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#######Script Purpose#######
  2. # Gathers input from running line
  3. # Creates 2 datasores
  4. #   - Based of a replay
  5. #   - One for the vm datastore
  6. #
  7. # - Scans the esxi host and waits until the volumes are mapped to esxi
  8. # - Maps a datastore
  9. # - Creates a vm with a raw device mapping
  10. # - Waits for the vm to start
  11. # - Returns server ip address when the server is up and running
  12. #createvm.ps1 -esxiHostname "172.16.1.10" -esxiUsername "PSScript" -esxiPassword "icecream" -storageCenterHostname "172.16.1.100" -storageCenterUsername "PSScript" -storageCenterPassword "icecream" -storageCenterEsxiServerWorldWideNames "iqn.1998-01.com.vmware:co-vm1-6d1c98d8" -storageCenterGoldImageSerialNumber "00001f49-00000136" -newServerName "TEST_RENEE" -newServerHdSize "80g" -newServerDatastoreSize "10g" -newServerMemory "1024" -newServerCpu "1" -newServerOsConstant "windows7Server64Guest"
  13. #############################
  14. #>
  15.  
  16. <#******************************
  17.  
  18. #  For Debug Only
  19. $esxiHostname = "172.16.1.10"
  20. $esxiUsername = "PSScript"
  21. $esxiPassword = "icecream"
  22.  
  23. $storageCenterHostname = "172.16.1.100"
  24. $storageCenterUsername = "PSScript"
  25. $storageCenterPassword = "icecream"
  26.  
  27. $storageCenterEsxiServerWorldWideNames = "iqn.1998-01.com.vmware:co-vm1-6d1c98d8"
  28. $storageCenterGoldImageSerialNumber = "00001f49-00000136"
  29.  
  30. $newServerName = "TEST_RENEE"
  31. $newServerHdSize = "80g"
  32. $newServerDatastoreSize = "10g"
  33. $newServerMemory = 1024
  34. $newServerCpus = 1
  35. $newServerOsConstant = "windows7Server64Guest"
  36.  
  37. #******************************
  38. #>
  39.  
  40. # Param statement to set the variables from the script run-line when called
  41. param($esxiHostname, $esxiUsername, $esxiPassword, $storageCenterHostname, $storageCenterUsername, $storageCenterPassword, $storageCenterServer, $storageCenterEsxiServerWorldWideNames, $storageCenterGoldImageSerialNumber, $newServerName, $newServerHdSize, $newServerDatastoreSize, $newServerMemory, $newServerCpus, $newServerOsConstant)
  42.  
  43. # These variables need to stay in this script (They are not for testing only)
  44. $rescanHBsRetryLimit = 4   # This value controls how many times ESXi will loop to rescan all HBA's when a new volume is mapped. (If you are getting errors when the HBA is not found, increase this value.)
  45. $serverBootWaitLimit = 15  # This value controls how long the server will wait for a VM to boot (May need to be increased depending on server performance [Slower = Higher Value])
  46. $WarningPreference = "SilentlyContinue"
  47. $ErrorActionPreference = "SilentlyContinue"
  48.  
  49. function Main
  50. {
  51.     LoadPowercli
  52.     LoginESXi
  53.  
  54.     LoadCompellentStorageCenter
  55.     LoginToCompellentStorageCenter
  56.    
  57.     #duplicateServerCheck
  58.    
  59.     CreateVolumes
  60.    
  61. }
  62.  
  63. # Load the VMWare vSphere PowerCLI Snapin
  64. function LoadPowercli
  65. {
  66.     if (!(Get-PSSnapin VMware.VimAutomation.Core -ea SilentlyContinue)) # Checks to see if the snapin is already loaded
  67.     {
  68.       Add-PSSnapin VMware.VimAutomation.Core # Loads the VMWare vSphere PowerCLI Snapin
  69.     }
  70. }
  71.  
  72. # Connect to the ESXi Server
  73. function LoginESXi
  74. {
  75.     Connect-VIServer $esxiHostname -User $esxiUsername -Password $esxiPassword | Out-Null
  76. }
  77.  
  78. # Load the Compellent Storage Center Snapin
  79. function LoadCompellentStorageCenter
  80. {
  81.     if (!(Get-PSSnapin Compellent.StorageCenter.PSSnapin -ea SilentlyContinue)) # Checks to see if the snapin is already loaded
  82.     {
  83.         Add-PSSnapin -name Compellent.StorageCenter.PSSnapin # Loads the Compellent Storage Center PowerShell Snapin
  84.     }
  85. }
  86.  
  87. # Connect to the Compellent Storeage Center - SAN
  88. function LoginToCompellentStorageCenter
  89. {
  90.     $storageCenterPassword = ConvertTo-SecureString $storageCenterPassword -AsPlainText -Force
  91.     Get-SCConnection -HostName $storageCenterHostname -User $storageCenterUsername -Password $storageCenterPassword -Save $storageCenterHostname -Default | Out-Null
  92. }
  93.  
  94. function duplicateServerCheck
  95. {
  96.     $vm = Get-VM -Name $newServerName
  97.     if($vm -ne $null)
  98.     {
  99.         EndScript "Server with that name already created: please rename server and try again"
  100.     }
  101. }
  102.  
  103. function CreateVolumes
  104. {
  105.     "Creating Volumes" # Status return line to the GUI program.
  106.    
  107.     # Creates a folder to store the new volumes in. The name of the folder is equal to the new VM Server name.
  108.     $newFolder = New-SCVolumeFolder -Name ($newServerName)
  109.    
  110.     $newVolumeDatastore = New-SCVolume -Name ($newServerName + " Datastore") -ParentSCVolumeFolder $newFolder -Size ($newServerDatastoreSize)
  111.        
  112.     New-SCVolumeMap -SCVolume $newVolumeDatastore -SCServer (Get-SCServer -WorldWideName $storageCenterEsxiServerWorldWideNames) | Out-Null
  113.    
  114.     # Get the Gold-Volume Object
  115.     $goldVolume = Get-SCVolume -SerialNumber $storageCenterGoldImageSerialNumber
  116.  
  117.     # Get the replay from the Gold Volume Object
  118.     $goldReplay = Get-SCReplay -SourceSCVolume $goldVolume -State "Frozen"
  119.     $goldReplay = $goldReplay[-1]
  120.    
  121.     # Create a New Volume on the SAN from Gold Volume replay name is based off the new server name and the currnet time
  122.     $newReplayVolume = New-SCVolume -Name ($newServerName + " Replay") -SourceSCReplay $goldReplay -ParentSCVolumeFolder $newFolder
  123.  
  124.     # Make the volume larger if needed
  125.     Expand-SCVolume -SCVolume $newReplayVolume -NewSize $newServerHdSize | Out-Null
  126.        
  127.     # Map the New Volume (Replay) up to the ESXi Server
  128.     New-SCVolumeMap -SCVolume $newReplayVolume -SCServer (Get-SCServer -WorldWideName $storageCenterEsxiServerWorldWideNames) | Out-Null
  129.    
  130.     "Created " + $newVolumeDatastore.name + " and " + $newReplayVolume.name # Satus return line to GUI Program.
  131.    
  132.     RescanHBAs $newVolumeDatastore.DeviceId $newReplayVolume.DeviceId
  133.     }
  134.  
  135. Function RescanHBAs # Rescans all HBA's on the ESXi Server - To search for the new Volume & Datastore Mappings
  136. {
  137.     param($datastoreSN,$volumeSN) # Takes in the Datastore (DS) serial number and the Volume (VOL) serial number.
  138.    
  139.     $datastoreCanonicalName = "naa." + $datastoreSN # Transform the DS serial number to the DS Canonical name.
  140.     $volumeCanonicalName = "naa." + $volumeSN       # Transform the VOL serial number to the VOL Canonical name.
  141.    
  142.     "Resaning ESXi HBAs" # Status return line to the GUI program.
  143.     # ----------------------------- Should there be a sleep here of like 20 seconds???
  144.     Get-VMHostStorage -RescanAllHba -Refresh | Out-Null  # Preforms the inital rescan of all HBA's
  145.    
  146.     $datastoreScusiLun = Get-ScsiLun -CanonicalName $datastoreCanonicalName
  147.     $volumeScusiLun = Get-ScsiLun -CanonicalName $volumeCanonicalName
  148.    
  149.     for( $i=0; $i -le $rescanHBsRetryLimit; $i++) # Loop to repeatly rescan the HBA's until the Datastores show up as mapped to the ESXi server.
  150.     {                                             # The loop will Break when it hits the rescanHBsRetryLimit set
  151.         "data: " + $datastoreScusiLun
  152.         "vol: " + $volumeScusiLun
  153.         if($datastoreScusiLun -eq $null -or $volumeScusiLun -eq $null)
  154.         {
  155.             "Waiting for ESXi to discover volumes" # Status return line to the GUI Program.
  156.             sleep -Seconds 20
  157.             Get-VMHostStorage -RescanAllHba -Refresh -RescanVmfs | Out-Null
  158.             $datastoreScusiLun = Get-ScsiLun -CanonicalName $datastoreCanonicalName
  159.             $volumeScusiLun = Get-ScsiLun -CanonicalName $volumeCanonicalName
  160.         }
  161.     }
  162.     if($datastoreScusiLun -eq $null -or $volumeScusiLun -eq $null) # Check to see that the RescanAllHBA's was successfull (If not the script will end and the error will be sent to the GUI User)
  163.     {
  164.         "ERROR: Can not find new drives" # Retun Error to the GUI Program.
  165.         EndScript # Terminates Script
  166.         # break
  167.     }
  168.     "Drives maped to ESXI" # Status return line to the GUI Program.
  169.    
  170.     CreateVirtualServer $datastoreCanonicalName $volumeCanonicalName # Move on to the next function (Creating the VM in EXSi)
  171. }
  172.  
  173. Function CreateVirtualServer # Function to create a new VM Server in ESXi (Takes in DatastoreCanoicalName and VolumeCanonicalName)
  174. {
  175.     param($datastoreCanonicalName,$volumeCanonicalName)
  176.    
  177.     "Creating VM" # Status return line to the GUI program.
  178.    
  179.     # Create VM Datstore
  180.     $newDatastore = New-Datastore -vmfs -VMHost (get-vmhost -Name $esxiHostname) -Path $datastoreCanonicalName -Name ($newServerName + " - Datastore")
  181.    
  182.     # Create VM
  183.     $newVM = New-VM -Name $newServerName -version v7 -VMHost $esxiHostname -Datastore $newDatastore -MemoryMB $newServerMemory -NumCpu $newServerCpus -GuestId $newServerOsConstant
  184.    
  185.     # Remove original Hard Drive (Need to remove and create a new HD, to be able to mount the RawPhysical Drive)
  186.     Remove-HardDisk -HardDisk (Get-HardDisk -vm $newVM) -Confirm:$false | Out-Null
  187.    
  188.     # Get the device
  189.     $deviceName = Get-ScsiLun -CanonicalName $volumeCanonicalName
  190.    
  191.     # Create/Edit Hard Disk
  192.     New-HardDisk -VM $newVM -DiskType RawPhysical -DeviceName $deviceName.ConsoleDeviceName | Out-Null
  193.    
  194.     # Edit iSCSI
  195.     Get-ScsiController -VM $newVM | Set-ScsiController -BusSharingMode NoSharing | Out-Null
  196.     $scsiController = Get-HardDisk -VM $newVM | Select -First 1 | Get-ScsiController
  197.     Set-ScsiController -ScsiController $scsiController -Type VirtualLsiLogicSAS | Out-Null
  198.    
  199.     "Starting VM" # Status return line to the GUI program.
  200.    
  201.     # Start the new VM Server
  202.     Start-VM -vm $newServerName | Out-Null
  203.    
  204.     WaitForServerToBoot # Move on to the next function.
  205. }
  206.  
  207. function WaitForServerToBoot # This function simply waits for the server to do the initial boot process.
  208. {
  209.     "Waiting For server to boot" # Status return line to the GUI program.
  210.    
  211.     for( $i=0; $i -le $serverBootWaitLimit; $i++)
  212.     {
  213.         $vm = Get-VM -Name $newServerName
  214.        
  215.         "Still waiting for server to boot" # Status return line to the GUI program.
  216.        
  217.         if($vm.Guest.ExtensionData.IpAddress -ne $null) # $vm.Guest.State -eq "Running"
  218.         {
  219.             "IP Address " + $vm.Guest.ExtensionData.IpAddress # Status return line to the GUI program. (IP Address)
  220.             break #$i = 99 ## Breaks out of the loop provided that $serverBootWaitLimit < 99 (I think break can be used here)
  221.         }
  222.         sleep -Seconds 20
  223.     }
  224.     if($vm.Guest.State -eq "NotRunning")
  225.     {
  226.         "VM not running contact your system administrator" # Status return line to the GUI program.
  227.     }
  228.    
  229.     EndScript # Call the fuction to terminat the script
  230. }
  231.  
  232. function EndScript # This function basically ends the execution of the script and terminates the connections.
  233. {
  234.     param($Error)
  235.     Remove-SCConnection -name $storageCenterHostname -Confirm:$false   # Remove the Compellent SC Connection
  236.     Disconnect-VIServer -Confirm:$false  # Remove the vSphere ESXi Server Connection
  237.     $error # Status return line to the GUI program.
  238.  
  239. }
  240.  
  241. Main # Runs the Main Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement