简体   繁体   English

当SCCM以管理员身份运行脚本并以非管理员身份登录时,$ ENV:USERNAME返回计算机名称

[英]$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.

相关问题 SCCM Powershell脚本中的$ ENV:USERNAME - $ENV:USERNAME in SCCM Powershell script Powershell 脚本 - 仅以管理员身份运行某些命令,以非管理员身份运行脚本的 rest - Powershell Script - Run certain commands only as admin and rest of script as non-admin 以非管理员身份远程重启服务? - Restarting service remotely as non-admin? 从非管理上下文执行时 Get-CrmConnection 失败 (3.3.0.857) - Get-CrmConnection failing when executed from non-admin context (3.3.0.857) 获取非管理员帐户的IIS应用程序池WMI对象 - Get IIS application pool WMI object for a Non-admin account 使用 Powershell 在非管理员帐户上执行远程进程 - Execute a remote process on a non-admin account using Powershell 如何从提升的 PowerShell 控制台以非管理员身份运行进程? - How to run a process as non-admin from an elevated PowerShell console? 修复脚本运行时,SCCM 合规性状态始终为“合规” - SCCM Compliance state always 'Compliant' when remediation script runs 在非管理员用户下从远程运行PS cmdlet - 访问被拒绝 - Running PS cmdlets from remote under non-admin users - Access Denied 从多个Windows服务器获取非管理文件共享的列表 - Obtain a list of non-admin file shares from multiple Windows servers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM