Jump to content

RegWrite


Recommended Posts

Hello,

I need to RegWrite running the script with a different (admin) account (shift right click -> run as different user).But it is not working.

$root = "c:\Data"
$RegSID  = "S-1-5-21-3798503727-2312606340-957070891-1013"
$username = "test"

$RegSIDpath = "HKEY_USERS\" & $RegSID & "\SOFTWARE\Microsoft\OneDrive\Tenants\18f52f13-d6e1-47e9-801f-xxxxx\"

$OneDriveDefault = $root  & '\' & $Username & '\'& "OneDrive - Default"
RegWrite($RegSIDpath, "DefaultRootDir", "REG_SZ",$OneDriveDefault)

MsgBox(0, '', $username)
MsgBox(0, '', $RegSID)
MsgBox(0, '', $RegSIDpath)

 

Link to comment
Share on other sites

Try:
 

#RequireAdmin

Global $root = "C:\Data"
Global $username = @UserName;"test"
Global $RegSID  = _GetSidAccount($username);"S-1-5-21-3798503727-2312606340-957070891-1013"
Global $RegSIDpath = "HKEY_USERS\" & $RegSID & "\SOFTWARE\Microsoft\OneDrive\Tenants\18f52f13-d6e1-47e9-801f-xxxxx\"
Global $OneDriveDefault = $root  & '\' & $Username & '\'& "OneDrive - Default"

ConsoleWrite($username&@CRLF)
ConsoleWrite($RegSID&@CRLF)
ConsoleWrite($RegSIDpath&@CRLF)

Global $RegWriteReturn = RegWrite($RegSIDpath, "DefaultRootDir", "REG_SZ",$OneDriveDefault)
ConsoleWrite($RegWriteReturn&@CRLF)
MsgBox(Null, "RegWrite()", $RegWriteReturn ? "OK" : "ERROR")

Func _GetSidAccount($sAccount = @UserName, $sSystem = "")
    Local $tData = DllStructCreate("byte SID[256]")
    Local $sCall = DllCall("advapi32.dll", "bool", "LookupAccountNameW", "wstr", $sSystem, "wstr", $sAccount, "struct*", $tData, "dword*", DllStructGetSize($tData), "wstr", "", "dword*", DllStructGetSize($tData), "int*", 0)
    If @error Or Not $sCall[0] Then Return SetError(@error, @extended, "")
    Local $pSID = DllStructGetPtr($tData, "SID")
    Local $iCall = DllCall("advapi32.dll", "bool", "IsValidSid", "struct*", $pSID)
    If @error Or Not $iCall[0] Then Return SetError(@error, @extended, "")
    Local $dCall = DllCall("advapi32.dll", "bool", "ConvertSidToStringSidW", "struct*", $pSID, "ptr*", 0)
    If @error Or Not $dCall[0] Then Return SetError(@error, @extended, "")
    Local $pStringSid = $dCall[2]
    Local $aLen = DllCall("kernel32.dll", "int", "lstrlenW", "struct*", $pStringSid)
    Local $sSID = DllStructGetData(DllStructCreate("wchar Text[" & $aLen[0] + 1 & "]", $pStringSid), "Text")
    DllCall("kernel32.dll", "handle", "LocalFree", "handle", $pStringSid)
    Return SetError(0, 0, $sSID)
EndFunc   ;==>_GetSidAccount

 

Regards,
 

Link to comment
Share on other sites

Thank you for reply:

Works if I run with current user, but I need to run with different user that has admin rights (shift right click Run as diferent user):

Message:

ERROR

user: test 

RegSID: S-1-5-21-3798503727-2312606340-957070891-1013

RegSIDpath: HKEY_USERS\S-1-5-21-3798503727-2312606340-957070891-1013\SOFTWARE\Microsoft\OneDrive\Tenants\18f52f13-d6e1-47e9-801f-xxxxx\

 

Everything looks right, besides ERROR :(

Edited by hendrikhe
Link to comment
Share on other sites

  • Developers

Have you considered x86/x64 with this as you likely run the script in 32 bits mode.
By the way: you never responded to the other thread with your runas challenge. 

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

The thing is that a x86 compiled script will handle the registry as x86 not x64!  From the helpfile:

Quote

When running on 64-bit Windows if you want to read a value specific to the 64-bit environment you have to suffix the HK... with 64 i.e. HKLM64.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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