Jump to content

Only Run Script At Login Screen (Windows 7)


 Share

Recommended Posts

Hi guys,

I've created a pretty crude script to try and get rid of old user profiles from our business computers - we've tried various Visual Basic Scripts but they don't seem to do the job very well.

Basically I only want to the script to run whilst at the login screen and stop when someone is logged in. I can't find a terribly successful way to determine if a user is logged in (the script will be running as Local System so determining the current user will be pointless). One thing I wondered is if explorer.exe is started whilst at the login screen so only run the script if that process doesn't exist - I'll need to test that one out.

My main question I suppose is does Autoit have any built-in method (or a dirty hack) of determining whether or not the PC is sat at the login screen or actually in a user session (we don't allow user switching so there's only ever one logged in user)?

My current bit of script if anyone is interested is...

While 1

sleep(500)

$loop = 1
$loop2 = 4

RunWait(@ComSpec & " /c " & " dir /b c:users > c:dl.txt", "", @SW_HIDE)
$file = FileOpen("c:dl.txt")

While 1
$fol = FileReadLine ($file ,$loop)
If $fol = "" Then ExitLoop
$donotdeletefol = "0"
If $fol = "administrator" Or $fol = "Public" Or $fol = "Default" Or $fol = "All users" Then $donotdeletefol = "1"
If $donotdeletefol = "0" Then DirRemove ( "c:users" & $fol ,1 )
$loop = $loop + 1
WEnd

FileClose("c:dl.txt")
If FileExists ("c:dl.txt") Then FileDelete ("c:dl.txt")

While 1
$SID = RegEnumKey("HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionProfileList", $loop2)
$ProfLoc = RegRead("HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionProfileList" & $SID, "ProfileImagePath")
If $SID = "" Then ExitLoop
$donotdeletereg = "0"
If $ProfLoc = "c:usersadministrator" Or $ProfLoc = "c:usersPublic" Or $ProfLoc = "c:usersDefault" Or $ProfLoc = "c:usersAll users" Then $donotdeletereg = "1"
If $donotdeletereg = "0" Then RegDelete ("HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionProfileList" & $SID)
$loop2 = $loop2 + 1
WEnd

WEnd
Edited by AmbientMike
Link to comment
Share on other sites

  • Moderators

Hi, AmbientMike. You can find if someone is logged on through WMI. I use something like this; you might be able to modify it for your needs:

$machine = "123456-IS"

Global $objWMIService = ObjGet("winmgmts:\\" & $machine & "\root\CIMV2")
If @error Then
     MsgBox(0, "", "No one is logged on.")
Else
     $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
         For $objItem in $colItems
         MsgBox(0, "", $objItem.username)
         Next
EndIf

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • 2 weeks later...

Tried that method and I didn't seem to get anywhere with it. I installed it as a service running it as Local System and nothing much happened...

While 1

Global $objWMIService = ObjGet("winmgmts:" & @ComputerName & "rootCIMV2")
If @error Then

FileOpen ( "c:testingtons.txt" ,1 )
FileWriteLine ( "c:testingtons.txt", "test" )

sleep(1000)

Else

sleep(1000)

EndIF

WEnd

I would expect it to write to a text file in the root of C: when at the login screen but it doesn't...

I used...

While 1

If ProcessExists ("Explorer.exe") Then

sleep(1000)

Else

FileOpen ( "c:testingtons.txt" ,1 )
FileWriteLine ( "c:testingtons.txt", "test" )

sleep(1000)
EndIf
WEnd

And that seems to work - although if anyone kills Explorer or it crashes then it might start the script when I don't really want it happening.

Edited by AmbientMike
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...