Using Environment Variables in PowerShell Scripts

hey guys, how's everyone doing?

I'm exploring possibilities with scripts in atera and would like recommendations for working with environment variables.

In my tests I noticed that the script submission occurs through a powershell session with different values than the real user.

Values like $env:USERNAME may not exactly match the actual user of the machine.

My context of use is to subscribe to TeamViewer groups for example, cost to subscribe to the devices by getting the user/device name.

Exemple: My real value when I run whoami is AzureAD\MarcosSantos

'''

$userlocal = whoami
Write-Host $userlocal

'''

Comments

  • AlexYoungNSM
    AlexYoungNSM Member Posts: 21 ✭✭

    Hi,

    That's because the Atera service runs at NT AUTH\SYSTEM, if you ran your script as current user it would use your AzureAD or whatever that user that's logged in as, if nobody is logged in it would say "No user logged in" and won't run the script.

  • marcos.santos
    marcos.santos Member Posts: 5

    Thank you Alex, the option is in my face all the time. Making the change worked well.

    Before reading your answer I got a positive result with the following command:

    Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty UserName

    Just documenting the discovery, thank you for the support.