6

我需要将脚本文件夹永久添加到我的 PowerShell 路径(不仅仅是特定会话)。我正在运行以下代码:

[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\scripts", "Machine")

错误如下:

使用“3”个参数调用“SetEnvironmentVariable”的异常:“不允许请求的注册表访问。”

如何获得注册表访问/修复此问题?

编辑:不确定是否有帮助,但我在 Windows Server 2012 上使用 PowerCLI(VMware PowerShell API)。

4

3 回答 3

9

听起来您可能不是以提升管理员身份运行的。默认情况下,我相信 Server 2012 的任务栏上有一个 PowerShell 快捷方式。右键单击它,然后选择“以管理员身份运行”(或类似的东西)。然后尝试在原始帖子中运行该命令。

于 2013-11-20T22:36:53.270 回答
2

将权限授予HKLM\System\CurrentControlSet\Control\Session Manager\Environment所需的用户

于 2017-02-15T21:06:36.150 回答
1

我以非管理员用户身份在 JEA PSSession 中运行脚本(管理员权限不是我们环境中的选项。)@Nipp 的回答直接指出了我的正确方向(当我有足够的声誉时,我会投票给你.) 这应该适用于任何想要允许非管理员更新环境变量(包括路径)的人:

#allow necessary registry permissions to allow updating environment variables
#e.g.:
#Allow-EnvironmentVariableUpdate -Principal 'Power Users'
function Allow-EnvironmentVariableUpdate()
{
    Param(
    [string]$Principal  #name of user or group
    )

    $acl= get-acl -path "hklm:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
    $inherit = [system.security.accesscontrol.InheritanceFlags]"None"
    $propagation = [system.security.accesscontrol.PropagationFlags]"None"
    $rights = "QueryValues,SetValue,CreateSubKey"
    $rule=new-object system.security.accesscontrol.registryaccessrule $Principal,$rights,$inherit,$propagation,"Allow"
    $acl.addaccessrule($rule)
    $acl | set-acl
    "'$Principal' can edit environment variables."
}
于 2018-06-25T21:55:02.450 回答