Saturday, April 7

Citrix STOP & START scripts

Situation:

Client couldn't change Citrix scheduled reboot to match with the Patching window and hence we were looking out for an option that would not only notify the connected users to logoff, that will first disable the logon and then notify the users and then logoff their sessions after 15 mins (3 reminders after every 5 mins).



1) Used STOP script to disabled the logon, notify and log off user followed with patching activity & server reboot

@echo off
REM ******************************************************************************
REM    File:        CitrixStop.bat
REM    Purpose:    This script will disable remote logins into the server, send out
REM             message to all logged in users, and then terminate all logged in
REM             sessions prior to patching beginning.
REM ******************************************************************************

REM Disable remote logins
CHANGE LOGON /DISABLE

REM Message to logged in users
MSG * "Please save your work and logoff your session for maintenance. Your session will be automatically logged off in 15 minutes"

REM 5 minute pause
TIMEOUT /T 300 /NOBREAK

REM Message to logged in users
MSG * "Please save your work and logoff your session for maintenance. Your session will be automatically logged off in 10 minutes"

REM 5 minute pause
TIMEOUT /T 300 /NOBREAK

REM Message to logged in users
MSG * "Please save your work and logoff your session for maintenance. Your session will be automatically logged off in 5 minutes"

REM 5 minute pause
TIMEOUT /T 300 /NOBREAK

REM Log out all logged in users
QUERY SESSION > session.txt
FOR /F "skip=2 tokens=3," %%i IN (session.txt) DO logoff %%i
TIMEOUT /T 30 /NOBREAK
DEL session.txt
TIMEOUT /T 5 /NOBREAK
EXIT

2) Post reboot, used START script to check the status of Citrix IMA service and enable logon. If service fails keep logon disabled. (You can check any service or can use same script for non-citrix boxes as well)

'******************************************************************************
'* File:     CitrixStart.vbs
'* Purpose:  This script will check to make sure key Citrix related services
'*             are started after patching.  If they aren't then an attempt to
'*             start them will take place.  If they are running then login into
'*             server will be enabled.
'******************************************************************************

Option Explicit

'*******************
'* Define Constants
'*******************

Const ForWriting = 2

'********************
'* Declare Variables
'********************

Dim objNetwork, objWMIService, objService, objFSO
Dim objWriteFile
Dim WshShell
Dim colListOfServices
Dim strComputer, strLogFile, strCommand
Dim arrService (2)
Dim bWaitOnReturn
Dim i, j

Set objNetwork = WScript.CreateObject("WScript.Network")

'* Get Server name.
strComputer = objNetwork.ComputerName
'strComputer = "."
'WScript.Echo strComputer

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'* Assigning the service name to the arrary
arrService (0) = "IMAService"
arrService (1) = "CtxHttp"
arrService (2) = "CitrixXTEServer"

'* Setting i equal to zero
i = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")

strLogFile = "C:\Program Files\Hewlett Packard\opsware\start-app\CitrixStart.txt"

Set objWriteFile = objFSO.CreateTextFile(strLogFile, ForWriting)

'* For loop for going through the array of services
For Each j In arrService
    Set colListOfServices = objWMIService.ExecQuery _
        ("Select * FROM Win32_Service WHERE Name='" & arrService (i) & "'")
    For Each objService in colListOfServices
      
        '* Checking the state of the service. If the service is running, then
        '* continue to the next service in the array. If the service isn't
        '* running, then start it.
        objWriteFile.WriteLine "Checking " & objService.DisplayName
        If objService.State = "Stopped" Then
            objWriteFile.WriteLine objService.DisplayName & " " & objService.State
            'WScript.Echo strMessage
            'WScript.Echo "The service isn't running"
            objService.StartService()
          
            '* Allow the script to sleep for 30 seconds.
            WScript.Sleep (30 * 1000)
        End If
        '* Checking the state of the service again.  If the service isn't
        '* running then the script will email the proper group and leave
        '* logins disabled on the server.
        If objService.State = "Stopped" Then
            objWriteFile.WriteLine "The " & objService.DisplayName & " will not start, and logins on this server will remain disabled."
            WScript.Quit
        End If
    Next
    i = i + 1
Next

'* WshShell object is used whenever you want to run a program locally,
'* manipulate the contents of the registry, create a shortcut, or
'* access a system folder.
Set WshShell = WScript.CreateObject("WScript.Shell")

strCommand = "CHANGE LOGON /ENABLE"
bWaitOnReturn = True
WshShell.Run(strCommand, bWaitOnReturn)
objWriteFile.WriteLine "Logins have been enabled."

objWriteFile.Close

Set WshShell = Nothing
Set objWMIService = Nothing
Set colListOfServices = Nothing
Set objWriteFile = Nothing
Set objFSO = Nothing
WScript.Quit

No comments:

Post a Comment