Logoff users in CMD with ID

Recommended Posts

Hey guys!

I need your help again. So I am working on a Project with which I can Logoff a User in a Terminal-Server (Windows Server 2012 R2), logged in as Admin. Then backup the users partition and log him back in. Sounds pretty easy for you guys, right? So my problem is... I seem to not be able to get the User ID. You can see and display the ID in a textfile with:

Run("query user>>c:\users\example\desktop\helpmeguys.txt")

This line just writes a textfile of "query user" on the desktop. This is an example on what it looks like:

BENUTZERNAME          SITZUNGSNAME       ID  STATUS  LEERLAUF   ANMELDEZEIT
>wg                                  console                       2  Aktiv                     17    25.07.2016 08:19

What I need right now is the ID. Any Idea on how to read it off of the Textfile?

Please give me examples because I am still a rookie!

With kind regards

Eddi96

Edited by Eddi96
Share on other sites

Seems to be a CSV format with @Tab as delimiter. So test:

#include <Array.au3>
#include <File.au3>

Global $sFile='User.txt' Global$aUsers
_FileReadToArray($sFile,$aUsers,$FRTA_NOCOUNT,@TAB) _ArrayDisplay($aUsers)

if the arraydisplay don't work attach your origibnalfile.

Share on other sites

Here's an example:

#include <Array.au3>

$aFileArray = FileReadToArray("c:\tmp\x.txt") ; NOTE: the first line of the file is in element 0, but that only holds column headers so we start from 1! For$lineIndex = 1 to UBound($aFileArray) ; Let's take every group of non-whitespace characters from the current line and put it in an array:$aSplit = StringRegExp($aFileArray[$lineIndex], "(\S+)", $STR_REGEXPARRAYGLOBALMATCH) _ArrayDisplay($aSplit, "All elements on line")

; We know the format of the file, so we know where the values will be in the array:
$userName =$aSplit[0]
$userId =$aSplit[2]

MsgBox(64, "Info", "Username: " & $userName & @CRLF & "ID: " &$userId)
Next

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share on other sites
Local $sContent = FileRead("helpmeguys.txt") Local$aSessions = StringRegExp($sContent, "rdp-tcp#\d+\h+\H+\h+(\d+)", 3) _ArrayDisplay($aSessions)

Share on other sites

Here's an example:

#include <Array.au3>

$aFileArray = FileReadToArray("c:\tmp\x.txt") ; NOTE: the first line of the file is in element 0, but that only holds column headers so we start from 1! For$lineIndex = 1 to UBound($aFileArray) ; Let's take every group of non-whitespace characters from the current line and put it in an array:$aSplit = StringRegExp($aFileArray[$lineIndex], "(\S+)", $STR_REGEXPARRAYGLOBALMATCH) _ArrayDisplay($aSplit, "All elements on line")

; We know the format of the file, so we know where the values will be in the array:
$userName =$aSplit[0]
$userId =$aSplit[2]

MsgBox(64, "Info", "Username: " & $userName & @CRLF & "ID: " &$userId)
Next

Hey Sad Bunny, thanks for the quick response but when I rewrote it and started it, it gives me the right ID but also an Error : Array variable has incorrect number of subscirpts or subscript dimension range exceeded. Any Idea on why it does this?

Share on other sites

Yes, typo in my code. Use this line:

For $lineIndex = 1 to UBound($aFileArray) - 1

