简体   繁体   中英

Optimizing script to create Citrix farm policies is running too slow

With help from the community I've finally completed a base script that will create worker groups, policies, policy settings and then link the policies to the worker groups.

I have many write-hosts just to see whats going and to see where the script is holding up the most, will be removed down the road.

When it sets the set-ctxgrouppolicyconfiguration settings, it's sluggish. The script takes about 8-10 min to complete which is very slow. Citrix app center is inherently slow when manually writing each policy to the SQL backend, so not sure if this can be sped up at all.

import-module Citrix.GroupPolicy.Commands 
add-pssnapin *Citrix* 

$count = 0
$tomorrow = ConvertTo-DwordDate -date (get-date).AddDays(1)
new-xafolder 'WorkerGroups/reboot schedules'

$days = @("mon", "tues", "wed", "thurs", "fri", "sat", "sun")
foreach ($day in $days) {
    write-host "processing $day"
    $count = $count + 1

    write-host "creating workerGroup"
    new-xaworkergroup "$day - 3am" -folderpath 'WorkerGroups/reboot schedules' -description "scheduled reboot for $day - 3am"

    write-host "creating policy"
    new-ctxgrouppolicy "reboot $day 3am" -type computer -priority $count

    write-host "linking policy to workerGroup"
    add-ctxgrouppolicyfilter "reboot $day 3am" computer servergroup0 workergroup "$day - 3am"

    write-host "setting reboot parameters"
    set-ctxgrouppolicyconfiguration "reboot $day 3am" computer scheduledReboots enabled
    set-ctxgrouppolicyconfiguration "reboot $day 3am" computer rebootWarningMessage enabled
    set-ctxgrouppolicyconfiguration "reboot $day 3am" computer rebootWarningStartTime enabled  -value start60MinutesBeforeReboot
    set-ctxgrouppolicyconfiguration "reboot $day 3am" computer rebootWarningInterval enabled   -value every10Minutes
    set-ctxgrouppolicyconfiguration "reboot $day 3am" computer rebootScheduleTime enabled      -value 180
    set-ctxgrouppolicyconfiguration "reboot $day 3am" computer rebootScheduleFrequency enabled  -value 7
    set-ctxgrouppolicyconfiguration "reboot $day 3am" computer rebootDisableLogOnTime enabled   -value disable15MinutesBeforeReboot
    set-ctxgrouppolicyconfiguration "reboot $day 3am" computer rebootScheduleStartDate enabled  -value $tomorrow
}
$count = $count + 1
set-ctxgrouppolicy -policyname unfiltered -type computer -priority $count

write-host "script complete"

#------------------------------
#functions for date conversion
#------------------------------

function convertFrom-DwordDate([int32]$DwordValue){
    #ex. $DwordValue = 132055314
    #convert to hex
    $hex = $DwordValue.ToString('X8')
    #$Ex. $hex = 0x07df0112 = 0x07df (year) 0x01(month) 0x12 (day)

    #Convert to date string
    $datestring = '{0:D4}\{1:D2}\{2:D2}' -f [convert]::ToUInt32($hex.Substring(0,4),16), [convert]::ToUint32($hex.Substring(4,2),16), [convert]::ToUInt32($hex.Substring(6,2),16)
    #convert to datetime and output
    $datetime = [datetime]::ParseExact($datestring,'yyyy\\MM\\dd',$null)
    #output
    $datetime
}

function ConvertTo-DwordDate([datetime]$Date) {
    #convert to combined hex
    $combinedhex = '{0:X}{1:X2}{2:X2}' -f $Date.Year, $Date.Month, $Date.Day
    #convert to decimal
    $decimal = [convert]::ToUInt32($combinedhex,16)
    #output
    $decimal
}

I'm not familiar with the Citrix SDK, but looking at this site , it seems that you could recieve the whole policy as an object. I would suggest doing that so you could modify it locally and only save the GPO once at the end of each "day" in your loop. That would replace 8 save-calls with 1. Something like this:

CAUTION: NOT TESTED! Read through and try it in a test environment first. As said, I don't have experience with this module and can not guarantee that it works as expected or at all. You might also want to try running the commands manually first and not the whole loop to minimize the possible "damage".

import-module Citrix.GroupPolicy.Commands 
add-pssnapin *Citrix* 

$count = 0
$tomorrow = ConvertTo-DwordDate -date (get-date).AddDays(1)
new-xafolder 'WorkerGroups/reboot schedules'

