so I have an INI file called New-VDSHSDProfileDeletion which contains the following;
[pre][State]
Enabled=True
DeleteNotUsedDays=1
[Exclusions]
Example=xxx;xxx;xxx
ExcludedDeliveryGroups=
ExcludedServers=
ExcludedDays=[/pre]
and a script which parses and does conditional checks using If…Then but looks messy. Is there a better way to code that portion?
Function New-VDSHSDProfileDeletion {
#Setup default variables
$VDSHSDProfileDeletionINI = "$env:ctxadmin\EnvironmentSpecific\Functions\New-VDSHSDProfileDeletion\New-VDSHSDProfileDeletion.ini"
#Get Delivery Group from Local Server
$RegDG = (Get-ItemProperty -path 'HKLM:\software\citrix\virtualDesktopAgent\State').DesktopGroupName
#get current date and conver to current day
$date = Get-Date -format "dddd"
$StrDayofweek = $date.ToString()
Write-Verbose "INFO: Check event source exists, if not create it"
if ([System.Diagnostics.EventLog]::SourceExists("New-VDSHSDProfileDeletion") -eq $False) {New-EventLog -LogName XenAppAdmin -Source New-VDSHSDProfileDeletion}
Write-Verbose "INFO: Get desktop group name from registry"
$DesktopGroupName = (Get-ItemProperty HKLM:\SOFTWARE\Citrix\VirtualDesktopAgent\State -Name DesktopGroupName).DesktopgroupName
Write-Verbose "INFO: DesktopGroupName is $DesktopGroupName"
Write-Verbose "INFO: Get INI file contents and read in variables"
if ($(Test-Path $VDSHSDProfileDeletionINI) -eq $True) {
$INIFile = Get-IniContent $VDSHSDProfileDeletionINI
}
else {
Write-Verbose "ERROR: $VDSHSProfileDeletionINI did not exist."
Write-EventLog -LogName XenAppAdmin -Source New-VDSHSDProfileDeletion -EntryType Error -EventId 1016 -Message "$VDSHSDProfileDeletionINI did not exist."
Exit 1
}
#Setup INI file variable contents
if (($INIFile.State.Enabled)) {$DeletionsEnabled = $INIFile.State.Enabled}
if (($INIFile.State.DeleteNotUsedDays)) {$DeleteNotUsedDays = $INIFile.State.DeleteNotUsedDays}
if (($INIFile.Exclusions)) {$ExcludedDeliveryGroups = $INIFile.Exclusions.ExcludedDeliveryGroups -split ";"}
if (($INIFile.Exclusions)) {$ExcludedServers = $INIFile.Exclusions.ExcludedServers -split ";"}
if (($INIFile.Exclusions)) {$ExcludedDays = $INIFile.Exclusions.ExcludedDays -split ";"}
#Conditional check for exclusions and execution
If ($DeletionsEnabled -eq $true) {
if ($ExcludedDays.toupper() -contains $strdayofweek.toupper()) {
write-host "$dayofweek Day is excluded from running Profile Deletions"
Write-EventLog -LogName XenAppAdmin -Source New-VDSHSDProfileDeletion -EntryType Information -EventId 1000 -Message "$dayofweek Day is excluded from running Profile Deletions"
Return 0
}
else {
if (($ExcludedServers -contains ($env:COMPUTERNAME).toupper()) -or ($ExcludedDeliveryGroups -contains $RegDG.ToUpper())) {
if ($ExcludedServers -contains $env:COMPUTERNAME) {
write-host "$env:COMPUTERNAME is excluded from running Profile Deletions"
Write-EventLog -LogName XenAppAdmin -Source New-VDSHSDProfileDeletion -EntryType Information -EventId 1000 -Message "$env:COMPUTERNAME is excluded from running Profile Deletions"
}
if ($ExcludedDeliveryGroups -contains $RegDG) {
write-host "$RegDG is excluded from runnning Profile Deletions against this Delivery Group"
Write-EventLog -LogName XenAppAdmin -Source New-VDSHSDProfileDeletion -EntryType Information -EventId 1000 -Message "$RegDG is excluded from runnning Profile Deletions against this Delivery Group"
}
Return 0
}
else {
write-host "Delivery Group $RegDG and Server $env:COMPUTERNAME check passed. Executing Delprof2 command..."
$DelProf2 = Start-NewCommand -commandTitle "DelProf2" -commandPath "$env:ctxadmin\Administration\HSDDailyMaintenance\VDSModules\Tools\DelProf2.exe" -commandArguments "/d:$DeleteNotUsedDays /u"
if ($delprof2.stderr.Length -gt 0) {
Write-EventLog -LogName XenAppAdmin -Source New-VDSHSDProfileDeletion -EntryType Error -EventId 1001 -Message $delprof2.stderr
Return 1
}
else {
Write-EventLog -LogName XenAppAdmin -Source New-VDSHSDProfileDeletion -EntryType Information -EventId 1000 -Message $delprof2.stdout
Return 0
}
}
}
}
Else {
Write-host "Profile deletions disabled"
Write-EventLog -LogName XenAppAdmin -Source New-VDSHSDProfileDeletion -EntryType Information -EventId 1002 -Message "Profile deletions disabled"
Return 0
}
}