[英]$ENV:USERNAME returns the machine name when SCCM runs a script as admin with a non-admin logged in
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. 它需要将应用程序数据复制到$ENV:USERNAME\\AppData\\<dir>\\*
(除其他外),并且需要以管理员身份运行才能正常工作。
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. 我发现的问题是,当脚本在具有管理员特权的非管理员配置文件上运行时,环境变量$ENV:USERNAME
返回计算机名称,而不是实际登录的用户名。
Is there a way around this? 有没有解决的办法?
You can either use ([Security.Principal.WindowsIdentity]::GetCurrent()).Name
or you get the profile path from the registry : 您可以使用([Security.Principal.WindowsIdentity]::GetCurrent()).Name
或从注册表获取配置文件路径:
HKEY_CURRENT_USER\Volatile Environment\APPDATA
HKEY_CURRENT_USER\Volatile Environment\LOCALAPPDATA
HKEY_CURRENT_USER\Volatile Environment\USERPROFILE
Choose your weapon :) 选择你的武器 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.