1

This may be a weird question. But i need to do this .

Actually we are trying to execute UItests as part of Continuous integration purpose. Problem : When the testagent machine is idle for sometime, desktop gets locked and tests starts failing. We cannot remove security feature which prevents system from getting locked. IT is a violation of our company policy.

Our Solution We will check if system (windows 8 / 2008) is idle at regular intervals like 3 mins or so,and if it is idle, then we will trigger a vbs script to perform clicking of num key. And after 3 minutes again we check if system is idle and decide. Whole objective is to prevent system from getting locked.

You may wonder whether this is also a security violation. When security aduit happens, they will see whether any windows feature is disbled and not whether we are running scripts in background to keep system active.

So any one knows how to check via batch file / C# to achieve the above scenario. Anyother working suggestion also welcomed. Helps appreciated.

Thanks, VVP

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
VVP
  • 766
  • 4
  • 14
  • 39
  • 1
    Why not just click NumLock every three minutes regardless of whether the machine is idle? (A better choice would be ScrollLock. Nobody uses that key.) – Raymond Chen Aug 21 '15 at 06:14
  • There might be some useful information [here](http://stackoverflow.com/questions/17982409/simulate-a-keypress-for-x-seconds). – Micke Aug 21 '15 at 06:33
  • Thanks @RaymondChen Hitting scroll lock / num lock is ok. But i thought i can get a clean way, then good. But seems like it wont matter much and scroll lock wont interfere with tests and perhaps safe among keys.:) Thanks, – VVP Aug 21 '15 at 07:18
  • Thanks @Micke The link has lots of info.will explore them. Thanks, VVP – VVP Aug 21 '15 at 07:24
  • Call [SendInput](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396) to synthesize a keystroke at a very low level. I don't think posting a keyboard message like @Micke suggested will reset the system idle. To determine amount of time system has been idle, call [GetLastInputInfo](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646302(v=vs.85).aspx) – David Ching Aug 21 '15 at 21:17
  • @DavidChing : Thanks for the input. Will try it out. – VVP Aug 28 '15 at 06:27
  • I will put the vbs file which invokes scroll key in startup. But it is opening up a command prompt. Any idea how to make it run in background. I tried setting obj.Visible=False in vbs file. But not working. Any setting we can make inside vbs file to run in background? – VVP Aug 28 '15 at 06:28
  • I got it in Windows 8.Added a line at end. Set wsc_obj = Nothing But not working for Windows 2008 Server . Any idea how to make it work for both version? – VVP Aug 28 '15 at 06:43
  • You might want to add a vbs tag to your question. – David Ching Aug 28 '15 at 14:25

1 Answers1

1

You can try this code using sendkeys in vbscript that Simulate the click NumLock every 3 minutes :

Option Explicit
If AppPrevInstance() Then 
    MsgBox "There is an existing proceeding !" & VbCrLF &_
    CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
    WScript.Quit   
Else  
Dim ws : Set ws = CreateObject("WScript.Shell")
Do
    ws.SendKeys "{NUMLOCK}"
    Pause 3 ' To sleep for 3 minutes
Loop
End If
'********************************************************************************************
Sub Pause(min)
    Wscript.Sleep(1000 * 60 * min)
End Sub
'*********************************************************************************************
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
            " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function    
'*********************************************************************************************
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'*********************************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70