正在设置 Git 服务器:为什么在使用 Powershell 设置变量时访问被拒绝

Setting up Git Server: Why is access denied while setting variables using Powershell

我正在尝试在 Windows 上设置一个本地 Git 服务器,正如本网站所述:https://github.com/PowerShell/Win32-OpenSSH/wiki/Setting-up-a-Git-server-on-Windows-using-Git-for-Windows-and-Win32_OpenSSH。当我尝试设置变量 $machinePath ($machinePath = ${C:\Program Files\Git\mingw64\bin}::GetEnvironmentVariable('Path', 'MACHINE')) 时,我收到一条错误消息,告诉我访问路径 C:\Program Files\Git\mingw64\bin 被拒绝。我以管理员身份执行 运行 PowerShell。谁能告诉我如何解决这个问题?

链接指南的第一步是 运行 这些 命令将 git 添加到全局路径。除非您在其他地方安装了 git,否则没有理由更改它们:

$gitPath = Join-Path -Path $env:ProgramFiles -ChildPath "git\mingw64\bin"
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'MACHINE')
[Environment]::SetEnvironmentVariable('Path', "$gitPath;$machinePath", 'Machine')

请注意,设置 machine-level 环境变量 需要 running-as-administrator。


关于 powershell 语法:

# accessing a class's member method/property is done via 
[ClassName]::Method('parameter1','p2')

# curly brackets are usually script blocks or hash tables
$sciptblock = { ping localhost }
$hashtable = @{ key1 = 'value1'; key2 = 'value2'}

# they can also be used to set a variable name with spaces:
${a b c} = 'abc'

# BUT if you have a file path, powershell will GET/SET the data of the file:
${c:\temp\test.txt} = 'test file'