Unidesk 3.0 Script to Increase UEP Size for Persistent Desktops Summary When creating a desktop the size of the Personalization Layer (UEP) is defined in GB for the desktop. There are two vhdx files that make up the UEP both set to this size. One is for Application files and Settings; the other is for user data. They are start out the last two disks attached to the desktop but when more layers are added they can then be in any volume on the desktop. The Path to the UEP files is always under the CP appliance folder\unidesklayers\users. See below. This script can be used to increase the size of the UEP disks. The script uses The Hyper-V PowerShell Cmdlets to increase the VHDX size. It then using PowerShell Remoting with DiskPart scripts to extend the volume in Windows. Version 1.0 February 28, 2015 Page 1
Restrictions The following restriction should be kept in mind: 1. This applies to Unidesk 3.x systems only (Hyper-V), do not use on Unidesk 2.x 2. The desktop must be running when the script is run, therefore if desktops are actively being REBIC ed ensure that the desktops you are running the script against are not in the list of those being rebuilt. 3. This script only works on Persistent Desktops. Non-Persistent Desktops must be recreated. Requirements 1. The Hyper-V Module is required on the machine the script is run from 2. Administrator Access to HyperV is required 3. Administrative Access to the desktops is required 4. The Desktops must have WINRM enabled, running and configured. This is best done through GPO. Setting up the script Make sure the script is not blocked. Check its properties and unblock if necessary. Copy the script into a folder. Edit the script appropriately. At the top of the script there is a section where variables are defined as shown below. Each of these settings must be configured for your environment. Note: the script can be run by entering a lits of desktops in the $Destops array as shown below. The script will also check for a Desktops.txt file in the folder the script is run from. If that file is found the script will load desktops from the file and use the file only and not the setting below. The file should have only the desktop name one per line. # Define Variables #Enter one or more Desktops in an array. #If Desktops.txt is found in the script directory the desktops to update will be loaded from Deskotps.txt rather than this setting. $Desktops = @("persistentrds5") #Enter the Size in GB that the desktop will be i.e if you want the UEP to be 10 GB use 10 $SizeinGB = 15 #Enter vcenter Server FQDN. You will be prompted for credentials $HyperVServer = "hyperv2.zylowski.com" #Enter Admin User and password on Windows Desktops $GuestUser = "zylowski\rzylowski" $GuestPassword = "password" The $Desktops array can have as many desktops defined as needed. Just separate the desktops with a coma and put quotes around them. The $size in GB is the size the script sets the UEP to. So if you want the UEP to be 20 GB and it is currently 5 GB you enter 20. Version 2.0 April 20, 2012 Page 2
Running the script Run PowerShell and change directories to the script folder. Make sure you have previously run the following command to allow scripts to run on this system or run it now Set-ExecutionPolicy Remote Signed Then run the script by typing>.\growuephv.ps1 Or you can use the PowerShell ISE or any other PowerShell Editor. The script will output lots of information as it runs, it will also create a log in the script folder. The upgrade here was from 4 to 10 GB. The script #Script Developed by Rob Zylowski, Unidesk Corporation #Version 1.0 10-5-2012 #This script can be used to increase the size of the Personalization Layer for Unidesk Persistent Desktops #The desktops should be running and the user can even be logged on #The script requires the HyperV module be installed on your workstation and WinRM running and configured on the desktops function Get-ScriptDirectory $Invocation = (Get-Variable MyInvocation -Scope 1).Value Split-Path $Invocation.MyCommand.Path Function LogLine($strLine) write-host $strline $global:logoutput += "$strline `n" #This may generate an error if its already loaded so we will clear the error cache $Error.Clear() # Define Variables Version 2.0 October 12, 2012 Page 3
#Enter one or more Desktops in an array. #If Desktops.txt is found in the script directory the desktops to update will be loaded from Deskotps.txt #rather than this setting. $Desktops = @("persistentrds5") #Enter the Size in GB that the desktop will be i.e if you want the UEP to be 10 GB use 10 $SizeinGB = 15 #Enter vcenter Server FQDN. You will be prompted for credentials $HyperVServer = "hyperv2.zylowski.com" #Enter Admin User and password on Windows Desktops $GuestUser = "zylowski\rzylowski" $GuestPassword = "password" # MAIN SCRIPT #If Desktops.txt exists use it for the source desktops $ScriptSource = Get-ScriptDirectory $mydatetime = Get-Date -Format MM-dd-yyyy-HHmmss $global:logfile = "$ScriptSource\GrowUEP-$myDateTime.txt" LogLine "--------------------------------------------------" Logline "Running Hyper-V Extend Unidesk UEP Disks Script" LogLine "--------------------------------------------------" if (Test-Path "$ScriptSource\Desktops.txt") $Desktops = Get-Content -Path "$ScriptSource\Desktops.txt" $count = $Desktops.Count Logline "Desktops.txt file found with [$count] desktops to extend" else $count = $Desktops.Count Logline "Desktops.txt file NOT found will use desktop list defined in script" Logline "Ther are [$count] desktops to extend" #Load HyperV module Logline "Importing Module HyperV" Import-Module Hyper-V $Capacity = 1024*1024*1024*$SizeinGB Logline "Disk Capacity will be set to [$SizeinGB] GB" Logline "--------------------------------------------------" Logline "" #Run the process for each MM in the Desktops Array foreach ($MM in $Desktops) Logline "Processing Desktop [$MM]" #Get the list of hard drives for the virtual machine $CurrentVM = Get-VM -Name $MM -ComputerName $HyperVServer $GetAllHds = Get-VHD -VMId $CurrentVM.id -ComputerName $HyperVServer #For each hard disk see if its a UEP disk by looking at the path. If it has a /User/ in the path its a UEP. foreach ($s in $GetAllHds) If ($s.path -like "*\User\*") $myuepdisk = $s.path logline "$myuepdisk is a UEP Disk, Resizing to [$Capacity] GB" Resize-VHD -Path $s.path -ComputerName $HyperVServer -SizeBytes $Capacity Else # $s.path + " is Not a UEP Disk" if (!($?)) Version 2.0 April 20, 2012 Page 4
Logline "HyperV Extend Disk failed skipping this desktop [$MM]" continue #Get the disk name using regex $regex = [regex]"([^\\]+)$" $VMDK = [regex]::match($s.path, $regex).groups[1] #Get the prefix using regex $regex = [regex]"^(.*?)\..*" $UEPPrefixFull = [regex]::match($vmdk, $regex).groups[1] $UEPPrefixFullString = "$UEPPrefixFull" $UEPPrefix = $UEPPrefixFullString.substring(0,6) #Now use POwershell remoting to extend the volume size on the desktop. Logline "Running Extend Job on Remote Computer..." $output = Invoke-Command -ComputerName $MM -ScriptBlock param ($UEPPrefix) #Get the disks using LIST DISK command NEW-ITEM name LISTVOLUMES.TXT itemtype file force OUT-NULL ADD-CONTENT path LISTVOLUMES.TXT LIST VOLUME $LISTDISKS=(DISKPART /S LISTVOLUMES.TXT) NEW-ITEM name EXTENDVOLUME.TXT itemtype file force OUT-NULL foreach ($VOL in $LISTDISKS) if ($VOL.length -ne 0) if ($VOL.length -gt 31) $volumestring = $VOL.substring(24,7).trim() if (($volumestring -ne "") -and ($volumestring -ne "------") -And ($volumestring -ne "on 6.3") -And ($volumestring -ne "Micros") -And ($volumestring -ne "BOOT")) if ($volumestring -eq $UEPPrefix) $volumeid = $vol.substring(8,3).trim() write-host "UEP Volume Found:" $volumestring "volumeid: $volumeid" write-output "UEP Volume Found:" $volumestring "volumeid: $volumeid `n" #Get the size to extend the disk NEW-ITEM name GETDISKS.TXT itemtype file force OUT-NULL ADD-CONTENT path GETDISKS.TXT RESCAN ADD-CONTENT path GETDISKS.TXT SELECT VOLUME $volumeid ADD-CONTENT path GETDISKS.TXT LIST DISK $GETDISKS=(DISKPART /S GETDISKS.TXT) foreach ($DISK in $GETDISKS) if ($DISK.length -ne 0) if ($DISK.substring(0,1).trim() -eq "*") $DiskID = $DISK.substring(6,3).trim() write-host "Disk ID: $DiskID" write-output "Disk ID: $DiskID `n" $ExtendSize = $DISK.substring(36,5).trim() $SizeMetric = $DISK.substring(41,2).trim() write-host "Metrix Sized Used: $SizeMetric" write-host "Metrix Sized Used: $SizeMetric `n" [int]$intextendsize = [convert]::toint32($extendsize, 10) if($sizemetric -eq "GB") $intextendsize = $intextendsize*1024 Version 2.0 October 12, 2012 Page 5
if (Test-Path EXTENDVOLUME.TXT)Remove-Item EXTENDVOLUME.TXT write-host "Extend Size: $intextendsize MB" write-output "Extend Size: $intextendsize MB `n" write-host "Extending Disk Size By : $intextendsize MB" write-output "Extending Disk Size By : $intextendsize MB `n" ADD-CONTENT path EXTENDVOLUME.TXT SELECT VOLUME $volumeid ADD-CONTENT path EXTENDVOLUME.TXT EXTEND SIZE=$intExtendSize ADD-CONTENT path EXTENDVOLUME.TXT DETAIL DISK $RemoteOutput = $EXTENDTVOL=(DISKPART /S EXTENDVOLUME.TXT) write-host "List Disk Output After Extend Operation" write-output "List Disk Output After Extend Operation `n" write-host "" write-output "`n" write-host "$RemoteOutput" write-output "$RemoteOutput `n" write-host "Removing working files.." write-output "Removing working files.. `n" Remove-Item EXTENDVOLUME.TXT -Confirm:$false Remove-Item GETDISKS.TXT -Confirm:$false Remove-Item LISTVOLUMES.TXT -Confirm:$false write-host "-------------------------------------------------------" write-output "------------------------------------------------------- `n" write-host "" write-output "`n" -ArgumentList $UEPPrefix Logline "" $RunningOutput += $global:logoutput $RunningOutput += $output $global:logoutput = "" $RunningOutput Out-file -FilePath $global:logfile -Encoding ASCII -Append Version 2.0 April 20, 2012 Page 6