Jump to content

Problem with "net localgroup" command


Guest Toaster-MTL
 Share

Recommended Posts

Guest Toaster-MTL

When I run the following script

$DestDomain = "DomainXYZ"

$AdminGrpCode = runwait((@ComSpec & " /c " &'net localgroup Administrators "'&$DestDomain&"\"&@UserName&'" /add',"c:\")

MsgBox(0,"Test",$AdminGrpCode)

The "net localgroup" command always ends up failing with the error number 2, which according to "net helpmsg" means "The system cannot find the file specified." When I run this line from an interactive command prompt it works fine. I have even tried using the full path to net.exe (with and without the ComSpec portion) and have tried specifying c:\windows\system32 for the working directory. All return the same error.

Oh yeah, the user running the script is already a memeber of the local administrators group.

Link to comment
Share on other sites

Guest Toaster-MTL

For easier debugging try to start the @ComSpec with "/k" instead of "/c". Then you can see the output of the net-command.

<{POST_SNAPBACK}>

The /k option actually helped quite a bit. We are now able to see the "real" error code instead of the one returned by the RunWait command.

However, I would like to check the error message programatically and that is proving difficult. When I try and redirect the output of the command into a file, the file is always empty. For instance I am using the following code:

$AdminGrpCode = runwait(@ComSpec & " /k " &'net localgroup Administrators "'&$DestDomain&'\'&@UserName&'" /ADD 2>'&$SystemDrive&'\FCWin2K3\AddUser.txt')

if $AdminGrpCode = 0 Then
    LogLine("Domain user added to local administrators group")
Else
    $AdminGrpReturn = FileOpen($SystemDrive &"\FCWin2K3\AddUser.txt",2) 
    $AdminGrpReturnVal = FileReadLine($AdminGrpReturn, 1)
    FileClose($AdminGrpReturn)
    LogLine("Adding domain user to local administrators group returned: "&$AdminGrpReturnVal)
    Crashed("Error adding user to local admin group: "&$AdminGrpReturnVal)
EndIf

The AddUser.txt file is always 0 bytes and empty (when I use /k or /c). I have also tried

$AdminGrpCode = runwait(@ComSpec & " /k " &'net localgroup Administrators "'&$DestDomain&'\'&@UserName&'" /ADD >'&$SystemDrive&'\FCWin2K3\AddUser.txt')

with the same results. Any help would be greatly appreciated.

Link to comment
Share on other sites

If the NET command isn't working, then it's probably outputting to the ErrOut instead of StdOut. To redirect ErrOut, use "2>(filename)" instead of just ">(filename)"

Link to comment
Share on other sites

Guest Toaster-MTL

If the NET command isn't working, then it's probably outputting to the ErrOut instead of StdOut.  To redirect ErrOut, use "2>(filename)" instead of just ">(filename)"

<{POST_SNAPBACK}>

If you take a look at the code

runwait(@ComSpec & " /k " &'net localgroup administrators "'&$DestDomain&'\'&@UserName&'" /ADD 2>'&$SystemDrive&'\FCWin2K3\AddUser.txt')

I am using the 2> format to capture ErrOut, but still the file is empty after the command completes with an error.

Link to comment
Share on other sites

I don't have access to a MS domain, so I can't dynamically test your code, Toaster-MTL, but as a guess I'm saying that it's either the child FCWin2K3 folder isn't being created or the $SystemDrive variable is not being assigned correctly. When in doubt I'll pop open a MsgBox with such a filesystem path to see if it's being built right...

If you're in a situation where it's permissable to use the beta version of AutoIt3, you can call NET.EXE (+ its command line args) directly with Run(), then use the StdoutRead and StderrRead functions to capture any output to strings and test the content of the strings to determine your results. If you still want a permanent record for log purposes you can write the result to the system registry or a file or whatever...

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

Guest Toaster-MTL

I don't have access to a MS domain, so I can't dynamically test your code, Toaster-MTL, but as a guess I'm saying that it's either the child FCWin2K3 folder isn't being created or the $SystemDrive variable is not being assigned correctly. When in doubt I'll pop open a MsgBox with such a filesystem path to see if it's being built right...

If you're in a situation where it's permissable to use the beta version of AutoIt3, you can call NET.EXE (+ its command line args) directly with Run(), then use the StdoutRead and StderrRead functions to capture any output to strings and test the content of the strings to determine your results. If you still want a permanent record for log purposes you can write the result to the system registry or a file or whatever...

<{POST_SNAPBACK}>

The FCWin2K3 folder is created and the $SystemDrive variable is properly set (there are several functions that use both those items not listed in the code I published), so that's not the issue.

However your suggestion about using the beta with StoutRead and SterrRead is a really good idea. I'll look into it.

Is the beta considered stable at this point?

Link to comment
Share on other sites

Is the beta considered stable at this point?

Hmm. I think "beta" says it most honestly; there are some known bugs and there are new features still being added that might break. The StdxxxYyy code itself is pretty insular and has been in the beta since February, I guess, and has had its share of fixes and "makes sense" changes in behavior. I'd declare it fairly stable by now.

If you haven't played with the beta you don't know what you're missing. Most of the buzz on this forum is about stuff with COM, DLL calls and GUI construction that you need the beta to do...

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

If you take a look at the code

runwait(@ComSpec & " /k " &'net localgroup administrators "'&$DestDomain&'\'&@UserName&'" /ADD 2>'&$SystemDrive&'\FCWin2K3\AddUser.txt')

I am using the 2> format to capture ErrOut, but still the file is empty after the command completes with an error.

<{POST_SNAPBACK}>

Have you tried doing a

runwait(@ComSpec & " /k " &'net localgroup administrators "'&$DestDomain&'\'&@UserName&'" /ADD >'&$SystemDrive&'\FCWin2K3\AddUser.txt' & ' 2>&1')

Redirects both standard output and standard err to the file.

Edited by blademonkey

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...