I need to execute a PS which creates registry entries for the current user and right after restarts a service (admin rights needed). I found a module called RunAsUser that seems to do this very nicely. https://github.com/KelvinTegelaar/RunAsUser
Install-PackageProvider -Name "NuGet" -RequiredVersion "2.8.5.201" -Force -Confirm:$False
install-module RunAsUser -Confirm:$False -Force
$scriptblock = {
Set-ItemProperty -Path "HKCU:\HKEY_CURRENT_USER\Software\Palo Alto Networks\GlobalProtect\Settings\" -Name LastUrl -Value "vpn.xxx.yyy"
}
invoke-ascurrentuser -scriptblock $scriptblock
Restart-Service -Name PanGPS
But when I run it with a user with local admin rights I get the following error asking for SYSTEM rights.
invoke-ascurrentuser : Not running with correct privilege. You must run this script as system or have the SeDelegateSessionUserImpersonatePrivilege token. At C:\Temp\MoveFromVPN2toVPN.ps1:30 char:1
+ invoke-ascurrentuser -scriptblock $scriptblock
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-AsCurrentUser
Any idea on how to become SYSTEM? I managed to obtain a shell with SYSTEM using psexec command.
psexec.exe -i -s powershell.exe
From there my script works flawless, but I would like to make it programmatically directly inside my script.
Any idea how to do this?
thanks.