maloysius Posted February 16, 2022 Share Posted February 16, 2022 Hello AutoIt community! I am developing a script for my point-of-sale company that needs to log out ALL users first before it executes the next part. I have previously used the "logoff" command using batch ran as an administrator. When I call this batch from AutoIt, even if the AutoIt script is ran as administrator, the batch is NOT ran as admin. Due to this, it just gives me the "logoff is not a recognized command" error. I have tried running it like this in the script as well: RunWait(@ComSpec & " /c " & "logoff 2") RunWait(@ComSpec & " /c " & "logoff 3") RunWait(@ComSpec & " /c " & "logoff 4") RunWait(@ComSpec & " /c " & "logoff 5") RunWait(@ComSpec & " /c " & "logoff 6") RunWait(@ComSpec & " /c " & "logoff 7") RunWait(@ComSpec & " /c " & "logoff 8") RunWait(@ComSpec & " /c " & "logoff 9") RunWait(@ComSpec & " /c " & "logoff 10") RunWait(@ComSpec & " /c " & "logoff 11") RunWait(@ComSpec & " /c " & "logoff 12") RunWait(@ComSpec & " /c " & "logoff 13") RunWait(@ComSpec & " /c " & "logoff 14") RunWait(@ComSpec & " /c " & "logoff 15") RunWait(@ComSpec & " /c " & "logoff 16") RunWait(@ComSpec & " /c " & "logoff 17") RunWait(@ComSpec & " /c " & "logoff 18") RunWait(@ComSpec & " /c " & "logoff 19") RunWait(@ComSpec & " /c " & "logoff 20") RunWait(@ComSpec & " /c " & "logoff 21") RunWait(@ComSpec & " /c " & "logoff 22") RunWait(@ComSpec & " /c " & "logoff 23") RunWait(@ComSpec & " /c " & "logoff 24") RunWait(@ComSpec & " /c " & "logoff 25") RunWait(@ComSpec & " /c " & "logoff 26") RunWait(@ComSpec & " /c " & "logoff 27") RunWait(@ComSpec & " /c " & "logoff 28") RunWait(@ComSpec & " /c " & "logoff 29") RunWait(@ComSpec & " /c " & "logoff 30") My idea was to see if I could use the elevated privileges of the AutoIt script in a different way, but this doesn't work either. I've looked into the Shutdown function, but this only allows you to log the current user off. I have this written to log out all sessions from 2-30, but I am realizing that that may not be the best way to do this - is there some quick way to log out all active sessions besides the one you're on? Does anyone possibly have any ideas on this one????? Thanks in advance!! Link to comment Share on other sites More sharing options...
ad777 Posted February 16, 2022 Share Posted February 16, 2022 @maloysius your script missing #RequireAdmin. check season id using task manger(users) or using cmd commad:query session check your id from user name iam ِAutoit programmer. best thing in life is to use your Brain to Achieve everything you want. Link to comment Share on other sites More sharing options...
maloysius Posted February 16, 2022 Author Share Posted February 16, 2022 Hi ad777, thanks for the reply! I apologize, this is only a small snippet of my code - the full program is almost 2k lines at this point! I should have mentioned that I do indeed have #RequireAdmin at the top. The two other suggestions you have are great ways to get that information, I appreciate it! How would you suggest I go about using it to log those users off the system via AutoIt? Do you think it's possible? Link to comment Share on other sites More sharing options...
ad777 Posted February 17, 2022 Share Posted February 17, 2022 (edited) 2 hours ago, maloysius said: The two other suggestions you have are great ways to get that information, I appreciate it! How would you suggest I go about using it to log those users off the system via AutoIt? Do you think it's possible? yeah it's possible,you can logoff user by [ID] using cmd:logoff ID thx to @Subz for the _Array1Dto2D or you can logoff user by name here is script for it: expandcollapse popup#RequireAdmin #include <Array.au3> #include <AutoItConstants.au3> _LogoffUser("name");;; name of person you want to log off Func _LogoffUser($user) Global $sOutput FileDelete(".\list.log") Sleep(10) Local $iPID = Run(@ComSpec & " /c " & "query session", "", @SW_HIDE, $STDOUT_CHILD) While 1 $sOutput &= StdoutRead($iPID) If $sOutput <> "" And StringInStr($sOutput, $user) Then FileWriteLine(".\list.log", $sOutput) Local $sFilePath = ".\list.log" Local $sFileData = FileRead($sFilePath) Local $sHeader = FileReadLine($sFilePath, 1) Local $iColumn1 = StringInStr($sHeader, "USERNAME") - 1 Local $iColumn2 = StringInStr($sHeader, "ID") - $iColumn1 - 1 Local $iColumn3 = StringLen($sHeader) - $iColumn1 - $iColumn2 - 1 Local $aFileData1D = StringRegExp($sFileData, "(.{" & $iColumn1 & "})(.{" & $iColumn2 & "})(.{" & $iColumn3 & "})", 3) Local $aFileData2D = _Array1DTo2D($aFileData1D, 3) Local $iSearch = _ArraySearch($aFileData2D, $user, 0, 0, 0, 1, 1, 1) Local $SeasonID = StringReplace($aFileData2D[$iSearch][2], "Disc", "") Run(@ComSpec & " /c " & "logoff " & $SeasonID, "", @SW_HIDE) ExitLoop EndIf WEnd EndFunc ;==>_LogoffUser Func _Array1DTo2D($avArray, $iCols, $iStart = 0, $iEnd = 0, $iFlag = 0) If $iStart = Default Or $iStart < 0 Then $iStart = 0 If $iEnd = Default Then $iEnd = 0 If Not IsArray($avArray) Then Return SetError(1, 0, 0) If UBound($avArray, 0) <> 1 Then Return SetError(3, 0, 0) Local $iUBound = UBound($avArray) - 1 If $iEnd < 1 Then $iEnd = $iUBound If $iEnd > $iUBound Then $iEnd = $iUBound If $iStart > $iEnd Then Return SetError(2, 0, 0) Local $iNbRows = ($iEnd - $iStart + 1) / $iCols If $iFlag And IsFloat($iNbRows) Then Return SetError(2, 0, 0) Local $aRet[Ceiling($iNbRows)][$iCols] Local $iCol = 0, $iRow = 0 For $i = $iStart To $iEnd If $iCol = $iCols Then $iCol = 0 $iRow += 1 EndIf $aRet[$iRow][$iCol] = $avArray[$i] $iCol += 1 Next Return $aRet EndFunc ;==>_Array1DTo2D Edited February 17, 2022 by ad777 maloysius 1 iam ِAutoit programmer. best thing in life is to use your Brain to Achieve everything you want. Link to comment Share on other sites More sharing options...
maloysius Posted February 17, 2022 Author Share Posted February 17, 2022 Fantastic ad777, thank you so much for the time and effort!! Really appreciate you! I will try this out when I get a moment 👍 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now