Jump to content

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
added operating system, added some begging
Link to comment
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.

Link to comment
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?

Link to comment
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?

Link to comment
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.

Link to comment
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\

Link to comment
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?

Link to comment
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.

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

×
×
  • Create New...