(I forgot to add the -1 and didn't notice the error  )

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share on other sites

So I have another Problem,

when I use

Run ("C:\Users\wg\Desktop\GTScript\TEMPquery.bat")

the bat file:

query user>>c:\users\wg\desktop\GTScript\tempID1.txt

It doesnt write the text...

But if i just start it with my mouse, it does write the text. I don't get why it does it, do you have another way of running a batch through autoit?

Share on other sites

You may want to put #RequireAdmin on top of your scipt so the script runs with admin rights, that may help.

But yeah you certainly don't have to do it through a batch file. There are lots of ways to get these sorts of information more directly. Look at the help file for StdoutRead to see how to directly capture the output of that command without having to pipe it through a textfile. There are probably other ways to get to this information directly from Windows API's using some cool UDF, but you'll have to search for that yourself

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share on other sites

You may want to put #RequireAdmin on top of your scipt so the script runs with admin rights, that may help.

But yeah you certainly don't have to do it through a batch file. There are lots of ways to get these sorts of information more directly. Look at the help file for StdoutRead to see how to directly capture the output of that command without having to pipe it through a textfile. There are probably other ways to get to this information directly from Windows API's using some cool UDF, but you'll have to search for that yourself

#RequireAdmin

#include <Array.au3>

Run("C:\Windows\System32\query user>>c:\GTScript\tempID1.txt")

$aFileArray = FileReadToArray("c:\GTScript\tempID1.txt") For$lineIndex = 1 to UBound($aFileArray) - 1$aSplit = StringRegExp($aFileArray[$lineIndex], "(\S+)", $STR_REGEXPARRAYGLOBALMATCH)$userName = $aSplit[0]$userId = $aSplit[2] FileOpen("c:\GTScript\tempID2.txt", 2)$Sek = 1
sleep($Sek * 500) FileWrite("c:\GTScript\tempID2.txt",$userId)

FileDelete("c:\GTScript\tempID1.txt")

Next

Share on other sites

Hey people, thank you all for you help but I have another challange right now.

#include <Array.au3>

$aFileArray = FileReadToArray("c:\tmp\x.txt") ; NOTE: the first line of the file is in element 0, but that only holds column headers so we start from 1! For$lineIndex = 1 to UBound($aFileArray) ; Let's take every group of non-whitespace characters from the current line and put it in an array:$aSplit = StringRegExp($aFileArray[$lineIndex], "(\S+)", $STR_REGEXPARRAYGLOBALMATCH) _ArrayDisplay($aSplit, "All elements on line")

; We know the format of the file, so we know where the values will be in the array:
$userName =$aSplit[0]
$userId =$aSplit[2]

MsgBox(64, "Info", "Username: " & $userName & @CRLF & "ID: " &$userId)
Next

This is the Code im working with, but the Problem is:  I need to rewrite it because we dont know where the needed ID is. We know how the File is built and we know that the ID is always in the same row with the Username, the problem I have right now is that I don't know how to read for a certain Username and then read the ID from that row. Can you please help me with that?

Share on other sites

Test this:

#include <Array.au3>
#include <File.au3>

Global $sFile = 'Users.txt' Global$aUsers
_FileReadToArray($sFile,$aUsers, $FRTA_NOCOUNT) _ArrayColInsert($aUsers, 1)
_ArrayColInsert($aUsers, 1) _ArrayColInsert($aUsers, 1)
_ArrayColInsert($aUsers, 1) _ArrayColInsert($aUsers, 1)
_ArrayColInsert($aUsers, 1) For$i = 0 To UBound($aUsers) - 1$aSplit = StringRegExp($aUsers[$i][0], "(\S+)", $STR_REGEXPARRAYGLOBALMATCH) For$j = 0 To UBound($aSplit)-1$aUsers[$i][$j] =$aSplit[$j]
Next
$aUsers[$i][0]=StringReplace($aUsers[$i][0],'>','')
Next
_ArrayDisplay($aUsers,'User')$sUser='wg'
MsgBox(64,'Searcher UserID '&$sUser,_FindUserID($aUsers,$sUser)) Func _FindUserID($aArray,$sSearch) Local$iRow=_ArraySearch($aArray,$sUser)
If @error Then Return SetError(@error,-1,'')

Share on other sites

The func should return ID for the user:

$sUser='wg' MsgBox(64,'Searcher UserID '&$sUser,_FindUserID($aUsers,$sUser))

if you need userfrom id you have to use :

$sID='2' ;change ID to your needs MsgBox(64,'Searcher UserID '&$sID,_FindUserByID($aUsers,$sID))

Func _FindUserByID($aArray,$sID)
Local $iRow=_ArraySearch($aArray,$sID,0,0,0,0,1,2) If @error Then Return SetError(@error,-1,'') Local$sName=$aArray[$iRow][0]
Return SetError(0,$iRow,$sName)
EndFunc

rest of script is same.

Edited by AutoBert

Create an account

Register a new account

• Similar Content

• Hi, I am using python to call the Autoit function. I found a interesting problem.
env:  win10 64bit
python3.6.4  x86、    python3.8.6  x64
autoit v3.3.16.0
code:
#filename: demo.py from ctypes import windll dll = windll.LoadLibrary(r"D:\it_tools\autoit\AutoIt3\AutoItX\AutoItX3_x64.dll") # or AutoItX3.dll dll.AU3_Send("#r", 0) Behaviour:
(1) run with the "python.exe"
it will not open the run dialog, but input a "r" in the cmd window.

(2) run with xxxxxx.exe (renamed from python.exe, you can rename whatever you like)
work success

I dont know why it happened.   I think it shoule be related to Python and Autoit.  So I came here...
Can someone give me some advices.  Thanks a lot.
(by the way, My English is not very well... I wish I  have provided the enough information... If you need more details, please contact me    )
• By Vaiola
We need to transfer from windows server 2012 to windows server 2016, we didn't find a way using to transfer the data automatically or to email us after tasks are finished.
Is there a way to copy the data with NTFS/Shared permissions and timestamps without any loss?
• By zoel
Hello people, I have a script which calls CMD and executes the netstat command, but when I run it through SCITE the GUI opens but I have no output, How can I run the command as administrator?
Here is my script so far
#include <AutoItConstants.au3> #include <GUIConstantsEx.au3> #include <GuiEdit.au3> #include <ScrollBarsConstants.au3> #include <WindowsConstants.au3> #RequireAdmin Local $aWndPos Local$hWnd = GUICreate("Form2", 900, 420, -1, -1, Default + $WS_MAXIMIZE) GUISetBkColor(0xE4E4E4) Local$idFilemenu = GUICtrlCreateMenu("&File") Local $idExititem = GUICtrlCreateMenuItem("Exit",$idFilemenu) Local $idInput = GUICtrlCreateEdit("", 210, 10, 660, 360) GUICtrlSetResizing(-1,$GUI_DOCKBORDERS) GUICtrlSetFont(-1, 10, 400, Default, "COURIER NEW") Local $idButton = GUICtrlCreateButton("NetStat", 10, 10, 190, 25) GUICtrlSetResizing(-1, BitOR($GUI_DOCKLEFT, $GUI_DOCKTOP,$GUI_DOCKWIDTH, $GUI_DOCKHEIGHT)) GUISetState(@SW_SHOW) While 1$nMsg = GUIGetMsg() Switch $nMsg Case$GUI_EVENT_CLOSE Exit Case $idExititem Exit Case$idButton GUICtrlSetData($idInput, _CmdInfo() & @CRLF) _GUICtrlEdit_Scroll($idInput, $SB_SCROLLCARET) _GUICtrlEdit_Scroll($idInput, $SB_SCROLLCARET) EndSwitch WEnd Func _CmdInfo($_sCmdInfo = "netstat /b") Local $iPID = Run(@ComSpec & " /c " &$_sCmdInfo, "", @SW_HIDE, BitOR($STDERR_CHILD,$STDOUT_CHILD)) ; Wait until the process has closed using the PID returned by Run. ProcessWaitClose($iPID) ; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead. Local$sOutput = StringReplace(StdoutRead($iPID), @CRLF & @CRLF, @CRLF) Local$sOutErr = StringReplace(StderrRead($iPID), @CRLF & @CRLF, @CRLF) Local$sReult = $sOutput <> "" ?$sOutput : $sOutErr Return$sReult EndFunc

• hello autoit team
am trying to make the list of features in list view and control it
am using the dism command line to read output
but i found some problems
what i need is the list of features in 2d array
$array[$n][0]= name $array[$i][1] = state
when i tested the code
it give me some results that i don't need to it e.g
------ ------  | --------
or
name       | state
i need just the list of features and there state
here is my example
#RequireAdmin #NoTrayIcon #include <AutoItConstants.au3> Wow64EnableWow64FsRedirection(false) _Windows_Get_Features() func _Windows_Get_Features() local $a_FeaturesArray[1][2]$a_FeaturesArray[0][0] local $i_Dism_Run = Run("DISM /online /english /get-features /format:table", "", @sw_hide, BitOR($STDERR_CHILD, $STDOUT_CHILD)) local$s_OutputDism = "", $a_OutPutDism While 1$s_OutputDism = StdoutRead($i_Dism_Run) If @error Then exitLoop if ($s_OutputDism = "") or (StringRegExp($s_OutputDism, "^((\-){1,})?(?:\s)\|?(?:\s)((\-){1,})$", 0) = 1) then ContinueLoop if (StringRegExp($s_OutputDism, "((([\s\d\-\+\_\,]{1})\|([\s\d\-\+\_\,]{1})){1,})", 0) = 1) then$a_OutPutDism = StringSplit($s_OutputDism, @lf) for$i = 1 to $a_OutPutDism[0] if msgBox(1,$i, $a_OutPutDism[$i]) = 1 then exitLoop next endIf Wend endFunc func Wow64EnableWow64FsRedirection($b_Enabled) local$h_OpenFS = DLLOpen("kernel32.dll") local $Return = DllCall($h_OpenFS, "boolean", "Wow64EnableWow64FsRedirection", "boolean", $b_Enabled) if @error then DLLClose($h_OpenFS) Return SetError(@error, @extended, -1) else DLLClose($h_OpenFS) Return$Return[0] endIf endFunc

• Hello! Thanks you for looking at my post
**No source code I'm sorry work related can't copy information**
I've been using autoit for about 1 year.
I'm having trouble automating a click on an internet explorer web page and I've tried a lot of examples from help pages and forums already. The object I'm trying to click on isnt always in the same spot so I can't use mouse click or control click, I have tried to use the different get collection options and clickbyname, or index or get object. I'm just struggling.
Description of object I'm trying to click --
HTML Code looks like <a ng-click.. "Click Here" it appears it's just a click able object named "click here" that opens a hidden window by running a script inside the web page. I'm not able to grab the information from the window unless it's open so I have to automate this click somehow.

I understand it's difficult to assist without having something to look at, I apologize for that sincerely and appreciate and assistance and suggestions.
×

• Wiki
• AutoIt Resources

• FAQ
×
• Create New...