hendrikhe

RegWrite

7 posts in this topic

#1 ·  Posted

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)

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

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,
 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted

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 


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

Share this post


Link to post
Share on other sites

#5 ·  Posted

Hey Jos,

I compiled this script in x86. Computers that I will run script are x64. Do I have to make any changes?

PS: will check other thread soon. I am short in time 😓

Share this post


Link to post
Share on other sites

#6 ·  Posted

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


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

Share this post


Link to post
Share on other sites

#7 ·  Posted

Thanks, chaged script to

 

 

if @OSArch = "X64" or @OSArch = "IA64" then
    $RegSIDpath = "HKEY_USERS64\" & $RegSID & "\SOFTWARE\Microsoft\OneDrive\Tenants\18f52f13-d6e1-47e9-801f-xxxx\"
Else
    $RegSIDpath = "HKEY_USERS\" & $RegSID & "\SOFTWARE\Microsoft\OneDrive\Tenants\18f52f13-d6e1-47e9-801f-xxxx\"
EndIf

 

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