Advertisement
Guest User

Untitled

a guest
Aug 10th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.83 KB | None | 0 0
  1. # Set Disk Storage Runbook
  2. # Version 0.8
  3.  
  4. workflow Set-Disk-Iops
  5. {
  6. param
  7. (
  8. [Parameter(Mandatory=$true)]
  9. [String] $Name,
  10.  
  11. [Parameter(Mandatory=$true)]
  12. [String] $Operation,
  13.  
  14. [Parameter(Mandatory=$true)]
  15. [String] $VMMJOBID,
  16.  
  17. [Parameter(Mandatory=$true)]
  18. [object] $PARAMS,
  19.  
  20. [Parameter(Mandatory=$true)]
  21. [object] $RESOURCEOBJECT
  22. )
  23.  
  24. # Connection to access VMM server.
  25. $VmmConnection = Get-AutomationConnection -Name 'VmmConnection'
  26. $VmmServerName = $VmmConnection.ComputerName
  27.  
  28. $SecurePassword = ConvertTo-SecureString -AsPlainText -String $VmmConnection.Password -Force
  29. $VmmCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $VmmConnection.Username, $SecurePassword
  30.  
  31. # PARAMS to vars USING (Disk Create/Delete)
  32. if ($name -eq "VMM.VirtualDiskDrive") {
  33. $DiskIDTemplate = $PARAMS.VirtualHardDiskId #VHDX Template ID
  34. $DiskName = $PARAMS.FileName #New Virtual Disk Name
  35. $VMID = $PARAMS.VMId # Virtual Machine ID
  36. }
  37.  
  38. # PARAMS to vars USING (VM Create/Delete)
  39. if ($name -eq "VMM.VirtualMachine") {
  40. if ($Operation -eq "Create") {
  41. $DiskIDTemplate = "System" #VHDX ROOT Use only Standard storage
  42. $VMID = $RESOURCEOBJECT.Id # Virtual Machine ID
  43. } else { #Delete VM
  44. $VMID = $PARAMS.Id # Virtual Machine ID
  45. }
  46. write-output vmID_$VMID
  47. }
  48.  
  49. # Connection to access MsSQL server.
  50. $MsSQLCred = Get-AutomationPSCredential -Name 'MsSQL-BillingDB'
  51. [string] $MsSQLLogin = $MsSQLCred.Username
  52. $MsSQLPassword = $MsSQLCred.Password
  53. [string] $MsSQLDatabase = Get-AutomationVariable -Name 'MsSQL-Billing-Database'
  54. [string] $MsSQLServer = Get-AutomationVariable -Name 'MsSQL-Billing-Server'
  55.  
  56. #Get iops Defaults
  57. [string] $iops = Get-AutomationVariable -Name 'iops-defaults'
  58.  
  59. inlinescript {
  60.  
  61. Write-output "Start Inline"
  62.  
  63. # Import VMM module.
  64. Import-Module virtualmachinemanager
  65.  
  66. # Connect to VMM server.
  67. Get-SCVMMServer -ComputerName $Using:VmmServerName
  68.  
  69. ### VARS DB Settings
  70. $SQLserver = $USING:MsSQLServer
  71. $SQLDatabase = $USING:MsSQLDatabase
  72. $SQLuser = $USING:MsSQLLogin
  73. $SQLSecurePassword = $USING:MsSQLPassword
  74. # We need unsecure password to connect DB
  75. $SQLBSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SQLSecurePassword)
  76. $SQLUnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($SQLBSTR)
  77.  
  78. ### MsSQL CONNECTION
  79. $Connection = New-Object System.Data.SQLClient.SQLConnection
  80. $Connection.ConnectionString = "Server = '$SQLServer';database='$SQLDatabase'; User ID = '$SQLuser'; Password = '$SQLUnsecurePassword';trusted_connection=true; Integrated Security=false"
  81. $Connection.Open()
  82. $Command = New-Object System.Data.SQLClient.SQLCommand
  83. $Command.Connection = $Connection
  84. ###
  85.  
  86. $job = Get-SCJob -ID $USING:vmmjobid
  87.  
  88. ## Check Job Status
  89.  
  90. $JobStatus=$Job.Status
  91.  
  92. #Wait Until Task Completed
  93. while ($JobStatus -eq "Running") {
  94. write-output "Start Sleep"
  95. start-sleep 30
  96. $JobStatus=$Job.Status
  97. }
  98. #Test Job Result
  99. if ($JobStatus -eq "Failed") {
  100. write-output "Job Failed!"
  101. write-output JOBid:$job.ID
  102. break
  103. }
  104.  
  105.  
  106.  
  107. function CheckSQLRecord() {
  108. ## Search Disk if exists in DB
  109. $query = "SELECT * FROM Disks WHERE DiskID like '"+$DiskID+"'"
  110. $Command.CommandText = $query
  111. $reader = $Command.ExecuteReader()
  112. $Test_SQL = $reader.Read()
  113. $reader.Close()
  114. return $Test_SQL
  115. ###
  116. }
  117.  
  118. ## Add Disk to Table SQL
  119. if ($USING:Operation -eq "Create") { #Create Disk or VM
  120.  
  121. write-output "Start Create Disk jobs"
  122.  
  123. ### iops limit (standard:fast:ultra)
  124. $iolimit=$USING:iops
  125. $Qos_STD = $iolimit.Split(":")[0]
  126. $Qos_Fast = $iolimit.Split(":")[1]
  127. $Qos_Ultra = $iolimit.Split(":")[2]
  128. ###
  129.  
  130. #Universal vars
  131. $vmID = $USING:VMID #For vmID and VmName
  132. $VirtualMachine = Get-SCVirtualMachine -id $vmID
  133. $VmName = $VirtualMachine.Name
  134. [string]$Subscription = $VirtualMachine.UserRoleID
  135.  
  136. if ($Using:Name -eq "VMM.VirtualDiskDrive") { #Vars To Create Disk
  137. #New Disk Name
  138. write-output "Create Disk Vars"
  139. [string]$DiskName = $USING:DiskName
  140. #Get New Disk Storage Type
  141. [string]$StorageType = (Get-SCVirtualHardDisk -ID $USING:DiskIDTemplate).Tag #Standard,fast,ultra
  142. $VDisk = Get-SCVirtualHardDisk -vm $VirtualMachine | ? name -eq $DiskName
  143. } else { #Vars To Create VM
  144. write-output "Create VM Vars"
  145. $VDisk = $VirtualMachine | Get-SCVirtualHardDisk
  146. write-output ($Vdisk).Count
  147. [string]$DiskName = $VDisk.Name+"(root)"
  148. [string]$StorageType = $USING:DiskIDTemplate
  149. }
  150.  
  151. $DiskID = ($VDisk.ID).ToString()
  152. write-output vmID:$vmID, DiskID:$DiskID
  153. $test_sql = CheckSQLRecord
  154.  
  155. #Write-Output $test_sql
  156.  
  157. ### If Disk not found IN DB
  158. if ($test_sql -eq $false) {
  159.  
  160. #Job parsing
  161. $Name = $Job.Name
  162. $Owner = $Job.Owner
  163. $Time = $Job.EndTime.ToString()
  164. $JobID = ($Job.ID).ToString()
  165.  
  166. write-output "IN CREATE STRING"
  167.  
  168. $Command.CommandText = "INSERT INTO Disks (DiskID, DiskName, VMID, VmName, Owner, StorageType, jobid, Date, Subscription) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')" -f $DiskID, $DiskName, $VmID, $VmName, $Job.Owner, $StorageType, $JobID, $Time, $Subscription
  169. $Command.ExecuteNonQuery() | out-null
  170. }
  171.  
  172. #write-output $DiskID, $DiskName, $vmID, $vmName, $Job.Owner, $StorageType, $JobID, $Time, $vhdx_template
  173.  
  174. #ADD QoS
  175.  
  176. $Location = $VDisk.Location
  177.  
  178. $DiskHost = $VDisk.HostName
  179.  
  180. #Start Work (for example, set disk iops)
  181. write-output "Start setting disk iops"
  182.  
  183. if ($StorageType -eq "Standard" -OR $StorageType -eq "System") { [int]$iops = $Qos_STD }
  184. if ($StorageType -eq "Fast") { [int]$iops = $Qos_Fast }
  185. if ($StorageType -eq "Ultra") { [int]$iops = $Qos_Ultra }
  186.  
  187. ###What We Will Do With Disk?
  188. #Set MaxIOPS Limit:
  189. Invoke-Command -ScriptBlock {Get-VMHardDiskDrive -VMName $Args[0] | ? Path -eq $Args[1] | Set-VMHardDiskDrive -MaximumIOPS $Args[2]} -ComputerName $DiskHost -Credential $USING:VmmCredential -ArgumentList $VMname,$Location,$iops
  190. ############
  191. # Other Jobs (May be Disk Migration to Other Storage)
  192. ############
  193. }
  194.  
  195. if ($USING:Operation -eq "Delete" -AND $Using:Name -eq "VMM.VirtualDiskDrive") { #Delete Disk
  196. ## Delete DISK From Database If Disk was found IN DB
  197. write-output "Start disk removing from DB"
  198. #Get Disk ID
  199. $jobfull = Get-SCJob -job $job -full
  200. $job_auditrecords = $jobfull.AuditRecords
  201. $RemovedDisk = $job_auditrecords.ObjectData | ? ObjectType -eq VirtualHardDisk
  202. $DiskID = ($RemovedDisk.ID | Select -First 1).ToString()
  203. ##
  204. $test_sql = CheckSQLRecord
  205. Write-Output $test_sql
  206.  
  207. if ($test_sql -eq $true) {
  208. #write-output REMOVE $DiskID
  209. $Command.CommandText = "DELETE FROM Disks WHERE DiskID like '$DiskID'"
  210. $Command.ExecuteNonQuery() | out-null
  211. }
  212. }
  213.  
  214. if ($USING:Operation -eq "Delete" -AND $Using:Name -eq "VMM.VirtualMachine") { #Delete VM
  215. ## Delete All VM records From Database
  216. write-output "Start VM removing from DB"
  217. #Write to Event Log
  218. #Get Disk ID
  219. #$jobfull = Get-SCJob -job $job -full
  220. #$job_auditrecords = $jobfull.AuditRecords
  221. ##
  222. #write-output REMOVE VM $DiskID
  223. $vmID = $USING:VMID
  224. $Command.CommandText = "DELETE FROM Disks WHERE VMID like '$vmID'"
  225. $Command.ExecuteNonQuery() | out-null
  226.  
  227. }
  228.  
  229. #CLOSE DB Connection
  230. $Connection.Close()
  231.  
  232. } -PSComputerName $VmmServerName -PSCredential $VmmCredential
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement