locked
Logon Verification Script RRS feed

  • Question

  • Does anyone have any recomendations for a script that will logoff users from a laptop if a file (allowLogon.txt) modification date is past the current date. I need to force laptop users on a domain to bring their laptops in every 90 days for wsus and antivirus defs.

    Tuesday, January 12, 2010 6:51 PM

All replies

  • You can use VBScript. For example something like this (copy and paste to Notepad and save the file with .vbs extension):
    Const LOG_OFF = 0
    Const FORCED_LOG_OFF = 4
    
    strFilePath = "c:\Temp\allowLogon.txt"
    strComputer = "."
    intNumberofDays = 90
    
    On Error Resume Next
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set File = GetObject("winMgmts:CIM_DataFile.Name='" & strFilePath & "'")
    
    strDateModified = File.LastModified
    
    WMIDateStringToDate = DateSerial(Left(File.LastModified, 4), Mid(File.LastModified, 5, 2), Mid(File.LastModified, 7, 2))
    
    intCountdays = DateDiff("d",WMIDateStringToDate,Now)
    
    If intCountDays > intNumberofDays Then 
    	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2") 
    	Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
    	For Each objOperatingSystem in colOperatingSystems
    		ObjOperatingSystem.Win32Shutdown(FORCED_LOG_OFF)
    	Next
    End If
    The script checks last date of the c:\Temp\allowLogon.txt file. If the modification date is older than intNumberofDays (90) then it forcefully logs the user off (forcefully meaning that it shuts down the programs even though they may display a prompt for user action - for example to save a file). If you want to check if the file was modified today just set intNumberofDays to 0.

    The script can be added to group policy logon scripts or to registry Run key etc. You may want to run it using the following command:
    %systemroot%\system32\wscript.exe /B /Nologo full_path_to_vbs_script
    %systemroot%\system32\wscript.exe /B /Nologo c:\Windows\script.vbs
    Wednesday, January 13, 2010 6:24 PM