1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| '##########################################################################################################################
'# Script Name : Auto-Lock_On_Idle_TimeOut.vbs #
'# Auto-Lock your workstation after Idle Timeout #
'# Verrouillage automatique de votre poste de travail après un délai d'inactivité #
'# Idea comes from here ==> This snippet is from http://stackoverflow.com/a/15846912 #
'# https://stackoverflow.com/questions/15845508/get-idle-time-of-machine/15846912#15846912 #
'# https://gist.github.com/wendelb/1c364bb1a36ca5916ca4 ===> Auto-Lock your workstation after Idle-Timeout with PowerShell#
'##########################################################################################################################
Option Explicit
Dim Copyright,Msg
Msg = Lang
Copyright = Msg(0) & ChrW(169) &" Hackoo 2020"
If AppPrevInstance() Then
MsgBox Msg(1) & VbCrLF & CommandLineLike(WScript.ScriptName),VbExclamation,Copyright
WScript.Quit
Else
Dim Timeout_Idle,strCommand,VbsPath,ShortcutName
Timeout_Idle = "15" '15 minutes
strCommand = "rundll32.exe user32.dll,LockWorkStation"
VbsPath = Wscript.ScriptFullName
ShortcutName = "Auto-Lock_On_Idle_TimeOut"
Call Shortcut(VbsPath,ShortcutName)
Call Write_Run_PScript(Timeout_Idle,strCommand)
End If
'---------------------------------------------------------------------------------------------------------------
Sub Shortcut(PathApplication,ShortcutName)
Dim objShell,StartFolder,objShortCut,MyTab
Set objShell = CreateObject("WScript.Shell")
MyTab = Split(PathApplication,"\")
If ShortcutName = "" Then
ShortcutName = MyTab(UBound(MyTab))
End if
StartFolder = objShell.SpecialFolders("Startup")
Set objShortCut = objShell.CreateShortcut(StartFolder & "\" & ShortcutName & ".lnk")
objShortCut.TargetPath = DblQuote(PathApplication)
ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,44"
objShortCut.Save
End Sub
'---------------------------------------------------------------------------------------------------------------
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'---------------------------------------------------------------------------------------------------------------
Sub Write_Run_PScript(Timeout_Idle,strCommand)
Const ForWriting = 2
Dim fs,Ws,ts,Ret,PSFile,ByPassPSFile,M
Set fs = CreateObject("Scripting.FileSystemObject")
Set Ws = CreateObject("WScript.Shell")
PSFile = Ws.ExpandEnvironmentStrings("%Temp%") & fs.GetTempName & ".ps1"
ByPassPSFile = "PowerShell -ExecutionPolicy bypass -noprofile -file "
Set ts = fs.OpenTextFile(PSFile,ForWriting,True)
ts.WriteLine "$idle_timeout = New-TimeSpan -Minutes "& Timeout_Idle &""
ts.WriteLine "Add-Type @'"
ts.WriteLine "using System;"
ts.WriteLine "using System.Diagnostics;"
ts.WriteLine "using System.Runtime.InteropServices;"
ts.WriteLine "namespace PInvoke.Win32 {"
ts.WriteLine " public static class UserInput {"
ts.WriteLine " [DllImport(""user32.dll"", SetLastError=false)]"
ts.WriteLine " private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);"
ts.WriteLine " [StructLayout(LayoutKind.Sequential)]"
ts.WriteLine " private struct LASTINPUTINFO {"
ts.WriteLine " public uint cbSize;"
ts.WriteLine " public int dwTime;"
ts.WriteLine " }"
ts.WriteLine " public static DateTime LastInput {"
ts.WriteLine " get {"
ts.WriteLine " DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount);"
ts.WriteLine " DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks);"
ts.WriteLine " return lastInput;"
ts.WriteLine " }"
ts.WriteLine " }"
ts.WriteLine " public static TimeSpan IdleTime {"
ts.WriteLine " get {"
ts.WriteLine " return DateTime.UtcNow.Subtract(LastInput);"
ts.WriteLine " }"
ts.WriteLine " }"
ts.WriteLine " public static int LastInputTicks {"
ts.WriteLine " get {"
ts.WriteLine " LASTINPUTINFO lii = new LASTINPUTINFO();"
ts.WriteLine " lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO));"
ts.WriteLine " GetLastInputInfo(ref lii);"
ts.WriteLine " return lii.dwTime;"
ts.WriteLine " }"
ts.WriteLine " }"
ts.WriteLine " }"
ts.WriteLine "}"
ts.WriteLine "'@"
ts.WriteLine "$locked = 0;"
ts.WriteLine "Do {"
ts.WriteLine " $idle_time = [PInvoke.Win32.UserInput]::IdleTime;"
ts.WriteLine " if (($locked -eq 0) -And ($idle_time -gt $idle_timeout)) {"
ts.WriteLine "$ws = New-Object -ComObject ""Wscript.Shell"""
ts.WriteLine "$Title = """ & Msg(0) & """"
ts.WriteLine "$Answer = $ws.Popup(""" & Msg(2) & """ +"
ts.WriteLine """" & Msg(3) & """,""20"",$Title,48+4+4096)"
ts.WriteLine "if ($Answer -eq 7) # Si l'utilisateur clique sur le bouton No "
ts.WriteLine "{"
ts.WriteLine " $locked = 0;"
ts.WriteLine "}"
ts.WriteLine "else # Sinon la session va être verouillé au bout de 20 secondes si l'utilsateur ne clique sur aucun bouton"
ts.WriteLine "{"
ts.WriteLine " "& strCommand &""
ts.WriteLine " $locked = 1;"
ts.WriteLine "}"
ts.WriteLine " }"
ts.WriteLine " if ($idle_time -lt $idle_timeout) {"
ts.WriteLine " $locked = 0;"
ts.WriteLine " }"
ts.WriteLine " Start-Sleep -Seconds 10"
ts.WriteLine "}"
ts.WriteLine "while (1 -eq 1)"
ts.Close
Ret = Ws.run(ByPassPSFile & PSFile,0,True)
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
'----------------------------------------------------------------------------------------------------------------
Function OSLang()
Dim dtmConvertedDate,strComputer,objWMIService,oss,os
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each os in oss
OSLang = os.OSLanguage
Next
End Function
'----------------------------------------------------------------------------------------------------------------
Function Lang()
Dim MsgFR,MsgEN
MsgFR = Array("Verrouillage automatique en cas d'inactivité " ,"ATTENTION ! Il y a une autre instance en cours d'exécution !",_
"Attention ! Votre Session va être verrouillé dans 20 secondes !`n`n Voulez-vous confirmer le Verrouillage de votre session ?","`n`n OUI ou NON ?")
MsgEN = Array("Automatic Lock Session On Idle TimeOut ","ATTENTION ! There is another instance running !",_
"Warning ! Your Session will be locked in 20 seconds !`n`n Do you want to confirm the Locking of your session ?","`n`n YES or NO ?")
'Vérifiez si le système est français pour définir le tableau français comme message, sinon définissez-le comme anglais
'Check if the system is french to set the French array as message otherwise set it as English
If Oslang = 1036 Then
Lang = MsgFR ' French Array Message to be set
Else
Lang = MsgEN ' English Array Message to be set
End If
End Function
'---------------------------------------------------------------------------------------------------------------- |
Partager