Jump to content
Eddi96

Logoff users in CMD with ID

Recommended Posts

Eddi96

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
SadBunny

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
Eddi96
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
SadBunny

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
Eddi96

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
SadBunny

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
Eddi96
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
Eddi96

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
AutoBert

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
Eddi96

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
AutoBert

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

    • Pagi
      By Pagi
       
      <a href="index.php=4143b27b41db0a40c499202865c3e24d" class="menuitem " target="\_self"> "Menu Item 1" </a>
      Can you tell me if it´s possible to click this menu button? If yes, how? Cause I have no idea, I think i should use _IETagNameGetCollection
    • milkmoron
      By milkmoron
      https://www.autoitscript.com/forum/topic/153520-iuiautomation-ms-framework-automate-chrome-ff-ie/?do=findComment&comment=1156373 At least we have an element title: [Entered By:] class: [WindowsForms10.EDIT.app.0.30495d1_r9_ad1] Having the following values for all properties: Title is: <Entered By:> Class := <WindowsForms10.EDIT.app.0.30495d1_r9_ad1> controltype:= <UIA_EditControlTypeId> ,<50004> , (0000C354) 322;370;736;184 *** Parent Information top down *** 3: Title is: <View Activity> Class := <WindowsForms10.Window.8.app.0.30495d1_r9_ad1> controltype:= <UIA_WindowControlTypeId> ,<50032> , (0000C370) 306;72;784;605 "Title:=View Activity;controltype:=UIA_WindowControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1"" 2: Title is: <> Class := <WindowsForms10.Window.8.app.0.30495d1_r9_ad1> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 314;103;768;542 "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1"" 1: Title is: <> Class := <WindowsForms10.Window.8.app.0.30495d1_r9_ad1> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 314;103;749;451 "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1"" 0: Title is: <Delete> Class := <WindowsForms10.Window.8.app.0.30495d1_r9_ad1> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 314;122;749;432 "Title:=Delete;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1"" ;~ *** Standard code maintainable *** #include "UIAWrappers.au3" AutoItSetOption("MustDeclareVars", 1) _UIA_setVar("oP1","Title:=View Activity;controltype:=UIA_WindowControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1") ;View Activity _UIA_setVar("oP2","Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1") ; _UIA_setVar("oP3","Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1") ; _UIA_setVar("oP4","Title:=Delete;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1") ;Delete _UIA_setVar("EnteredBy:.mainwindow","title:=Entered By:;classname:=WindowsForms10.EDIT.app.0.30495d1_r9_ad1") ;~ Actions split away from logical/technical definition above can come from configfiles ;~_UIA_Action("oP1","highlight") _UIA_Action("oP1","setfocus") ;~_UIA_Action("oP2","highlight") _UIA_Action("oP2","setfocus") ;~_UIA_Action("oP3","highlight") _UIA_Action("oP3","setfocus") ;~_UIA_Action("oP4","highlight") _UIA_Action("oP4","setfocus") _UIA_action("EnteredBy:.mainwindow","setfocus") ;~ *** Standard code Flexible*** #include "UIAWrappers.au3" AutoItSetOption("MustDeclareVars", 1) Local $oP3=_UIA_getObjectByFindAll($UIA_oDesktop, "Title:=View Activity;controltype:=UIA_WindowControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1", $treescope_children) _UIA_Action($oP3,"setfocus") Local $oP2=_UIA_getObjectByFindAll($oP3, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1", $treescope_children) _UIA_Action($oP2,"setfocus") Local $oP1=_UIA_getObjectByFindAll($oP2, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1", $treescope_children) _UIA_Action($oP1,"setfocus") Local $oP0=_UIA_getObjectByFindAll($oP1, "Title:=Delete;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.30495d1_r9_ad1", $treescope_children) _UIA_Action($oP0,"setfocus") _UIA_setVar("EnteredBy:.mainwindow","title:=Entered By:;classname:=WindowsForms10.EDIT.app.0.30495d1_r9_ad1") _UIA_action("EnteredBy:.mainwindow","setfocus") Need some help selecting this defining it as a variable to save and then insert the text within, to the same application but different account.
    • PleaseHelpMEIWillLoveyou
      By PleaseHelpMEIWillLoveyou
      Is there a way to use a custom terminal or something from my pc and run a script on another pc.
      please let me know if its possible
      (not for anything bad just for a project im working on)
    • kelso
      By kelso
      Hello Guru's,
       
      I'm trying to write an autoit script to select from the dropdown list as you see in the attached picture. 
      I read the help page for _IEFormElementOptionSelect, but I cannot grasp how to correlate that with the source code that I'm seeing. any suggestions?

    • SC0U7
      By SC0U7
      Hello i have a text file which contain over 600KB of BASE64 strings like :
      TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
      Now what i need is divide this long string as autoit variable each max lenght (4000characters per line) example:
      Local $var
      $var &=  TVqQAAMAAAAEAAA..
      $var &=  VbAmejwqqqAACEE..
      and then how to i add encrypt function on every line ? how to i can xor it? example final will be :
      $var &=  XorEnc(TVqQAAMAAAAEAAA..)
      $var &=  XorEncVbAmejwqqqAACEE..)
       
      Thanks for any help and ideas 
×