Author Posts

February 29, 2016 at 11:15 am

Hello All,

First of all, I want to excuse myself, I'm a total beginer using powershell.

I'm reading and learning as I write this script. I downloaded a script which was too complicated for me, and it was strring another direction. I'm writint this script to reboot XenApp servers that has a 'Zero' load on them. My difficult part and I can't find much resources online is on how to report the status of the servers that has not been rebooted. I'm attaching the script below:

————————————————————————————————————————
Add-PSSnapin CITRIX*

#select the Controller
Set-XADefaultComputerName ZDC

#variable for Workergroups
$Workergroup = get-xaworkergroup "Workergroup"

#select servers
$InitialReboot = Get-XAWorkerGroupServer -WorkerGroupName $Workergroup

#disable login
$InitialReboot | Set-XAServerLogOnMode -LogOnMode ProhibitNewLogOnsUntilRestart

#reboot servers with 0 load
$InitialReboot | Get-XAServerLoad | where load -eq 0
Restart-Computer $InitialReboot.servername -ErrorAction SilentlyContinue

start-sleep -s 14400

#second reboot
#select servers with logon disabled
$CycleReboot = Get-XAWorkerGroupServer -WorkerGroupName $Workergroup | get-xaserver | Where-Object {$_.logonmode -eq "ProhibitNewLogOnsUntilRestart"}
$CycleReboot | ft -AutoSize

#list servers with 0 load

$CycleReboot | Get-XAServerLoad | where load -eq 0
Restart-Computer $Cyclereboot.servername -ErrorAction SilentlyContinue

——————————————————————————————————————

So the script is suppose is suppose to be simple and performed the following:
1) Select all servers in a workergroup
2) Disable login until server restart
3) Reboot all servers that has zero load
4) Time-freeze for 5 hrs
5) reboot the servers that has "logon disable' and Zero load

Missing step
6) email report of all servers that have not been rebooted

How can I write the script to email a report of all the servers that has not been rebooted?

March 1, 2016 at 8:30 am

The Send-MailMessage command can be used to send an e-mail. You'll obviously need to implement some way of tracking which servers have, and have not, been rebooted, so you have something to send in the message.

If you're concerned about servers that "failed" to Reboot, you're going to have to make some significant changes to your script. Right now, you're having Restart-Computer fail silently, which means when it fails, you have no way of knowing. That's what "SilentlyContinue" means. Instead, you'd probably need to error-handle a failed restart.

But Restart-Computer doesn't always KNOW when a remote machine has failed to restart. I'm not sure what your criteria is, or what might go wrong in your environment to prevent a restart. Perhaps reaching out to the computers and looking for an event log message that confirms a reboot recently took place?

March 3, 2016 at 1:45 pm

To be honest, I don't understand why you are trying to achieve this with PS. Even thought we all love the tool I would still prefer Citrix group policy extension to do that. Set nightly reboots and inform users x amount of times at interval of y to restart their application.

It is more or less bullet proof way to handle xenapps. Just check which version of extension you are running. There was/is a major bug in one of the 1.7.x versions. 1.7.7 is a fixed version.

Here is few lines that I used couple weeks ago when I had to mass reboot servers after upgrading golden images.

$XAs = (Get-ADComputer -fi {name -like "xa65*"}).name 
$objects = @(); foreach ($xa in $xas) {
                        $RDPCount = qwinsta /server:$xa | findstr ica-tcp#
                        
                        

                        $properties = @{
                                        'Server'=$xa
                                        'Logged in'=$RDPCount.count
                                        }

                        $object = New-Object –TypeName PSObject –Prop $properties
                        $objects += $object
                        
                        
                        } $objects | ft -AutoSize
$reboots = $objects | where {$_."Logged in" -eq 0} ; $reboots

foreach ($server in $reboots) {invoke-command -ComputerName $server.server -ScriptBlock {shutdown /r /t 0 /f}}

March 4, 2016 at 8:04 am

Don Jones – Currently I'm running a seperate script that shows the uptime of a server and filter it by a set amount of time. So if the server is not run past a set time, it will send out an email. I'm hoping to incorporate both scripts into one.

Aapeli Hietikko – Our clients have a strong hold on how the environment needs to run and should run, and we are stuck making miracles. We can't put any reboot policies because users cannot be booted off from their Citrix sessions, which leaves us to reboot servers that are available. We can't throw more servers to segment groups of servers that could schedule a reboot, because management don't want to spend more money, which leaves us with finding a time where we have the lease amount of users and reboot servers that are empty, but cannot boot any users off that have current sessions. That's why I'm writing this script, and learning Powershell on my own.

March 4, 2016 at 1:32 pm

That is fair enough reason and I can feel the pain 🙂

I don't believe always to the server loads as there might be an idle user and server load goes down to 0. Going after the rdp session count got me through a similar situation you are in. It was one time job.

You seem to have just about all together. If you paste here what you have then we can for sure make it work like you want.

You could write to file last execution time and then validate server reboots against that time or maybe we can even dig out when the script was run by task scheduler.