#Not sure if needed or if you could remove this and -DriveName CitrixGPO commands later
#Add PowerShell snapins (if necessary)
if ( (Get-PSSnapin -Name Citrix.Common.GroupPolicy -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin Citrix.Common.GroupPolicy }
if ( (Get-PSSnapin -Name Citrix.Common.Commands -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin Citrix.Common.Commands }
if ( (Get-PSSnapin -Name Citrix.XenApp.Commands -ErrorAction SilentlyContinue) -eq $null ) { Add-PSSnapin Citrix.XenApp.Commands }
New-PSDrive -Name CitrixGPO -PSProvider CitrixGroupPolicy -Root \ -DomainGPO "Citrix GPO"
#END "not sure if needed"


$days = @("mon", "tues", "wed", "thurs", "fri", "sat", "sun")
foreach ($day in $days) {
    write-host "processing $day"
    $count = $count + 1

    write-host "creating workerGroup"
    new-xaworkergroup "$day - 3am" -folderpath 'WorkerGroups/reboot schedules' -description "scheduled reboot for $day - 3am"

    write-host "creating policy"
    new-ctxgrouppolicy "reboot $day 3am" -type computer -priority $count

    write-host "linking policy to workerGroup"
    add-ctxgrouppolicyfilter "reboot $day 3am" computer servergroup0 workergroup "$day - 3am"

    write-host "getting policy"
    $objCitrixPolicy = Get-CtxGroupPolicyConfiguration -PolicyName "reboot $day 3am" -Type compuer -DriveName CitrixGPO #-DriveName CitrixGPO might not be needed

    write-host "modifying policy"
    $objCitrixPolicy.("scheduledReboots").State = "Enabled"

    $objCitrixPolicy.("rebootWarningMessage").State = "Enabled"

    $objCitrixPolicy.("rebootWarningStartTime").State = "Enabled"
    $objCitrixPolicy.("rebootWarningStartTime").Value = "start60MinutesBeforeReboot"

    $objCitrixPolicy.("rebootWarningInterval").State = "Enabled"
    $objCitrixPolicy.("rebootWarningInterval").Value = "every10Minutes"

    $objCitrixPolicy.("rebootScheduleTime").State = "Enabled"
    $objCitrixPolicy.("rebootScheduleTime").Value = 180

    $objCitrixPolicy.("rebootScheduleFrequency").State = "Enabled"
    $objCitrixPolicy.("rebootScheduleFrequency").Value = 7

    $objCitrixPolicy.("rebootDisableLogOnTime").State = "Enabled"
    $objCitrixPolicy.("rebootDisableLogOnTime").Value = "disable15MinutesBeforeReboot"

    $objCitrixPolicy.("rebootScheduleStartDate").State = "Enabled"
    $objCitrixPolicy.("rebootScheduleStartDate").Value = $tomorrow

    write-host "saving policy"
    Set-CtxGroupPolicyConfiguration $objCitrixPolicy -DriveName CitrixGPO #-DriveName CitrixGPO might not be needed

}
$count = $count + 1
set-ctxgrouppolicy -policyname unfiltered -type computer -priority $count

#Close PowerShell Drive from Citrix domain GPO
Remove-PSDrive -Name CitrixGPO

write-host "script complete"

#------------------------------
#functions for date conversion
#------------------------------

function convertFrom-DwordDate([int32]$DwordValue){
    #ex. $DwordValue = 132055314
    #convert to hex
    $hex = $DwordValue.ToString('X8')
    #$Ex. $hex = 0x07df0112 = 0x07df (year) 0x01(month) 0x12 (day)

    #Convert to date string
    $datestring = '{0:D4}\{1:D2}\{2:D2}' -f [convert]::ToUInt32($hex.Substring(0,4),16), [convert]::ToUint32($hex.Substring(4,2),16), [convert]::ToUInt32($hex.Substring(6,2),16)
    #convert to datetime and output
    $datetime = [datetime]::ParseExact($datestring,'yyyy\\MM\\dd',$null)
    #output
    $datetime
}

function ConvertTo-DwordDate([datetime]$Date) {
    #convert to combined hex
    $combinedhex = '{0:X}{1:X2}{2:X2}' -f $Date.Year, $Date.Month, $Date.Day
    #convert to decimal
    $decimal = [convert]::ToUInt32($combinedhex,16)
    #output
    $decimal
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM