Eddi96

Logoff users in CMD with ID

13 posts in this topic

#1 ·  Posted (edited)

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
added operating system, added some begging

Share this post


Link to post
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 this post


Link to post
Share on other sites
9 minutes ago, SadBunny said:

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
1 hour ago, SadBunny said:

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

So i found the solution. you had to add C:\Windows\System32\

Share this post


Link to post
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 this post


Link to post
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,'')
    Local $sID=$aArray[$iRow][2]
    Return SetError(0,$iRow,$sID)
EndFunc

you have to change path to your requiriies.

Share this post


Link to post
Share on other sites

Hey AutoBert thank you for your response,

so I tested your code and the only thing that happens is that it displays the Array. I tried to ask for the $sID with a MsgBox but the box doesnt show

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

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

Share this post


Link to post
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

  • Similar Content

    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      i am working on a application where if the flashing is success i get a window saying "SUCCESS" and if the flashing failed a window saying "FAILED" and i have to automatically identify pass or fail.the problem is both the windows are having the same control ID. how can i differentiate between both windows? so that i can make use of that in script for automation///
       
       
      Thanks
    • comtech80
      By comtech80
      Folks,
      I have an issue I've been trying to solve for a while, I'm trying to add static routes in DHCP via a 121 route rule in Windows 2012 R2 is a brutal manual process and wanted to automate this via AutoIT.
       
      When I use the "ControlGetText" everything displays properly in the MSG box but when I try and use "ControlSetText" or "ControlSend" the values won't display in the input box but the action comes back as successful?
      Anyone able to help me with this? I'm thinking this might be an active window issue but i'm not sure.
      Here is a part of my code.
       
      $hWnd = WinWait("[TITLE:Add a Static Route]","", 10)
      WinActivate($hWnd)
      $Status = ControlSend($hWnd, "", "[CLASS:Edit; INSTANCE:4]", "192"); Does not add 192 to the text box.
      Local $sTextEdit1 = ControlGetText($hWnd, "", "[CLASS:Edit; INSTANCE:1]")
      ConsoleWrite ( "ControlSend Status: " &  $Status & @CRLF); Returns a Value of 1
      ConsoleWrite ( "ControlGetText Value Edit1: " &  $sTextEdit1 & @CRLF);
       
      Please see attachment for more info.

    • lganta
      By lganta
      Hello!
      I created some scripts for a simple farming bot a few years ago and they rely on ControlSend.
      A year ago I quit that game and then installed Windows 10 (was using windows 7).
      Recently I installed that game and tried running them again and they seem to work (I have some messages displayed on the screen with the state of the bot ), except for the ControlSend part (which obviously is crucial).
      I started debugging the scripts so I created a basic script that makes use of Send. I tested this with Notepad in focus and works just fine and then with my game window in focus and it didn't work (it's supposed to write that text in an input box from the game).
      #include <MsgBoxConstants.au3> Sleep(3000); Send("some text"); MsgBox($MB_OK, "Notification", "Control was sent!");  
      Is there a way for the creators of the game to create some kind of security system against this? Or something happens because I updated to Windows 10?
      Is there something I'm missing?
      Thank you!
    • hcI
      By hcI
      Hello I would like to know if there is a way to return a sentence in cmd when I launch from it (because I add arguments).
      For example, diskpart.exe which help to manage the key and hdd connected, when you launch it with the parameter "/f" the app return a sentence saying that it don't recognize the parameter "/f" and it return the sentence in the cmd where i started the application, not a new one.
      That's what I want to do but I couldn't find anything that would solve my problem on internet and on AutoIt like ConsoleWrite / ConsoleWriteError (don't work).
       
      Thanks
    • Duck
      By Duck
      I'm attempting to capture the output from the command line tool PSEXEC. I'm using AutoIT to run an instance of PSEXEC against a remote PC to audit Local Admins in my environment using net.exe (C:\Windows\System32> net localgroup administrators). However the usual trick I use to capture command line output does not appear to work well with PSEXEC, as the bottom portion of the output is missing from the return. Any ideas or recommendations are greatly appreciated.  
       
      Here is what I'm working with: 
      ;This script will read from a list of hosts and report who has local admin privileges on the machine #RequireAdmin Global $fileName = @ScriptDir & '\test.txt' ;hostlist, one host per line readHostList() ;Read list of hosts Func readHostList() Local $file = FileOpen($fileName, 0) While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop ConsoleWrite($line & @CRLF) ;MsgBox(0,0,$line) getLocalAdmins($line) WEnd FileClose($file) EndFunc ;run PSEXEC to list local admins Func getLocalAdmins($remotePC) Local $testFile = @ScriptDir &'\test234.txt' FileOpen($testFile, 1) Local $psexec = 'psexec \\' & $remotePC & ' net localgroup administrators' FileWriteLine($testFile, _RunCmd($psexec) ) FileClose($testFile) EndFunc ;Used to return CLI output Func _RunCmd($sCommand) Local $nPid = Run(@Comspec & " /c" & $sCommand, @SystemDir, @SW_Hide, 8), $sRet = "" If @Error then Return "ERROR:" & @ERROR ProcessWait($nPid) While 1 $sRet &= StdoutRead($nPID) If @error Or (Not ProcessExists ($nPid)) Then ExitLoop WEnd Return $sRet EndFunc  
      ## If i manually run the command on the remote PC via PSEXEC I will get the following output: 
      PsExec v2.11 - Execute processes remotely
      Copyright (C) 2001-2014 Mark Russinovich
      Sysinternals - www.sysinternals.com
      Starting net on PCNAME... on PCNAME...
      net exited on PCNAME with error code 0.
      -------------------------------------------------------------------------------
      admin
      Administrator
      Alias name     administrators
      Domain\Domain Admins
      Comment        Administrators have complete and unrestricted access to the computer/domain
      Members
      The command completed successfully.
       
      ## The returned output from running the above script is as follows:
      PsExec v2.11 - Execute processes remotely
      Copyright (C) 2001-2014 Mark Russinovich
      Sysinternals - www.sysinternals.com
      Alias name     administrators
      Connecting to PCNAME...
      Starting PSEXESVC service on PCNAME...
      Connecting with PsExec service on PCName...
      Starting net on PCNAME..
      net exited on PCNAME with error code 0.
       
      **Note to test this script PSEXEC must be in the system dir or the path in the script changed 
      PSEXEC tool: https://docs.microsoft.com/en-us/sysinternals/downloads/psexec