AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 70603
Accepted
Nexus
Nexus
Asked: 2009-10-02 16:24:22 +0800 CST2009-10-02 16:24:22 +0800 CST 2009-10-02 16:24:22 +0800 CST

从命令行更新环境变量(Windows 2008 Server Core)

  • 772

相当直接的问题。我需要在 Windows Server 2008 Core 中更新我的 PATH 环境变量。考虑到没有 GUI,这是如何从命令行完成的?

windows-server-2008
  • 5 5 个回答
  • 20460 Views

5 个回答

  • Voted
  1. Best Answer
    Richard
    2009-10-03T00:26:57+08:002009-10-03T00:26:57+08:00

    要进行持久更改,请使用setx.

    例如,要将文件夹添加到当前路径的末尾,请使用:

    SETX Path %Path%;C:\MyFolder
    

    您的更改在当前会话中不可见cmd.exe,但它将在以后的所有会话中可见。

    SETX还允许在远程系统上设置系统环境变量。

    • 6
  2. user3347790
    2016-05-18T21:21:50+08:002016-05-18T21:21:50+08:00

    处理 Path 变量很棘手,因为它是系统 Path 和用户 Path 变量的组合。以前的答案没有考虑到这一点。例如

    SETX PATH %PATH%;C:\MyFolder
    

    将用户路径设置为整个当前系统路径,加上用户路径,然后将 ';C:\MyFolder' 附加到该路径。如果我使用过,SETX /M PATH %PATH%;C:\MyFolder那么系统路径会将当前用户路径添加到其中。

    对除 Path 之外的任何环境变量使用SETXor都可以。SETX /M不幸的是,处理路径变量很痛苦,因为它涉及更新注册表并且能够附加新值,我们必须首先将注册表项复制到环境变量中,将要添加的目录附加到路径中,然后编写结果返回注册表。

    还有一个问题是路径变量存储为 REG_EXPAND_SZ 字符串,并且系统路径通常包含对%SystemRoot%. 这意味着您用于读取、操作和写入 Path 变量的任何机制在理想情况下都不应扩展任何内容。

    最后,没有用户路径变量是很常见的,这意味着更新路径的代码需要考虑如果您尝试读取不存在的变量会出现的错误。

    下面是一些示例代码,可以通过更新注册表中的值来更新路径。

    @echo off
    setlocal ENABLEDELAYEDEXPANSION
    rem
    rem Add c:\bin to the path
    rem
    
    rem
    rem
    rem There are two values we can use for the PATHKEY. The one that starts
    rem with HKEY_LOCAL_MACHINE sets the path that's used by all processes and
    rem users on the system. The PATHKEY that starts with HKEY_CURRENT_USER
    rem updates the part of the Path that only visible to processes running
    rem under the current user account.
    rem
    set PATHKEY=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    set PATHKEY=HKEY_CURRENT_USER\Environment
    set PATHVAR=Path
    set TMPFILE=%TEMP%\addpath.txt
    set ADDPATH=c:\bin
    
    rem
    rem Read the Path value from the registry into a file. I could have coded 
    rem this with no temporary file by using:
    rem     for /f "delims=" %%i in ('REG QUERY "%PATHKEY%" /v "%PATHVAR%" 2>nul:') do set XLINE=%%i
    rem However, having the temporary file was useful for debugging and as 
    rem updating the path is something we don't often do we don't care if 
    rem doing so is a bit slower. 
    rem
    REG QUERY "%PATHKEY%" /v "%PATHVAR%" >"%TMPFILE%" 2>nul:
    if errorlevel 1 goto :newpath
    
    rem
    rem REG QUERY outputs several lines. We only care about the last non-blank line
    rem Fortunately, a 'for' loop ignores blank lines.
    rem
    for /f "delims=" %%i in (%TMPFILE%) do set XLINE=%%i
    
    rem
    rem Extract the value from the Path variable. Here's what we expect to see 
    rem in XLINE though with with spaces shown as ~ symbols: 
    rem
    rem    ~~~~Path~~~~REG_EXPAND_SZ~~~~Path-is-here..."
    rem
    rem See below for other ways we can extract the path value from XLINE.
    rem
    for /f "tokens=1,2,* delims= " %%i in ("!XLINE!") do set VARNAME=%%i & set VARTYPE=%%j & set XVALUE=%%k
    
    rem
    rem Append an element to the Path.
    rem
    set NEWPATH=!XVALUE!;!ADDPATH!
    
    rem
    rem Update the path
    rem
    REG ADD "%PATHKEY%" /v "%PATHVAR%" /t REG_EXPAND_SZ /d "!NEWPATH!" /f
    
    goto :done
    
    
    rem
    rem The Path variable does not exist and so create it
    rem
    :newpath
    REG ADD "%PATHKEY%" /v "%PATHVAR%" /t REG_EXPAND_SZ /d "!ADDPATH!"
    goto :done
    
    rem
    rem Delete the temporary file.
    rem
    :done
    del "%TMPFILE%"
    endlocal
    goto :eof
    
    rem
    rem Here are some variations for parsing XLINE to extract the value.
    rem
    
    rem
    rem Quick and dirty method. It takes advantage of that REG QUERY returns a 
    rem line with four spaces, the variable name which we know is four 
    rem characters, four spaces, the variable type which we know is 
    rem REG_EXPAND_SZ and is 13 characters, four spaces, and then the value. As 
    rem some systems may be using REG_SZ Path strings the quick and dirty method 
    rem seems like a bad idea. 
    rem
    set XVALUE=!XLINE:~29!
    
    rem
    rem One flaw with the method I used in the code above is that you are 
    rem allowed to have spaces in variable names. Here's a slight modification 
    rem to the code to support spaces in variable names. It takes advantage of 
    rem the fact that REG VIEW always puts four spaces each of the fields. We 
    rem first translate instances of four spaces into vertical bars and use that 
    rem character as the delimiter when parsing the line. 
    rem
    rem I could have used any character as the delimiter as long as it's one 
    rem that will not appear in the variable name, the variable type, or as the 
    rem first character(s) of the variable's value. Some people use a tab here. 
    rem I chose not to do that in this example as the tabs are not visible. 
    rem
    rem The code still has a flaw in that it will fail if the variable name 
    rem contains four consecutive spaces.
    rem
    set XLINE=!XLINE:    =^|!
    for /f "tokens=1,2,* delims=|" %%i in ("!XLINE!") do set VARNAME=%%i & set VARTYPE=%%j & set XVALUE=%%k
    
    • 2
  3. Massimo
    2009-10-02T16:27:42+08:002009-10-02T16:27:42+08:00

    自 DOS 时代以来一直如此:

    SET PATH = C:\SomeDir
    SET PATH = %PATH%;C:\AnotherDir
    
    • 1
  4. BrianP
    2009-10-02T16:30:03+08:002009-10-02T16:30:03+08:00

    来自 Mueller 的书 Administering Windows Server 2008 core - use WMIC

    WMIC 环境 Name="Path" SET VariableValue="C:\Temp;%PATH%

    • 1
  5. MikeBeaton
    2021-01-19T01:58:35+08:002021-01-19T01:58:35+08:00

    如果你想设置一个包含空格的路径或其他环境变量,我发现它更容易使用regedit——你可以从 Server Core 上的命令提示符开始。

    系统范围的环境变量在HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment,用户环境变量在HKCU\Environment.


    @user3347790 的回答提出了一些有效的观点:上面的许多方法都将用扩展路径替换路径(例如%ProgramFiles%,或%SystemRoot%或%UserProfile%将被永久扩展),并且它们还将机器和用户路径合并为一个。基于此,从该答案改编代码(如果您需要代码)或仅使用regedit(如果您只想手动进行一些更改)可能更安全......

    • 0

相关问题

  • 文件复制到分支机构

  • 对于 ASP.Net 应用程序,Windows 64 位相对于 32 位的主要优势是什么?

  • Windows Server 2008 Hyper-V 虚拟化服务器的最佳 RAID 配置?

  • 远程连接 sql server 不工作,但如果防火墙禁用它呢?

  • 无法从 SQL Server 2008 备份数据库

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve