$ENV:USERNAME returns the machine name when SCCM runs a script as admin with a non-admin logged in

≡放荡痞女 提交于 2020-01-17 04:49:07

问题


I have a script that needs to run from Software Center when a non-admin is logged in.

It needs to copy application data to $ENV:USERNAME\AppData\<dir>\* (among other things) and it needs to run as administrator to work properly.

The problem that I've discovered is that when the script is run on a non-admin profile with admin privileges the environment variable $ENV:USERNAME returns the machine name rather than the actual logged in username.

Is there a way around this?


回答1:


You can either use ([Security.Principal.WindowsIdentity]::GetCurrent()).Name or you get the profile path from the registry :

HKEY_CURRENT_USER\Volatile Environment\APPDATA
HKEY_CURRENT_USER\Volatile Environment\LOCALAPPDATA
HKEY_CURRENT_USER\Volatile Environment\USERPROFILE

Choose your weapon :)



来源:https://stackoverflow.com/questions/33767598/envusername-returns-the-machine-name-when-sccm-runs-a-script-as-admin-with-a-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!