1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# =========================================================================================================== # # Created by: Trentent Tye # # Creation Date: Oct 15, 2013 # # File Name: Fix-Time-Sync.ps1 # # Description: This script will be used to resolve an issue with VMWare where the VMWare Tools cause a # time sync with the host. If the host has an incorrect time it will knock the time out of # sync on the guest. To resolve this issue some text entires need to be made to the VMX # fix. This is detailed here: # http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1189 # # =========================================================================================================== Add-PSSnapin VMware.VimAutomation.Core connect-viserver wsvcenter20 $ExtraOptions = @{ "tools.syncTime"="0"; "time.synchronize.continue"="0"; "time.synchronize.restore"="0"; "time.synchronize.resume.disk"="0"; "time.synchronize.shrink"="0"; "time.synchronize.tools.startup"="0"; "time.synchronize.tools.enable" = "0"; "time.synchronize.resume.host" = "0"; } # build our configspec using the hashtable from above. I prefer this # method over the use of files b/c it has one less needless dependency. $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec # note we have to call the GetEnumerator before we can iterate through Foreach ($Option in $ExtraOptions.GetEnumerator()) { $OptionValue = New-Object VMware.Vim.optionvalue $OptionValue.Key = $Option.Key $OptionValue.Value = $Option.Value $vmConfigSpec.extraconfig += $OptionValue } # Get all vm's starting with name: $VMView = Get-View -ViewType VirtualMachine -Filter @{"Name" = "WSCTX"} foreach($vm in $VMView){ $vm.ReconfigVM_Task($vmConfigSpec) } # Get all vm's starting with name: $VMView = Get-View -ViewType VirtualMachine -Filter @{"Name" = "WSAPV"} foreach($vm in $VMView){ $vm.ReconfigVM_Task($vmConfigSpec) } |
Utilizing PowerShell to make Citrix VM Templates
Because my company doesn’t utilize provisioining servers for deploy new Citrix XenApp servers, I’ve had to come up with a couple of PowerShell scripts to make VMWare Templates that I can then deploy multiple XenApp servers. You need VMWare PowerCLI to run this script. This is my script:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 function create-template{Param([CmdletBinding()][Parameter(ValueFromPipeline=$true,Position=0,Mandatory=$true,HelpMessage="Insert Message")][ValidateNotNullOrEmpty()]$InputObject,[Parameter(Position=1,Mandatory=$false,HelpMessage="Insert Preferred Folder")]$folder,[Parameter(Position=2,Mandatory=$false,HelpMessage="Insert Preferred Target Datastore")]$datastore,[Parameter(Position=3,Mandatory=$false,HelpMessage="Insert Preferred Target Host")]$vmhost,[Parameter(Position=4,Mandatory=$false,HelpMessage="Insert Preferred Disk Storage Format")][ValidateSet("Thick","Thin")]$format = "Thin")PROCESS{$InputObject | %{$vm = Get-VM $_$name = $vm.name$newname = -join("clone-",$name)$date = get-date -uformat "-%Y-%m-%d"$templatename = -join("template-",$name,$date)If ($datastore -eq $null){$datastore = Get-Datastore -VM $vm}If ($folder -eq $null){$folder = $vm.Folder}If ($vmhost -eq $null){$vmhost = Get-Cluster -VM $vm | Get-VMHost | Get-Random | Where{$_ -ne $null}}Write-Host "VM = $vm"Write-Host "Name = $name"Write-Host "NewName = $newname"Write-Host "DataStore = $datastore"Write-Host "Folder = $folder"Write-Host "VMHost = $vmhost"write-Host "templatename=$templatename"sleep 4Write-Host "Setting up domain unjoin script..."Remove-Item "\\$name\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\remove.cmd"Add-Content "\\$name\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\remove.cmd" "netdom remove $name /Force"Add-Content "\\$name\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\remove.cmd" "netdom renamecomputer $name /newname:XA6TEMPLATE /Force"Add-Content "\\$name\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\remove.cmd" "REG ADD `"HKEY_LOCAL_MACHINE\SYSTEM\Setup\Status\SysprepStatus`" /v `"GeneralizationState`" /tREG_DWORD /d 0x7 /f"Add-Content "\\$name\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\remove.cmd" "REG ADD `"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform`" /v `"SkipRearm`" /t REG_DWORD /d 0x1 /f"Add-Content "\\$name\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\remove.cmd" "`"C:\Program Files (x86)\Citrix\XenApp\ServerConfig\XenAppConfigConsole.exe`"/ExecutionMode:ImagePrep /PrepMsmq:True"Add-Content "\\$name\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\remove.cmd" "shutdown -s -t 90 -f"Add-Content "\\$name\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\remove.cmd" "del /q `"c:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\remove.cmd"Write-Host "Setting Autologon..."psexec \\$name "REG.EXE" ADD `"\\$name\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon`" /v `"DefaultUserName`" /d Administrator /f`"sleep 5psexec \\$name "REG.EXE" ADD `"\\$name\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon`" /v `"DefaultPassword`" /d Hello /f`"sleep 5psexec \\$name "REG.EXE" ADD `"\\$name\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon`" /v `"AutoAdminLogon`" /t REG_DWORD /d 0x1 /F`"sleep 5psexec \\$name "REG.EXE" ADD `"\\$name\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon`" /v `"AutoLogonCount`" /t REG_DWORD /d 0x1 /F`"Write-Host "Cloning"New-VM -Name $newname -VM $vm -Location $folder -Datastore $datastore -VMHost $vmhost -DiskStorageFormat $formatWrite-Host "Unplugging NIC..."get-VM $newname | get-networkadapter | set-networkadapter -startconnected:$false -confirm:$falseWrite-Host "Starting VM..."start-vm $newnameWrite-Host "Powering on clone..."Remove-Item "\\$name\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\remove.cmd"Write-Host "We need to wait until the clone is powered off before we can template it..."do {sleep 1.0Write "Waiting for shutdown of VMs"} until (Get-VM $newname | Where { $_.PowerState -eq "poweredOff" })write-host "System is powered on"Write-Host "Creating NIC..."get-VM $newname | get-networkadapter | set-networkadapter -startconnected:$true -confirm:$falseWrite-Host "Removing any stale template-VM's"remove-template $templatename -confirm:$falseWrite-Host "Creating Template VM from clone"new-template -VM $newname -name $templatename -Location $folderWrite-Host "Removing Clone VM"remove-vm $newname -DeletePermanently -confirm:$false}}}
This script does the following:
1) Sets the inputs from a piped in object (get-vm VMTOTEMPLATE | create-template)
2) Sets a series of variables ($vm, $name, $newname, $date, $templatename, etc.)
3) We setup a startup script on the target server to make into a template that:
a) Removes the computer from the domain
b) renames the computer to a generic name (XATEMPLATE)
c) Adds registry keys that will allow sysprep to run
d) Configures XenApp to “Image” mode
e) Shuts itself down once running the script is complete
f) deletes the script from running on startup
4) We then set the target to autologin with the local admin user name and password so the startup script in step 3 will be run
5) Begins the cloning by making a new-vm with the target machine
6) We unplug the NIC from VMWare so that when it starts up the script won’t actually remove the machine from the domain, but will remove itself from the domain
7) start the clone
8) the PowerCLI will now wait till the machine turns itself off…
9) Then it will reconnect the NIC, remove any stale templates and then makes a new template and then removes the clone VM.
Done! 🙂
Read More