Advertisement
pakempf

Powershell VM deployement

Sep 2nd, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #*=============================================================================
  2. #* Script Name: create-vm.ps1
  3. #* Created:     2014-07-29
  4. #* Author:  Paul-Antoine KEMPF
  5. #* Purpose:     This is a simple script that executes get-process.
  6. #*          
  7. #*=============================================================================  
  8. #*=============================================================================
  9. #* PARAMETER DECLARATION
  10. #*=============================================================================
  11. Param(
  12.     [Parameter(Mandatory=$True,Position=1)]
  13.     [string]$vmname,
  14.     [Parameter(Mandatory=$True,Position=2)]
  15.     [string]$username,
  16.     [Parameter(Mandatory=$True,Position=3)]
  17.     [string]$os,
  18.     [Parameter(Mandatory=$True,Position=4)]
  19.     [string]$landesk,
  20.     [Parameter(Mandatory=$True,Position=5)]
  21.     [string]$sla,
  22.     [Parameter(Mandatory=$True,Position=6)]
  23.     [string]$supervision,
  24.     [Parameter(Mandatory=$True,Position=7)]
  25.     [string]$env,
  26.     [Parameter(Mandatory=$True,Position=8)]
  27.     [string]$owner
  28.    
  29. )
  30. #*=============================================================================
  31. #* REVISION HISTORY
  32. #*=============================================================================
  33. #* Date:  
  34. #* Author:
  35. #* Purpose:
  36. #*=============================================================================  
  37. #*=============================================================================
  38. #* IMPORT LIBRARIES
  39. #*=============================================================================  
  40. Write-Output ('<div class="content">')
  41. Write-Output ("<div class='center'>
  42.                 <img class='center' src='./images/bann.jpg'/>
  43.             </div>
  44.                 <h1 style='text-align:center'>VMaaS Deployement Tool</h1>
  45.                 <div style='text-align:center'>Please check following infos to make sure everything went well.<br/>
  46.                 ________________________________________________________<br/>
  47.                 </div>
  48. ")
  49.  
  50. Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
  51.  
  52. #Module VMWARE
  53.     if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
  54.        { Add-PsSnapin VMware.VimAutomation.Core }
  55. #*=============================================================================
  56. #* PARAMETERS
  57. #*=============================================================================  
  58. #*=============================================================================
  59. #* INITIALISE VARIABLES
  60. #*=============================================================================
  61. try {
  62.     $Logfile = "C:\powershell_scripts\logs\$($vmname)_$($username)_$($os).log"
  63.  
  64.     Function LogWrite
  65.     {
  66.        Param ([string]$logstring)
  67.  
  68.        Add-content $Logfile -value $logstring
  69.     }
  70.  
  71.     LogWrite ([datetime]::now,"Initialisation des logs")
  72.  
  73.     # Increase buffer width/height to avoid PowerShell from wrapping the text before
  74.     # sending it back to PHP (this results in weird spaces).
  75.     $pshost = Get-Host
  76.     $pswindow = $pshost.ui.rawui
  77.     $newsize = $pswindow.buffersize
  78.     $newsize.height = 3000
  79.     $newsize.width = 400
  80.     $pswindow.buffersize = $newsize
  81.  
  82.     #Récupération des variables depuis le fichier de configuration
  83.     $configPath     = "C:\powershell_scripts\config.xml"
  84.     [xml]$configXml = Get-Content $configPath
  85.     LogWrite $configPath
  86.     [System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configPath)
  87.  
  88.     #Variables de connexion à vCenter 5.5
  89.     LogWrite ([datetime]::now,"User :  $($($configXml.configuration.appSettings.add | where { $_.key -eq "vCenterUser" }).value)")
  90.     LogWrite ([datetime]::now,"PW :  $($($configXml.configuration.appSettings.add | where { $_.key -eq "vCenterPassword" }).value)")
  91.     [string]$My_vCenter          = $($configXml.configuration.appSettings.add | where { $_.key -eq "vCenter" }).value  
  92.     [string]$My_vCenter_User     = $($configXml.configuration.appSettings.add | where { $_.key -eq "vCenterUser" }).value
  93.     [string]$My_vCenter_Password = $($configXml.configuration.appSettings.add | where { $_.key -eq "vCenterPassword" }).value
  94.  
  95.     #Connexion à vCenter 5.5
  96.     Write-Output  "Connecting to vCenter...<br/>"
  97.     LogWrite ([datetime]::now,'Connecting vCenter 5.5')
  98.         $My_vCenterConnection = Connect-VIServer -Server $My_vCenter -User $My_vCenter_User  -Password $My_vCenter_Password
  99.     LogWrite ([datetime]::now,'Connexion à vCenter 5.5 : ',$My_vCenterConnection.IsConnected)
  100.     Write-Output  "Connection successful.<br/>"
  101.     #Récupération des variables par défaut
  102.     $My_Folder = Get-Folder -name "VM-Sandbox"
  103.     $My_ResourcePool = Get-ResourcePool -name "RP-Sandbox"
  104.     $My_Datastore = Get-Datastore -name "DS-C2_Test_SATA_01"
  105.     LogWrite ([datetime]::now,"Folder : $($My_Folder)")
  106.     LogWrite ([datetime]::now,"RP : $($My_ResourcePool)")
  107.     LogWrite ([datetime]::now,"DS : $($My_Datastore)")
  108. } catch {
  109.     Write-Output "Erreur lors de l'initialisation des variables.<br/>"
  110.     LogWrite $_
  111. }
  112.  #*=============================================================================
  113. #* EXCEPTION HANDLER
  114. #*=============================================================================  
  115. #*=============================================================================
  116. #* FUNCTION LISTINGS
  117. #*=============================================================================  
  118. #*=============================================================================
  119. #* Function:    function1
  120. #* Created:     2012-01-01
  121. #* Author:  My Name
  122. #* Purpose:     This function does X Y Z
  123. #* =============================================================================  
  124. #*=============================================================================
  125. #* END OF FUNCTION LISTINGS
  126. #*=============================================================================  
  127. #*=============================================================================
  128. #* SCRIPT BODY
  129. #*=============================================================================
  130. Write-Output "<br/>"
  131. Write-Output "Hello $username ! <br />"
  132. $notes = "$sla $env TMP $owner - Déployée automatiquement via VMaaS. Contact : $($username)"
  133. LogWrite $notes
  134. Switch ($os) {
  135.     'rhel' {
  136.         Write-Output "Deployement of a RHEL VM with name $vmname...<br/>"
  137.         try {
  138.             #$My_Template = Get-Template -Name 'VMAAS01VTL'
  139.             #$My_OSSpec = Get-OSCustomizationSpec -name "Personnalisation RHEL VMAAS"
  140.             #LogWrite ([datetime]::now,"OSSpec $($My_OSSpec)")
  141.             #$My_vm = New-VM -template $My_Template -location $My_Folder -name $vmname -notes $notes -ResourcePool $My_ResourcePool -datastore $My_Datastore -OSCustomizationSpec $My_OSSpec
  142.         } catch {
  143.             Write-Output "Erreur lors du déploiement. Merci d'analyser les logs.<br/>"
  144.             LogWrite $_
  145.         }
  146.     }
  147.     'windows2008r2' {
  148.         try {
  149.             Write-Output "Deployement of a Windows Server 2008 R2 VM with name $vmname...<br/>"
  150.             #$My_Template = Get-Template -Name 'VMAAS01VTW'
  151.             #$My_OSSpec = Get-OSCustomizationSpec -name "Personnalisation Windows VMAAS"
  152.             #LogWrite ([datetime]::now,"OSSpec $($My_OSSpec)")
  153.             #$My_vm = New-VM -template $My_Template -location $My_Folder -name $vmname -notes $notes -ResourcePool $My_ResourcePool -datastore $My_Datastore -OSCustomizationSpec $My_OSSpec
  154.         } catch {
  155.             Write-Output "Erreur lors du déploiement. Merci d'analyser les logs.<br/>"
  156.             LogWrite $_
  157.         }
  158.     }
  159. }
  160.     Write-Output "Looks like everything went well.<br/>
  161.         </div>
  162.     </body>
  163. </html>"
  164.  
  165. #*=============================================================================
  166. #* END SCRIPT BODY
  167. #*=============================================================================  
  168. Disconnect-VIServer -Server $My_vCenter -Confirm:$False -Force
  169. LogWrite ([datetime]::now,"Deconnection du serveur")
  170. #*=============================================================================
  171. #* END OF SCRIPT
  172. #*=============================================================================
  173.  
  174. LogWrite ([datetime]::now,"Fin du script")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement