-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcitrix_autodeploy.ps1
131 lines (113 loc) · 8.59 KB
/
citrix_autodeploy.ps1
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
try {
Add-PSSnapin Citrix.*
}
catch {
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "Citrix Powershell snapins not installed." -EntryType Error -EventId 1
Exit 1
}
function Import-ConfigFile {
try {
if (Test-Path (Join-Path $PSScriptRoot citrix_autodeploy_config.json)) {
$Config = Get-Content (Join-Path $PSScriptRoot citrix_autodeploy_config.json) | ConvertFrom-Json
}
}
catch {
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "$($Error[0].ToString())`r`n`r`n $($Error[0].ScriptStackTrace.ToString())" -EntryType Error -EventId 1
throw $Error[0]
}
return $Config
}
# Check if our custom event log exists
if ((Get-EventLog -List).Log -notcontains 'Citrix Autodeploy') {
throw 'Event log not found.'
}
Write-Verbose 'Loading config ...'
$Config = Import-ConfigFile
foreach ($AutodeployMonitor in $Config.AutodeployMonitors.AutodeployMonitor) {
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "Autodeploy job started: $(($AutodeployMonitor | Format-List | Out-String))" -EventId 0 -EntryType Information
try {
$AdminAddress = $AutodeployMonitor.AdminAddress
$BrokerCatalog = Get-BrokerCatalog -AdminAddress $AdminAddress -Name $AutodeployMonitor.BrokerCatalog -ErrorAction Stop
$DesktopGroupName = Get-BrokerDesktopGroup -AdminAddress $AdminAddress -Name $AutodeployMonitor.DesktopGroupName -ErrorAction Stop
$UnassignedMachines = Get-BrokerDesktop -AdminAddress $AdminAddress -DesktopGroupName $DesktopGroupName.Name -IsAssigned $false -ErrorAction Stop
$MachinesToAdd = $AutodeployMonitor.MinAvailableMachines - $UnassignedMachines.Count
$PreTask = $AutodeployMonitor.PreTask
$PostTask = $AutodeployMonitor.PostTask
}
catch {
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "$($Error[0].ToString())`r`n`r`n $($Error[0].ScriptStackTrace.ToString())" -EntryType Error -EventId 1
throw $Error[0]
break
}
if ($MachinesToAdd -ge 1) {
while ($MachinesToAdd -ne 0) {
try {
if ($PreTask) {
try {
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "Executing pre-task `'$($AutodeployMonitor.PreTask)`' for desktop group `'$($AutodeployMonitor.DesktopGroupName)`'" -EventId 5 -EntryType Information
$PreTaskOutput = & $PreTask
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "Pre-task output`r`n`r`n$PreTaskOutput" -EventId 7 -EntryType Information
}
catch {
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "Error occured in pre-task for desktop group $($AutodeployMonitor.DesktopGroupName)`r`n`r`n$($Error[0].ToString())`r`n`r`n $($Error[0].ScriptStackTrace.ToString())" -EntryType Error -EventId 1
}
}
$Logging = Start-LogHighLevelOperation -AdminAddress $AdminAddress -Source "Powershell Autodeploy" -StartTime $([datetime]::Now) -Text "Adding 1 Machines to Machine Catalog `'$($BrokerCatalog.Name)`'"
$IdentityPool = Get-AcctIdentityPool -AdminAddress $AdminAddress -IdentityPoolName $BrokerCatalog.Name
$IdentityPoolLockedTimeout = 60
# Check if identity pool is already locked, and if it is, wait for it to be unlocked.
# This may occur if an admin has created machines in Citrix Studio while this script is running.
if ($IdentityPool.Lock) {
$Stopwatch = [Diagnostics.Stopwatch]::StartNew()
while ($IdentityPool.Lock -and $Stopwatch.Elapsed.Seconds -le $IdentityPoolLockedTimeout) {
Start-Sleep -Seconds 1
}
$Stopwatch.Stop()
}
Set-AcctIdentityPool -AdminAddress $AdminAddress -AllowUnicode -Domain $IdentityPool.Domain -IdentityPoolName $IdentityPool.IdentityPoolName -LoggingId $Logging.Id
$NewAdAccount = New-AcctADAccount -AdminAddress $AdminAddress -Count 1 -IdentityPoolName $IdentityPool.IdentityPoolName -LoggingId $Logging.Id -ErrorAction Stop
$ProvScheme = Get-ProvScheme -AdminAddress $AdminAddress -ProvisioningSchemeName $BrokerCatalog.Name
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "Creating VM $($NewAdAccount.SuccessfulAccounts.ADAccountName.ToString().Split('\')[1].Trim('$')) in catalog `'$($BrokerCatalog.Name)`' and adding to delivery group `'$($DesktopGroupName.Name)`'" -EntryType Information -EventId 2
$NewVMProvTask = New-ProvVM -AdminAddress $AdminAddress -ADAccountName $NewAdAccount.SuccessfulAccounts -ProvisioningSchemeName $ProvScheme.ProvisioningSchemeName -RunAsynchronously -LoggingId $Logging.Id
$ProvTask = Get-ProvTask -AdminAddress $AdminAddress -TaskId $NewVMProvTask
$ProvTaskSleep = 15
while ($ProvTask.Active -eq $true) {
Start-Sleep -Seconds $ProvTaskSleep
$ProvTask = Get-ProvTask -AdminAddress $AdminAddress -TaskId $NewVMProvTask
}
if (-not($ProvTask.TerminatingError)) {
$NewBrokerMachine = New-BrokerMachine -AdminAddress $AdminAddress -MachineName $NewAdAccount.SuccessfulAccounts.ADAccountSid -CatalogUid $BrokerCatalog.Uid -LoggingId $Logging.Id
Add-BrokerMachine -AdminAddress $AdminAddress -InputObject $NewBrokerMachine -DesktopGroup $DesktopGroupName -LoggingId $Logging.Id
}
if ($PostTask) {
try {
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "Executing post-task `'$($AutodeployMonitor.PostTask)`' for machine `'$($NewBrokerMachine.MachineName)`' in desktop group `'$($AutodeployMonitor.DesktopGroupName)`'" -EventId 6 -EntryType Information
$PostTaskOutput = & $PostTask
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "Post-task output`r`n`r`n$PostTaskOutput" -EventId 8 -EntryType Information
}
catch {
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "Error occured in post-task for machine `'$($NewBrokerMachine.MachineName)`' in desktop group $($AutodeployMonitor.DesktopGroupName)`r`n`r`n$($Error[0].ToString())`r`n`r`n $($Error[0].ScriptStackTrace.ToString())" -EntryType Error -EventId 1
}
}
}
catch {
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "$($Error[0].ToString())`r`n`r`n $($Error[0].ScriptStackTrace.ToString())" -EntryType Error -EventId 1
Stop-LogHighLevelOperation -AdminAddress $AdminAddress -HighLevelOperationId $Logging.Id -EndTime $([datetime]::Now) -IsSuccessful $false
break
}
finally {
if (-not($Error)) {
Stop-LogHighLevelOperation -AdminAddress $AdminAddress -HighLevelOperationId $Logging.Id -EndTime $([datetime]::Now) -IsSuccessful $true
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message "Successfully created VM $($NewAdAccount.SuccessfulAccounts.ADAccountName.ToString().Split('\')[1].Trim('$')) in catalog `'$($BrokerCatalog.Name)`' and added it to delivery group `'$($DesktopGroupName.Name)`'" -EntryType Information -EventId 3
}
if ($IdentityPool.Lock) {
Unlock-AcctIdentityPool -AdminAddress $AdminAddress -IdentityPoolName $IdentityPool.IdentityPoolName -LoggingId $Logging.Id -ErrorAction SilentlyContinue
}
}
$MachinesToAdd--
}
} else {
$Message = "No machines needed for desktop group `'$($AutodeployMonitor.DesktopGroupName)`'`n`nAvailable machines: $($UnassignedMachines.Count)`nRequired available machines: $($AutodeployMonitor.MinAvailableMachines)`n`nAvailable machine names:`n$($UnassignedMachines.DNSName | Format-List | Out-String)"
Write-EventLog -LogName 'Citrix Autodeploy' -Source 'Citrix Autodeploy' -Message $Message -EventId 4 -EntryType Information
}
}