Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#######Script Purpose#######
- # Gathers input from running line
- # Creates 2 datasores
- # - Based of a replay
- # - One for the vm datastore
- #
- # - Scans the esxi host and waits until the volumes are mapped to esxi
- # - Maps a datastore
- # - Creates a vm with a raw device mapping
- # - Waits for the vm to start
- # - Returns server ip address when the server is up and running
- #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"
- #############################
- #>
- <#******************************
- # For Debug Only
- $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
- $newServerCpus = 1
- $newServerOsConstant = "windows7Server64Guest"
- #******************************
- #>
- # Param statement to set the variables from the script run-line when called
- param($esxiHostname, $esxiUsername, $esxiPassword, $storageCenterHostname, $storageCenterUsername, $storageCenterPassword, $storageCenterServer, $storageCenterEsxiServerWorldWideNames, $storageCenterGoldImageSerialNumber, $newServerName, $newServerHdSize, $newServerDatastoreSize, $newServerMemory, $newServerCpus, $newServerOsConstant)
- # These variables need to stay in this script (They are not for testing only)
- $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.)
- $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])
- $WarningPreference = "SilentlyContinue"
- $ErrorActionPreference = "SilentlyContinue"
- function Main
- {
- LoadPowercli
- LoginESXi
- LoadCompellentStorageCenter
- LoginToCompellentStorageCenter
- #duplicateServerCheck
- CreateVolumes
- }
- # Load the VMWare vSphere PowerCLI Snapin
- function LoadPowercli
- {
- if (!(Get-PSSnapin VMware.VimAutomation.Core -ea SilentlyContinue)) # Checks to see if the snapin is already loaded
- {
- Add-PSSnapin VMware.VimAutomation.Core # Loads the VMWare vSphere PowerCLI Snapin
- }
- }
- # Connect to the ESXi Server
- function LoginESXi
- {
- Connect-VIServer $esxiHostname -User $esxiUsername -Password $esxiPassword | Out-Null
- }
- # Load the Compellent Storage Center Snapin
- function LoadCompellentStorageCenter
- {
- if (!(Get-PSSnapin Compellent.StorageCenter.PSSnapin -ea SilentlyContinue)) # Checks to see if the snapin is already loaded
- {
- Add-PSSnapin -name Compellent.StorageCenter.PSSnapin # Loads the Compellent Storage Center PowerShell Snapin
- }
- }
- # Connect to the Compellent Storeage Center - SAN
- function LoginToCompellentStorageCenter
- {
- $storageCenterPassword = ConvertTo-SecureString $storageCenterPassword -AsPlainText -Force
- Get-SCConnection -HostName $storageCenterHostname -User $storageCenterUsername -Password $storageCenterPassword -Save $storageCenterHostname -Default | Out-Null
- }
- function duplicateServerCheck
- {
- $vm = Get-VM -Name $newServerName
- if($vm -ne $null)
- {
- EndScript "Server with that name already created: please rename server and try again"
- }
- }
- function CreateVolumes
- {
- "Creating Volumes" # Status return line to the GUI program.
- # Creates a folder to store the new volumes in. The name of the folder is equal to the new VM Server name.
- $newFolder = New-SCVolumeFolder -Name ($newServerName)
- $newVolumeDatastore = New-SCVolume -Name ($newServerName + " Datastore") -ParentSCVolumeFolder $newFolder -Size ($newServerDatastoreSize)
- New-SCVolumeMap -SCVolume $newVolumeDatastore -SCServer (Get-SCServer -WorldWideName $storageCenterEsxiServerWorldWideNames) | Out-Null
- # Get the Gold-Volume Object
- $goldVolume = Get-SCVolume -SerialNumber $storageCenterGoldImageSerialNumber
- # Get the replay from the Gold Volume Object
- $goldReplay = Get-SCReplay -SourceSCVolume $goldVolume -State "Frozen"
- $goldReplay = $goldReplay[-1]
- # Create a New Volume on the SAN from Gold Volume replay name is based off the new server name and the currnet time
- $newReplayVolume = New-SCVolume -Name ($newServerName + " Replay") -SourceSCReplay $goldReplay -ParentSCVolumeFolder $newFolder
- # Make the volume larger if needed
- Expand-SCVolume -SCVolume $newReplayVolume -NewSize $newServerHdSize | Out-Null
- # Map the New Volume (Replay) up to the ESXi Server
- New-SCVolumeMap -SCVolume $newReplayVolume -SCServer (Get-SCServer -WorldWideName $storageCenterEsxiServerWorldWideNames) | Out-Null
- "Created " + $newVolumeDatastore.name + " and " + $newReplayVolume.name # Satus return line to GUI Program.
- RescanHBAs $newVolumeDatastore.DeviceId $newReplayVolume.DeviceId
- }
- Function RescanHBAs # Rescans all HBA's on the ESXi Server - To search for the new Volume & Datastore Mappings
- {
- param($datastoreSN,$volumeSN) # Takes in the Datastore (DS) serial number and the Volume (VOL) serial number.
- $datastoreCanonicalName = "naa." + $datastoreSN # Transform the DS serial number to the DS Canonical name.
- $volumeCanonicalName = "naa." + $volumeSN # Transform the VOL serial number to the VOL Canonical name.
- "Resaning ESXi HBAs" # Status return line to the GUI program.
- # ----------------------------- Should there be a sleep here of like 20 seconds???
- Get-VMHostStorage -RescanAllHba -Refresh | Out-Null # Preforms the inital rescan of all HBA's
- $datastoreScusiLun = Get-ScsiLun -CanonicalName $datastoreCanonicalName
- $volumeScusiLun = Get-ScsiLun -CanonicalName $volumeCanonicalName
- 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.
- { # The loop will Break when it hits the rescanHBsRetryLimit set
- "data: " + $datastoreScusiLun
- "vol: " + $volumeScusiLun
- if($datastoreScusiLun -eq $null -or $volumeScusiLun -eq $null)
- {
- "Waiting for ESXi to discover volumes" # Status return line to the GUI Program.
- sleep -Seconds 20
- Get-VMHostStorage -RescanAllHba -Refresh -RescanVmfs | Out-Null
- $datastoreScusiLun = Get-ScsiLun -CanonicalName $datastoreCanonicalName
- $volumeScusiLun = Get-ScsiLun -CanonicalName $volumeCanonicalName
- }
- }
- 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)
- {
- "ERROR: Can not find new drives" # Retun Error to the GUI Program.
- EndScript # Terminates Script
- # break
- }
- "Drives maped to ESXI" # Status return line to the GUI Program.
- CreateVirtualServer $datastoreCanonicalName $volumeCanonicalName # Move on to the next function (Creating the VM in EXSi)
- }
- Function CreateVirtualServer # Function to create a new VM Server in ESXi (Takes in DatastoreCanoicalName and VolumeCanonicalName)
- {
- param($datastoreCanonicalName,$volumeCanonicalName)
- "Creating VM" # Status return line to the GUI program.
- # Create VM Datstore
- $newDatastore = New-Datastore -vmfs -VMHost (get-vmhost -Name $esxiHostname) -Path $datastoreCanonicalName -Name ($newServerName + " - Datastore")
- # Create VM
- $newVM = New-VM -Name $newServerName -version v7 -VMHost $esxiHostname -Datastore $newDatastore -MemoryMB $newServerMemory -NumCpu $newServerCpus -GuestId $newServerOsConstant
- # Remove original Hard Drive (Need to remove and create a new HD, to be able to mount the RawPhysical Drive)
- Remove-HardDisk -HardDisk (Get-HardDisk -vm $newVM) -Confirm:$false | Out-Null
- # Get the device
- $deviceName = Get-ScsiLun -CanonicalName $volumeCanonicalName
- # Create/Edit Hard Disk
- New-HardDisk -VM $newVM -DiskType RawPhysical -DeviceName $deviceName.ConsoleDeviceName | Out-Null
- # Edit iSCSI
- Get-ScsiController -VM $newVM | Set-ScsiController -BusSharingMode NoSharing | Out-Null
- $scsiController = Get-HardDisk -VM $newVM | Select -First 1 | Get-ScsiController
- Set-ScsiController -ScsiController $scsiController -Type VirtualLsiLogicSAS | Out-Null
- "Starting VM" # Status return line to the GUI program.
- # Start the new VM Server
- Start-VM -vm $newServerName | Out-Null
- WaitForServerToBoot # Move on to the next function.
- }
- function WaitForServerToBoot # This function simply waits for the server to do the initial boot process.
- {
- "Waiting For server to boot" # Status return line to the GUI program.
- for( $i=0; $i -le $serverBootWaitLimit; $i++)
- {
- $vm = Get-VM -Name $newServerName
- "Still waiting for server to boot" # Status return line to the GUI program.
- if($vm.Guest.ExtensionData.IpAddress -ne $null) # $vm.Guest.State -eq "Running"
- {
- "IP Address " + $vm.Guest.ExtensionData.IpAddress # Status return line to the GUI program. (IP Address)
- break #$i = 99 ## Breaks out of the loop provided that $serverBootWaitLimit < 99 (I think break can be used here)
- }
- sleep -Seconds 20
- }
- if($vm.Guest.State -eq "NotRunning")
- {
- "VM not running contact your system administrator" # Status return line to the GUI program.
- }
- EndScript # Call the fuction to terminat the script
- }
- function EndScript # This function basically ends the execution of the script and terminates the connections.
- {
- param($Error)
- Remove-SCConnection -name $storageCenterHostname -Confirm:$false # Remove the Compellent SC Connection
- Disconnect-VIServer -Confirm:$false # Remove the vSphere ESXi Server Connection
- $error # Status return line to the GUI program.
- }
- Main # Runs the Main Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement