Jump to content

Run only one function as admin


Go to solution Solved by UE_morf_boon,

Recommended Posts

Hi!
I have a program that has a specific function that renames the computer.
The entire program should be executed only on behalf of the current user, because some registry keys in the HKCU are being changed.
The current user does not have administrator rights, so #Requireadmin is not suitable as a solution.
The administrator accounts on the computers are different, so "RunAs" is not suitable.

Is there a way to trigger a UAC, when running a function?

The function code, if you need:

Func _RenamePC($Input2)
    Local $Name = GUICtrlRead($Input2)
    $objWMIService = ObjGet("winmgmts:\root\cimv2")
    For $objComputer In $objWMIService.InstancesOf("Win32_ComputerSystem")
        $oReturn = $objComputer.rename($Name)
    Next
EndFunc

 

Link to comment
Share on other sites

  • Solution

Nevermind, solved the problem via "powershell".

Solution code:

Func _RenamePC($Name)
    ShellExecuteWait("C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe", "start-process -verb runas 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe' "& _
    "-argumentlist '$compcname = get-wmiobject win32_computersystem; foreach($comp in $compcname){$oreturn = "& _
    "$comp.rename(''"&$Name&"'')}"& _
    "'", "", "", @SW_HIDE)
EndFunc

 

Edited by UE_morf_boon
Fix solution
Link to comment
Share on other sites

  • 2 weeks later...

Interesting !

I was searching a method to execute a powershell script as Admin from a non admin AutoIT script, and so I adapted your example this way:

Func _executeElevatedPS()
    ShellExecuteWait("powershell.exe", "start-process -verb runas 'powershell.exe' "& _
    "-argumentlist '-executionpolicy unrestricted -File  C:\temp\wp.ps1'", "", "", @SW_HIDE)
EndFunc

 

Link to comment
Share on other sites

Adding this:

Func _Set_Network_Private()
    ShellExecuteWait("powershell.exe", "start-process -verb runas 'powershell.exe' " & _
            "-argumentlist @('-executionpolicy unrestricted',' -command Get-NetConnectionProfile -InterfaceAlias Ethernet | Set-NetConnectionProfile -NetworkCategory Private -Confirm:$false -PassThru')", '', '', @SW_SHOW)
EndFunc

from this post

It's the same logic, but executing multiple powershell instructions without an external .ps1 file

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

×
×
  • Create New...