This message was deleted.
# _general
s
This message was deleted.
g
@Adam Yarborough How would I get the logged in user then? The session would be running as whatever the scheduled task is running as
a
So, @Hal Lange was right. Easiest way is Scheduled task, run as system, with the "Run with highest privileges" checked, in the actions tab: start a program, C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe, arguments: -NoProfile -ExecutionPolicy Bypass -file "C:\local\myscript.ps1" or whatever..
h
You can use that script to look to see who the current logged in user is, and add that user to the local admins group if domain user.
a
Copy code
$User = (Get-WmiObject -Class win32_computersystem -ComputerName localhost).UserName
Add-LocalGroupMember -Group "Backup Operators" -Member $User
or similar
h
maybe exclude if already a member of an known Admin group
@Adam Yarborough that script would yield System as that is the context of what the scheduled task would be running as.....
could query WMI for currently logged in user or just look at profile directories to see who is the current user
win32_logonSession would be the WMI class you would probably want
a
@Hal Lange I tested before replying and only users were returned, not system, which is why I included the "Backup Operators" group.
Copy code
$IsElevated = ([Security.Principal.WindowsPrincipal]     [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
$User = (Get-WmiObject -Class win32_computersystem -ComputerName localhost).UserName
Add-LocalGroupMember -Group "Backup Operators" -Member $User

$body = "IsElevated: $IsElevated`nUser: $user added to Backup Operators"
Send-MailMessage -To "ayarborough" -From "ayarborough@domain" -SmtpServer "smtp.domain" -Subject "Test" -Body $body
was the full script
h
hmm..... i have had it return what context it was running under... so i stopped using that one a couple of years ago for that reason.... maybe it is playing nice again.
i switched to the win32_LogonSession because it can differentiate between logged in/scripting/console/RDP/ICA.
a
I'll have to keep that in mind.
good to know.
g
Thanks I will give that a try. I had it setup but when it was running as System I was only getting the system account instead of the actual user