Advertisement
Guest User

Untitled

a guest
Feb 26th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ConnectMySQL([string]$user, [string]$pass, [string]$MySQLHost, [string]$database) {
  2.     # Load MySQL .NET Connector Objects
  3.     [void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")
  4.  
  5.     # Open Connection
  6.     $connStr = "server=" + $MySQLHost + ";port=3306;uid=" + $user + ";pwd=" + $pass + ";database="+$database
  7.     try {
  8.         $conn = New-Object MySql.Data.MySqlClient.MySqlConnection
  9.         $conn.ConnectionString = $connStr
  10.         $conn.Open()
  11.     } catch [System.Management.Automation.PSArgumentException] {
  12.         Write-Host "Unable to connect to MySQL server, do you have the MySQL connector installed..?"
  13.         Write-Host $_
  14.         Exit
  15.     } catch {
  16.         Write-Host "Unable to connect to MySQL server..."
  17.         Write-Host $_.Exception.GetType().FullName
  18.         Write-Host $_.Exception.Message
  19.        
  20.     }
  21.     Write-Host "Connected to MySQL database $MySQLHost\$database"
  22.  
  23.     return $conn
  24. }
  25.  
  26. function Execute-MySQLNonQuery($conn, [string]$query) {
  27.   $command = $conn.CreateCommand()                  # Create command object
  28.   $command.CommandText = $query                     # Load query into object
  29.   $RowsInserted = $command.ExecuteNonQuery()        # Execute command
  30.   $command.Dispose()                                # Dispose of command object
  31.   if ($RowsInserted) {
  32.     return $RowInserted
  33.   } else {
  34.     return $false
  35.   }
  36. }
  37.  
  38. function UpdateStatusTask($Connection ,[string] $status){
  39.     $QueryUpdate = "UPDATE portal.task set TaskStatus=$status where TaskName='Hyper-V'"
  40.     Execute-MySQLNonQuery $Connection $QueryUpdate
  41. }
  42.  
  43. function SetVMInfo($Connection){
  44.     $Servers = @('HCI160','HCI163')
  45.  
  46.     # Get VM info
  47.         Get-VM -ComputerName $Servers| ForEach-Object  {
  48.         $VHDSize = (Get-VMHardDiskDrive -ComputerName $_.ComputerName -VMName $_.VMName | Get-VHD -ComputerName $_.ComputerName | Measure-Object Size -Sum).Sum -as [double]
  49.         $VHDFileSize = (Get-VMHardDiskDrive -VMName $_.VMName -ComputerName $_.ComputerName | Get-VHD -ComputerName $_.ComputerName | Measure-Object FileSize -Sum).Sum -as [double]
  50.        
  51.         $QueryInsert = "INSERT INTO portal.vps_hyperv_sync (HyperVType,HyperVName,HyperVRAMUsed,HyperVRAM,HyperVDiskUsed,
  52.                HyperVDisk,HyperVStatus,HyperVOwnerNode, isCluster)
  53.                    VALUES ('{0}','{1}','{2}','{3}','{4}',
  54.                '{5}','{6}','{7}','{8}')" -f '1',$_.VMName,$_.MemoryAssigned.ToString(), $_.MemoryMaximum.ToString(),
  55.                 $VHDFileSize.ToString(), $VHDSize.ToString(), $_.State,$_.ComputerName,$_.IsClustered
  56.         Execute-MySQLNonQuery $Connection $QueryInsert
  57.         }
  58. }
  59.  
  60.  
  61.  
  62. function Main(){
  63. $MySQLAdminUserName = 'hyperv'
  64. $MySQLAdminPassword = 'Hqdv37RJp0jFBrGtkJaHoNbNOFMZiB3u'
  65. $MySQLDatabase = 'portal'
  66. $MySQLHost = '192.168.9.2'
  67. #$ConnectionString = "server=" + $MySQLHost + ";port=3306;uid=" + $MySQLAdminUserName + ";pwd=" + $MySQLAdminPassword + ";database="+$MySQLDatabase
  68. #$QueryInsert = "INSERT INTO portal.vps_hyperv_sync (HyperVType,HyperVName,HyperVRAMUsed,HyperVRAM,HyperVDiskUsed,
  69. #HyperVDisk,HyperVVPS,HyperVStatus,HyperVIP,HyperVOwnerNode, isCluster)
  70. # VALUES ('HyperVType','HyperVName','HyperVRAMUsed','HyperVRAM','HyperVDiskUsed',
  71. #'HyperVDisk','HyperVVPS','HyperVStatus','HyperVIP','HyperVNode',1)"
  72. $QueryClear = "TRUNCATE table portal.vps_hyperv_sync"
  73.  
  74. $Connection = ConnectMySQL $MySQLAdminUserName $MySQLAdminPassword $MySQLHost $MySQLDatabase
  75. UpdateStatusTask $Connection '1'
  76. Execute-MySQLNonQuery $Connection $QueryClear
  77. #Execute-MySQLNonQuery $Connection $QueryInsert
  78. SetVMInfo $Connection
  79. UpdateStatusTask $Connection '0'
  80. $Connection.Close()
  81. }
  82.  
  83. Main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement