Multi-thread reboot script

The situation: My organization runs many hundred virtual Citrix servers and we're having an issue with users logging in to those servers before our remote printing client, ThinPrint, has a chance to load.

Due to various issues too numerous to list, we have a 48 hour reboot cycle for these servers. At some point after a server has been up for 48 hours, we have logic built into the Citrix XenCenter console to disallow logins on a server and when the servers reach 0 user load they restart. This restart is controlled via a PowerShell script that runs every 15 minutes and checks to make sure the servers have 0 users, and if they do simply runs a shutdown.exe command. When the server comes back up it starts to let users back on, but the ThinPrint services often aren't done loading yet and said user will get an error if they try to print. Through the Citrix Powershell cmdlets I have the ability to disallow or allow logins, and I was thinking of how to implement this in either our existing reboot script or have another script that gets passed the servers that do reboot and then waits until they're up for 5 minutes and sets the allowlogins flag.

Any thoughts on how to best implement this?

T

August 27th, 2015 8:54am

You could possibly create a start up script, so when the server starts back up, disallow any users to log on, until the ThinPrint service is fully up. Now the question would be, how do you tell if ThinPrint is fully up, if you detect that the service is "Started" is that all you need, or even though the service is started, is there more to the service to be fully initialized?
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 9:03am

I might have to go a different route, the head Citrix guy here says that it's a few central XenServers that's controlling the hundreds of app/presentation servers and handles whether or not the app servers allow people on or not, but, I think I've come up with something:

In our restart script -which runs on the few central XenServer "controllers", add a few lines that creates and then starts another script on the fly. The newly created script would then wait until the restarted server comes back up (do start-sleep while -test-connection- is null). It would then wait for the ThinPrint service to be active (do start-sleep while $service.status -eq "stopped") and once that's done, set the login mode (set-XAServerLogOnMode AllowLogins). Once that's done and the script terminates, delete the newly created script.

August 27th, 2015 10:15am

Sounds like a plan, let us know how it goes.
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 10:27am

So I have a text file named tp_staging.txt with this:

start-sleep -Seconds 60
$server_ping = test-connection -count 1 $server -ErrorAction SilentlyContinue

DO
    {
    write-host "Server not booted, sleeping for 5" -ForegroundColor Red
    start-sleep 5
    $server_ping = test-connection -count 1 $server -ErrorAction SilentlyContinue
    } While ($server_ping -eq $null)
Write-Host "Exited out of ping loop" -ForegroundColor Green

$server_Thinprint_service =  Get-Service -ComputerName $server -Name TPAutoConnSvc -ErrorAction SilentlyContinue
DO
    {
    write-host "Server booted, checking for Service and sleeping for 5" -ForegroundColor Red
    start-sleep 5
    $server_Thinprint_service =  Get-Service -ComputerName $server -Name TPAutoConnSvc -ErrorAction SilentlyContinue
    } While ($server_Thinprint_service.Status -eq "Stopped")
Write-Host "Exited out of get-service loop" -ForegroundColor Green

Set-XAServerLogOnMode -ServerName $server -LogOnMode AllowLogOns

I then wrote a few lines and stuck them in a foreach loop (foreach $servername in $servers) that calls that text file and builds a script out of it:

$script_path = "c:\powershell"
$script_name = $script_path + "\" + $servername + "_." + "ps1"
$script_header = '$server' + "=" + """$servername"""
$script_base = get-content C:\Powershell\tp_staging.txt
$script_cleanup = "remove-item " + """$script_name"""

$script = $script_header + ($script_base | Out-String) + $script_cleanup

$script | Out-File $script_name

Invoke-Expression $script_name
It's worked in the few test cases I've tried it out in.

August 27th, 2015 12:39pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics