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

    • TheAutomator
      By TheAutomator
      A fullscreen console with custom commands!

      Introduction:
      Hi everyone!
      This funny project started as a question in the help section:
      https://www.autoitscript.com/forum/topic/174404-edit-detect-key-before-updating-content/
      I'd like to share this script with everyone that is interested. 
       
      Why would I want it?
      You like the old style fullscreen console (like in the old day's), You can add custom commands, You can customize the font a lot more compared to cmd.exe, You can share ideas or add tweaks to the script.  
      Still to do:
      Write a simple custom programming language to implement this tool. Writing a little help file / pdf to describe my little programming language. Add little sound effects like a beep if there is a syntax error (optional). Clean up and modify Console.Au3 content. Add an option to have to type a login password (optional). Make an optional installer that also gives scripts for this tool a custom icon and open with command. ...Call Neo?  
      Thanks to:
      xxaviarxx: debugging, some ideas. jguinch: debugging, adding a bunch of tweaks and ideas. kylomas: debugging, new ideas.  
      Edits and updates:
      Added usage of tab key in edit control Edit has focus now on startup I'm currently rewriting a simple custom programming language to implement this tool.  
      UDF download: Console.au3
       
      Regards
      TheAutomator
    • XaelloNegative
      By XaelloNegative
      Hi,
      I'm currently saving process time stamps of my employer's other employees and saving it in a text document as data storage. However, my employer wants it to be processed in a day to day basis to generate their average process time per day. How can I compare today from yesterday and so on and so forth with the succeeding days? Whats the best approach to this?
      Thank you.
      ~XN~
    • Ignacio
      By Ignacio
      Hello, and good day
      Im trying to make a sentence autocompleter so that when you type certain words (or commands) the scripts, and im in need of help/pointers
      That is what i have at the time and the issues i have currently is that:
      - I cant find an easy way to reset the counter to 0 in case a different letter from those are pressed ( tried NOT _ispressed but i think i got it wrong)
      -Is there another way to detect the key press that _ispressed? (i couldnt find it so far), since i feel like it is too clunky ( although maybe that is just me and my way to code)
      - for some reason the hex code (6F) for the / (divide nume pad) isnt working for me
       
      Im thinking of making a text file with some words to use them as variables/comparations  (so that at least removes the need of a variable for the words in the script) and make the script make a temporal text file to save the input and then compare it with the other one. But i dont know if that is a good approach.
      Thanks for your time and patience.
    • mdwerne
      By mdwerne
      Hello,
      I'm working on a script that writes detailed application event logs, and I'd like to know if there is a way with Autoit to write multiple lines of XML EventData (see example below):
      <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"> <System> <Provider Name="Application" /> <EventID Qualifiers="0">1001</EventID> <Level>4</Level> <Task>0</Task> <Keywords>0x80000000000000</Keywords> <TimeCreated SystemTime="2015-07-12T21:26:07.000000000Z" /> <EventRecordID>86554</EventRecordID> <Channel>Application</Channel> <Computer>YOUR_COMPUTER</Computer> <Security /> </System> <EventData> <Data>DeskTop Agent: Mike</Data> <Data>Observer Username: Miguel</Data> etc... </EventData> </Event> So far, using EventCreate, everything I send end's up in a single <data> entry (see below):
      <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"> <System> <Provider Name="Application" /> <EventID Qualifiers="0">1001</EventID> <Level>4</Level> <Task>0</Task> <Keywords>0x80000000000000</Keywords> <TimeCreated SystemTime="2015-07-12T21:26:07.000000000Z" /> <EventRecordID>86554</EventRecordID> <Channel>Application</Channel> <Computer>YOUR_COMPUTER</Computer> <Security /> </System> <EventData> <Data>DeskTop Agent: Mike Observer Username: Miguel</Data> </EventData> </Event> Here is the code I'm using thus far (which does not work the way I'd like):
      $LogData = @CRLF & "DeskTop Agent: " & @UserName & @CRLF & "Observer Username: " & $DTObserver & @CRLF & "File name/s with extension: " & $FilenameWextension & @CRLF & "Action Performed: " & $ActionPerformed & @CRLF & "Explanation: " & $Explanation & @CRLF & "Machine Name: " & @ComputerName & @CRLF & "IP Address: " & @IPAddress1 & @CRLF & "App1Installed: " & $App1 & @CRLF & "App2Installed: " & $App2 Run("eventcreate /T Information /ID 100 /L Application /SO DTALog /D " & Chr(34) & "DTALog Details: " & $LogData & Chr(34), "", @SW_HIDE, 2) I found a Stackoverflow post that talks about doing it in C# (https://stackoverflow.com/questions/7694276/how-to-add-multiple-lines-of-eventdata-to-an-eventlog-in-windows)
      but I'd like to determine if it can be accomplished with AutoIt!.
      Thanks for your time,
      -Mike
    • 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