Jump to content

StdinWrite and Special characters


Go to solution Solved by sahsanu,

Recommended Posts

I am working on a script that is gonna ease the domain migration of 100+ computers, one of the steps in the script is to create a local account and add it to the local administrators group. 

This all works fine on my test machine running english windows, but on a Swedish OS where the Administrator group is called "Administratörer" it doesnt find the group because it misinterprets the letter Ö.

Here is a short script demonstrating the same problem but when adding a user with Ö in the name. The result will be a user called "tempadmin÷"

#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator


#include <Constants.au3>
#include <MsgBoxConstants.au3>


$RunPid=Run(@ComSpec & ' /K', '',"",$STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
Sleep(1500)
StdinWrite($RunPid, 'Net User ' & "tempadminö" & ' ' & "secretpassword" & ' /add' & @CRLF)
Sleep(2000)
$result=StdoutRead($RunPid)
StdinWrite($RunPid)
ProcessClose($RunPid)


MsgBox(4096,"test",$result)

 

I have tried playing around with chcp and cmd /U, cmd /A but haven't really gotten any where, is there anyone that can nudge me in the right direction?

FYI: to my knowledge this script must be compiled and then run as admin before it will create any user.

Edited by Lantz
Link to comment
Share on other sites

Hello,

You said that you used chcp with no result but as it is working to me, could you try this?.

$RunPid=Run(@ComSpec & ' /k chcp 1250', '',"",$STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)

Cheers,

sahsanu

 

That seems to solve the problem!!

Thanks for helping out.

Link to comment
Share on other sites

Code page 1250 will only work with characters in CP1250. Welcome to the headache of so-called ANSI.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I made some functions to help with languages changes.

I share it with you, maybe it can help.

It's very slow because of the use of Winnt object...

Local $sUser = "tempadminö"
Local $sPassword = "secretpassword"

Local $sAdminGroupName = _GetAdminGroupName()

If _CreateLocalUser($sUser, $sPassword) Then _AddUserToGroup($sAdminGroupName, $sUser)



Func _GetAdminGroupName()
    Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    If $objWMIService = 0 Then SetError(1, 0, 0)
   
    $colGroup = $objWMIService.ExecQuery("select * from Win32_Group Where SID = 'S-1-5-32-544'")
    If NOT IsObj($colGroup) Then SetError(1, 0, 0)

    For $oGroup In $colGroup
        Return $oGroup.Name
    Next
    
    Return 0
EndFunc



Func _CreateLocalUser($sUsername, $sPassword, $bPasswordNeverExpires = 1, $bUserCannotChangePassword = 1)
    Local $colAccounts = ObjGet("WinNT://" & @ComputerName & "")
    Local $objUser = $colAccounts.Create("user", $sUsername)
    Local $Flags = $objUser.Get("UserFlags")
    
    $objUser.SetPassword($sPassword)

    If $bPasswordNeverExpires Then $Flags = BitOr($Flags, 0x10000)
    If $bUserCannotChangePassword Then $Flags = BitOr($Flags, 0x0040)
    
    $objUser.put("Userflags", $Flags )

    $objUser.setinfo
    If @error Then Return SetError(1, 0, 0)
    
    Return 1
EndFunc



Func _AddUserToGroup($sGroup, $sUser)
    Local $objGroup = ObjGet("WinNT://" & @ComputerName & "/" & $sGroup & ",group")
    If NOT IsObj($objGroup) Then Return 0
    
    Local $objUser = ObjGet("WinNT://" & @ComputerName & "/" & $sUser & ",user")
    If NOT IsObj($objUser) Then Return 0
    
    $objGroup.Add($objUser.ADsPath)
    If @error Then Return SetError(1, 0, 0)
    
    Return 1
EndFunc
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...