Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #*=============================================================================
- #* Script Name: create-vm.ps1
- #* Created: 2014-07-29
- #* Author: Paul-Antoine KEMPF
- #* Purpose: This is a simple script that executes get-process.
- #*
- #*=============================================================================
- #*=============================================================================
- #* PARAMETER DECLARATION
- #*=============================================================================
- Param(
- [Parameter(Mandatory=$True,Position=1)]
- [string]$vmname,
- [Parameter(Mandatory=$True,Position=2)]
- [string]$username,
- [Parameter(Mandatory=$True,Position=3)]
- [string]$os,
- [Parameter(Mandatory=$True,Position=4)]
- [string]$landesk,
- [Parameter(Mandatory=$True,Position=5)]
- [string]$sla,
- [Parameter(Mandatory=$True,Position=6)]
- [string]$supervision,
- [Parameter(Mandatory=$True,Position=7)]
- [string]$env,
- [Parameter(Mandatory=$True,Position=8)]
- [string]$owner
- )
- #*=============================================================================
- #* REVISION HISTORY
- #*=============================================================================
- #* Date:
- #* Author:
- #* Purpose:
- #*=============================================================================
- #*=============================================================================
- #* IMPORT LIBRARIES
- #*=============================================================================
- Write-Output ('<div class="content">')
- Write-Output ("<div class='center'>
- <img class='center' src='./images/bann.jpg'/>
- </div>
- <h1 style='text-align:center'>VMaaS Deployement Tool</h1>
- <div style='text-align:center'>Please check following infos to make sure everything went well.<br/>
- ________________________________________________________<br/>
- </div>
- ")
- Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
- #Module VMWARE
- if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
- { Add-PsSnapin VMware.VimAutomation.Core }
- #*=============================================================================
- #* PARAMETERS
- #*=============================================================================
- #*=============================================================================
- #* INITIALISE VARIABLES
- #*=============================================================================
- try {
- $Logfile = "C:\powershell_scripts\logs\$($vmname)_$($username)_$($os).log"
- Function LogWrite
- {
- Param ([string]$logstring)
- Add-content $Logfile -value $logstring
- }
- LogWrite ([datetime]::now,"Initialisation des logs")
- # Increase buffer width/height to avoid PowerShell from wrapping the text before
- # sending it back to PHP (this results in weird spaces).
- $pshost = Get-Host
- $pswindow = $pshost.ui.rawui
- $newsize = $pswindow.buffersize
- $newsize.height = 3000
- $newsize.width = 400
- $pswindow.buffersize = $newsize
- #Récupération des variables depuis le fichier de configuration
- $configPath = "C:\powershell_scripts\config.xml"
- [xml]$configXml = Get-Content $configPath
- LogWrite $configPath
- [System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configPath)
- #Variables de connexion à vCenter 5.5
- LogWrite ([datetime]::now,"User : $($($configXml.configuration.appSettings.add | where { $_.key -eq "vCenterUser" }).value)")
- LogWrite ([datetime]::now,"PW : $($($configXml.configuration.appSettings.add | where { $_.key -eq "vCenterPassword" }).value)")
- [string]$My_vCenter = $($configXml.configuration.appSettings.add | where { $_.key -eq "vCenter" }).value
- [string]$My_vCenter_User = $($configXml.configuration.appSettings.add | where { $_.key -eq "vCenterUser" }).value
- [string]$My_vCenter_Password = $($configXml.configuration.appSettings.add | where { $_.key -eq "vCenterPassword" }).value
- #Connexion à vCenter 5.5
- Write-Output "Connecting to vCenter...<br/>"
- LogWrite ([datetime]::now,'Connecting vCenter 5.5')
- $My_vCenterConnection = Connect-VIServer -Server $My_vCenter -User $My_vCenter_User -Password $My_vCenter_Password
- LogWrite ([datetime]::now,'Connexion à vCenter 5.5 : ',$My_vCenterConnection.IsConnected)
- Write-Output "Connection successful.<br/>"
- #Récupération des variables par défaut
- $My_Folder = Get-Folder -name "VM-Sandbox"
- $My_ResourcePool = Get-ResourcePool -name "RP-Sandbox"
- $My_Datastore = Get-Datastore -name "DS-C2_Test_SATA_01"
- LogWrite ([datetime]::now,"Folder : $($My_Folder)")
- LogWrite ([datetime]::now,"RP : $($My_ResourcePool)")
- LogWrite ([datetime]::now,"DS : $($My_Datastore)")
- } catch {
- Write-Output "Erreur lors de l'initialisation des variables.<br/>"
- LogWrite $_
- }
- #*=============================================================================
- #* EXCEPTION HANDLER
- #*=============================================================================
- #*=============================================================================
- #* FUNCTION LISTINGS
- #*=============================================================================
- #*=============================================================================
- #* Function: function1
- #* Created: 2012-01-01
- #* Author: My Name
- #* Purpose: This function does X Y Z
- #* =============================================================================
- #*=============================================================================
- #* END OF FUNCTION LISTINGS
- #*=============================================================================
- #*=============================================================================
- #* SCRIPT BODY
- #*=============================================================================
- Write-Output "<br/>"
- Write-Output "Hello $username ! <br />"
- $notes = "$sla $env TMP $owner - Déployée automatiquement via VMaaS. Contact : $($username)"
- LogWrite $notes
- Switch ($os) {
- 'rhel' {
- Write-Output "Deployement of a RHEL VM with name $vmname...<br/>"
- try {
- #$My_Template = Get-Template -Name 'VMAAS01VTL'
- #$My_OSSpec = Get-OSCustomizationSpec -name "Personnalisation RHEL VMAAS"
- #LogWrite ([datetime]::now,"OSSpec $($My_OSSpec)")
- #$My_vm = New-VM -template $My_Template -location $My_Folder -name $vmname -notes $notes -ResourcePool $My_ResourcePool -datastore $My_Datastore -OSCustomizationSpec $My_OSSpec
- } catch {
- Write-Output "Erreur lors du déploiement. Merci d'analyser les logs.<br/>"
- LogWrite $_
- }
- }
- 'windows2008r2' {
- try {
- Write-Output "Deployement of a Windows Server 2008 R2 VM with name $vmname...<br/>"
- #$My_Template = Get-Template -Name 'VMAAS01VTW'
- #$My_OSSpec = Get-OSCustomizationSpec -name "Personnalisation Windows VMAAS"
- #LogWrite ([datetime]::now,"OSSpec $($My_OSSpec)")
- #$My_vm = New-VM -template $My_Template -location $My_Folder -name $vmname -notes $notes -ResourcePool $My_ResourcePool -datastore $My_Datastore -OSCustomizationSpec $My_OSSpec
- } catch {
- Write-Output "Erreur lors du déploiement. Merci d'analyser les logs.<br/>"
- LogWrite $_
- }
- }
- }
- Write-Output "Looks like everything went well.<br/>
- </div>
- </body>
- </html>"
- #*=============================================================================
- #* END SCRIPT BODY
- #*=============================================================================
- Disconnect-VIServer -Server $My_vCenter -Confirm:$False -Force
- LogWrite ([datetime]::now,"Deconnection du serveur")
- #*=============================================================================
- #* END OF SCRIPT
- #*=============================================================================
- LogWrite ([datetime]::now,"Fin du script")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement