Last active
July 15, 2016 14:00
-
-
Save JamesCalleja/6775cbb23e8fd6b50a94c90c1dfef63a to your computer and use it in GitHub Desktop.
Citrix storefront base disk refesh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Written by: James Calleja # | |
| #Date: 15/07/2016 # | |
| # # | |
| #This script will connect to vcentre, clone from the Golden_Base_Image, and push it to the # | |
| #environment selected via cirtix # | |
| ################################################################################################ | |
| #load powershell modules and snapins | |
| add-pssnapin vmware.vimautomation.core -EA SilentlyContinue -WarningAction SilentlyContinue | |
| Add-PSSnapin citrix* -EA SilentlyContinue -WarningAction SilentlyContinue | |
| #Declair variables | |
| $AdminAddress = 'np-xd-dc01srv.env.local:80' | |
| $TargetEnvironment = $NULL | |
| $Envs = ('TE1', 'TE2', 'TE3', 'PSE', 'test') | |
| $ViServer = 'CSC-XD-VC01SRV' | |
| $SourceDisk = 'Golden_Base_Image' | |
| $vm = $NULL | |
| #Define functions | |
| Function Check-UserInput | |
| { | |
| if (($args[0] -is [int]) -eq $false -or $args[0] -le 0 -or $args[0] -ge 6) | |
| { | |
| return $false | |
| } | |
| else | |
| { | |
| return $true | |
| } | |
| } | |
| Function Get-TargetEnvironment | |
| { | |
| $TargetEnvironment = read-host 'Which environment do you want to refresh the base disk for?(1 - 4)' | |
| return $TargetEnvironment | |
| } | |
| Function Delete-VM | |
| { | |
| Trap | |
| { | |
| write-host 'Error while trying to delete' $args -fore Black -back red | |
| } | |
| if ((get-vm $args).powerstate -eq 'PoweredOn') | |
| { | |
| stop-vm $args -confirm:$false | |
| } | |
| get-vm $args | Remove-vm -DeletePermanently -Confirm:$false -EA Stop | |
| } | |
| #Check that the source base disk has been updated with the software \ pathch required for deployment | |
| write-host 'Have you updated the' $sourceDisk '? (yes/no)' | |
| $answer = read-host | |
| if ($answer -like 'y' -or $answer -like 'yes') | |
| { | |
| get-date -format hh:mm:ss | |
| #prompt user for which environment they want to refesh | |
| write ' | |
| 1: TE1 | |
| 2: TE2 | |
| 3: TE3 | |
| 4: PSE | |
| ' | |
| #vet input before continuing | |
| do | |
| { | |
| [Int]$targetEnvironment = get-TargetEnvironment | |
| } | |
| while((Check-UserInput $targetEnvironment) -eq $false) | |
| $env = $envs[$targetEnvironment -1] | |
| write-host $env 'selected' | |
| #get creds and connect to vcentre | |
| write 'enter credentials to connect to CSC-XD-VC01SRV' | |
| $viCreds = Get-Credential -Message $ViServer -UserName 'headoffice\' | |
| Connect-VIServer $ViServer -Credential $viCreds | |
| #find target base disk | |
| $vm = get-vm base_w7_$env -ea SilentlyContinue | |
| if ($vm -eq $NULL) | |
| { | |
| write-host 'Could not find base_w7_'$env | |
| write-host 'Creating a new VM from' $sourceDisk | |
| new-vm -vm $sourceDisk -Name base_w7_$env -Location (get-vm $sourceDisk).folder -Datastore (get-vm $sourceDisk | get-datastore) -VMHost (get-vm $sourceDisk | get-vmhost) -Confirm:$false | Out-Null | |
| write-host 'Created new vm' (get-vm base_w7_$env).name | |
| } | |
| else | |
| { | |
| write-host 'Found VM base_w7_'$env | |
| #rename old disk | |
| [string]$vm_old = $vm.name + '_old' | |
| set-vm $vm -Name $vm_old -Confirm:$false | Out-Null | |
| write-host 'Renamed' $vm 'to' $vm_old | |
| #Clone refresh disk | |
| new-vm -vm $sourceDisk -Name ($vm.name) -Location ($vm.location) -Datastore (get-vm $vm_old | get-datastore) -VMHost (get-vm $vm_old | get-vmhost) -Confirm:$false | Out-Null | |
| write-host 'Cloned' $sourceDisk 'to ' $vm.name | |
| #Delete the old vm and it's disks | |
| Delete-VM $vm_old | |
| write-host 'Deleted ' $vm_old | |
| } | |
| #build variables for citrix portion of the script | |
| $a = '*' + $env + '*' | |
| $MachineCatalog = get-brokercatalog *$env* | ?{$_.name -like $a -and $_.name -notlike '*dsx*' -and $_.name -notlike '*openspan*'} | select name | |
| [string]$vm_Name = '*Base_W7_' + $Env + '*' | |
| $LiteralPath = ls 'XDHyp:\HostingUnits\Desktops - Test Environment' | ?{$_.pspath -like $vm_Name} | |
| $literalPath = $LiteralPath.pspath.ToString().Trimstart('Citrix.Host.Admin.V2\Citrix.Hypervisor::') | |
| $SnapShot = 'Citrix_XD_' + $MachineCatalog.Name | |
| $MasterImageVM = $LiteralPath + '\' + $SnapShot + '.snapshot' | |
| $NamingScheme = 'VD-' + $env + '-W7-##' | |
| $domain = $env + '.env.local' | |
| $deliveryGroup = (Get-BrokerDesktopGroup * | ?{$_.name -like $a -and $_.name -notlike '*openspan*'}).name | |
| New-HypVMSnapshot -AdminAddress $AdminAddress -LiteralPath $LiteralPath -SnapshotName $SnapShot | Out-Null | |
| write-host 'New hypervisor snapshot created' $snapshot | |
| Set-ProvSchemeMetadata -AdminAddress $AdminAddress -Name 'ImageManagementPrep_DoImagePreparation' -ProvisioningSchemeName $MachineCatalog.Name -Value 'True' | Out-Null | |
| Publish-ProvMasterVmImage -AdminAddress $AdminAddress -MasterImageVM $MasterImageVM -ProvisioningSchemeName $MachineCatalog.Name -RunAsynchronously | Out-Null | |
| write-host 'Provisioning Master Image published' $MasterImageVM | |
| Get-AcctIdentityPool -IdentityPoolName $deliveryGroup | Set-AcctIdentityPool -NamingScheme $NamingScheme -StartCount 01 -NamingSchemeType Numeric -Domain $domain | |
| Start-BrokerRebootCycle -AdminAddress $AdminAddress -InputObject @($MachineCatalog.name) -RebootDuration 0 | Out-Null | |
| write-host 'Refreshing desktops' | |
| Set-BrokerDesktopGroup -AdminAddress $AdminAddress -InMaintenanceMode $False -Name $deliveryGroup | |
| write-host 'Turning maintenance mode off' | |
| get-date -format hh:mm:ss | |
| } | |
| else | |
| { | |
| write-host 'Please make updates to the' $sourceDisk 'before runing this script' | |
| exit | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment