Sign in to follow this  
Followers 0
pieterisme

Windows user account or string matching

6 posts in this topic

Hi All,

Basically I need to know the best way/s to accomplish this;

I need to find a specific user/user name (under windows management/users and groups) then put it into a variable for use to store files and data from a machine onto a folder with the users name.

Okay, so far the only way with autoit to find the username I desire is this;

RunWait(@ComSpec & " /c " & 'net user > "' & @ScriptDir & 'netusers.txt"', @WorkingDir, @SW_HIDE)

which outputs;

User accounts for \\lptp0001
-------------------------------------------------------------------------------
    Administrator       Guest      u0081    a0001                                                       
The command completed successfully.

My guess would be to load @ScriptDir & 'netusers.txt" then somehow match the text with a mask for u#### or a#### but I dunno really how to open a file read it or match the string part. Please help, thanks in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

What about something like this:

Dim $UserSID, $oWshNetwork, $oUserAccount
$objWMIService = objGet( "winmgmts:{impersonationLevel=impersonate}!//"  & @ComputerName & "/root/cimv2")
$oUserAccounts = $objWMIService.ExecQuery("Select Name, SID from Win32_UserAccount")
For $oUserAccount In $oUserAccounts
    If StringLeft($oUserAccount.Name,1) = "u" or StringLeft($oUserAccount.Name,1) = "a" Then
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oUserAccount.Name = ' & $oUserAccount.Name & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    EndIf
Next
Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Seconded, You beat me to it ;)

WMI is your friend.

For more examples look up Scriptomatic.

Share this post


Link to post
Share on other sites

He may also have to check that the WMI service is not disabled.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

What about something like this:

Dim $UserSID, $oWshNetwork, $oUserAccount
$objWMIService = objGet( "winmgmts:{impersonationLevel=impersonate}!//"  & @ComputerName & "/root/cimv2")
$oUserAccounts = $objWMIService.ExecQuery("Select Name, SID from Win32_UserAccount")
For $oUserAccount In $oUserAccounts
    If StringLeft($oUserAccount.Name,1) = "u" or StringLeft($oUserAccount.Name,1) = "a" Then
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oUserAccount.Name = ' & $oUserAccount.Name & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    EndIf
Next

Sweet guys fasts replies, also thanks Legacy99 for the WMI site I will look it up when I get more time always willing to learn ;)

However I am getting a variable empty issue when trying to assign to a string and remove the a or u prefix from the user account number

Dim $UserSID, $oWshNetwork, $oUserAccount
$objWMIService = objGet( "winmgmts:{impersonationLevel=impersonate}!//"  & @ComputerName & "/root/cimv2")
$oUserAccounts = $objWMIService.ExecQuery("Select Name, SID from Win32_UserAccount")
For $oUserAccount In $oUserAccounts
    If StringLeft($oUserAccount.Name,1) = "u" or StringLeft($oUserAccount.Name,1) = "a" And $oUserAccount.Name <> "Administrator" Then
        $ua_num = ($oUserAccount.Name)
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oUserAccount.Name = ' & $oUserAccount.Name & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
        $pc_number = StringTrimLeft($ua_num, 1)
        MsgBox(16, "mydebug", $pc_number)
    EndIf
Next

Share this post


Link to post
Share on other sites

Try it this way.

Dim $UserSID, $oWshNetwork, $oUserAccount
$objWMIService = objGet( "winmgmts:{impersonationLevel=impersonate}!//"  & @ComputerName & "/root/cimv2")
$oUserAccounts = $objWMIService.ExecQuery("Select Name, SID from Win32_UserAccount")
For $oUserAccount In $oUserAccounts
    If (StringLeft($oUserAccount.Name,1) = "u" or StringLeft($oUserAccount.Name,1) = "a") And $oUserAccount.Name <> "Administrator" Then
        $ua_num = ($oUserAccount.Name)
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oUserAccount.Name = ' & $oUserAccount.Name & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
        $pc_number = StringTrimLeft($ua_num, 1)
        MsgBox(16, "mydebug", $pc_number)
    EndIf
Next

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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
Sign in to follow this  
Followers 0