【发布时间】:2011-11-03 15:57:36
【问题描述】:
我想修改应用程序的路径,但这样做会破坏它,因为服务仍指向旧位置。
通过转到Administrative Tools > Services,您可以打开一个属性对话框并查看Path to executable,但无法更改它。
用户有什么方法可以修改服务路径而无需重新安装应用程序?
【问题讨论】:
我想修改应用程序的路径,但这样做会破坏它,因为服务仍指向旧位置。
通过转到Administrative Tools > Services,您可以打开一个属性对话框并查看Path to executable,但无法更改它。
用户有什么方法可以修改服务路径而无需重新安装应用程序?
【问题讨论】:
它涉及到编辑注册表,但服务信息可以在HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services中找到。找到您要重定向的服务,找到 ImagePath 子键并更改该值。
【讨论】:
sc 或sc.exe 更好,风险更小
还有this approach seen on SuperUser使用sc命令行而不是修改注册表:
sc config <service name> binPath= <binary path>
注意:binPath= 后面的空格很重要。您还可以使用以下方式查询当前配置:
sc qc <service name>
这会显示类似于以下内容的输出:
[SC] 查询服务配置成功
SERVICE_NAME:服务名称
TYPE : 10 WIN32_OWN_PROCESS START_TYPE : 2 AUTO_START ERROR_CONTROL : 1 NORMAL BINARY_PATH_NAME : C:\Services\ServiceName LOAD_ORDER_GROUP : TAG : 0 DISPLAY_NAME : <Display name> DEPENDENCIES : SERVICE_START_NAME : user-name@domain-name
【讨论】:
sc config mssqlserver binPath= "\"F:\SQL DATA\MSSQL10.MSSQLSERVER\MSSQL\Binn\sqlservr.exe\" -sMSSQLSERVER"
sc qc X 之后得到[SC] QueryServiceConfig FAILED 122:,请使用sc qc X 1000 参见this
sc.exe,而不是赤裸裸的 sc
您也可以使用 PowerShell:
Get-WmiObject win32_service -filter "Name='My Service'" `
| Invoke-WmiMethod -Name Change `
-ArgumentList @($null,$null,$null,$null,$null, `
"C:\Program Files (x86)\My Service\NewName.EXE")
或者:
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\My Service" `
-Name ImagePath -Value "C:\Program Files (x86)\My Service\NewName.EXE"
【讨论】:
打开运行(win+R),输入“Regedit.exe”,打开“注册表编辑器”,进入
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services
找到“Apache2.4”打开文件夹在右侧找到“ImagePath”,打开“ImagePath” 在“value Data”下输入以下路径:
"C:\xampp\apache\bin\httpd.exe" -k runservice 其他人的敌人 XAMPP 指向安装 Apache 的位置并在里面找到 bin 文件夹 "C:(Apache安装位置)\bin\httpd.exe" -k runservice
【讨论】:
对于像我这样试图修改 MongoDB 服务以使用身份验证的人来说,对这个 @CodeMaker 的答案稍作修改。
当我查看“服务”中的“可执行文件路径”时,执行的行已经包含语音标记。所以我不得不对他的例子做些小改动。
具体来说。
对我来说,路径是(注意语音标记)
"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --config "C:\Program Files\MongoDB\Server\4.2\bin\mongod.cfg" --service
在命令行中输入
sc config MongoDB binPath= "<Modified string with \" to replace ">"
在我的情况下是
sc config MongoDB binPath= "\"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe\" --config \"C:\Program Files\MongoDB\Server\4.2\bin\mongod.cfg\" --service -- auth"
【讨论】:
service-run 而不是 config)。
您不能直接编辑执行服务的路径。为此,您可以使用 sc 命令,
SC CONFIG ServiceName binPath= "Path of your file"
例如:
sc config MongoDB binPath="I:\Programming\MongoDB\MongoDB\bin\mongod.exe --config I:\Programming\MongoDB\MongoDB\bin\mongod.cfg --service"
【讨论】:
使用Invoke-WmiMethod 的替代方法是使用较新的CIM cmdlet。这也避免了对 @($null,$null...) 对象的需要,如上一个答案所示。
Get-CimInstance win32_service -Filter "Name='My Service'" | Invoke-CimMethod -MethodName Change -Arguments @{PathName="C:\Program Files\My Service\NewName.exe"}
【讨论】:
使用 'SC' 命令再深入一点,我们能够提取所有 'Services Name' 并获得所有 'QueryServiceConfig' :)
>SC QUERY > "%computername%-services.txt" [enter]
>FIND "SERVICE_NAME: " "%computername%-services.txt" /i > "%computername%-services-name.txt" [enter]
>NOTEPAD2 "%computername%-services-name.txt" [enter]
然后,继续“CMD”..
>FOR /F "DELIMS= SKIP=2" %S IN ('TYPE "%computername%-services-name.txt"') DO @SC QC "%S" >> "%computername%-services-list-config.txt" [enter]
>NOTEPAD2 "%computername%-services-list-config.txt" [enter]
原始数据已准备好用于提供“未来的批处理文件”,因此结果如下所示!!!
+ -------------+-------------------------+---------------------------+---------------+--------------------------------------------------+------------------+-----+----------------+--------------+--------------------+
| SERVICE_NAME | TYPE | START_TYPE | ERROR_CONTROL | BINARY_PATH_NAME | LOAD_ORDER_GROUP | TAG | DISPLAY_NAME | DEPENDENCIES | SERVICE_START_NAME |
+ -------------+-------------------------+---------------------------+---------------+--------------------------------------------------+------------------+-----+----------------+--------------+--------------------+
+ WSearch | 10 WIN32_OWN_PROCESS | 2 AUTO_START (DELAYED) | 1 NORMAL | C:\Windows\system32\SearchIndexer.exe /Embedding | none | 0 | Windows Search | RPCSS | LocalSystem |
+ wuauserv | 20 WIN32_SHARE_PROCESS | 2 AUTO_START (DELAYED) | 1 NORMAL | C:\Windows\system32\svchost.exe -k netsvcs | none | 0 | Windows Update | rpcss | LocalSystem |
但是,HTML 会更容易:D
欢迎任何好的改进想法V^_^
【讨论】:
sc,这样更容易,更不容易搞砸。
我只是想为 Git Bash 用户添加你应该将路径放在单引号 ' ' 中
sc config <service name> binPath='<binary path>'
在例如sc config MongoDB binPath='"C:\Program Files\MongoDB\Server\5.03\bin\mongod.exe\" --config \"C:\Program Files\MongoDB\Server\5.03\bin\mongod.cfg\" --service -- auth"'
这有助于我在 Windows 10 上使用 Git Bash 更新服务的路径
【讨论】:
您可以删除服务:
sc delete ServiceName
然后重新创建服务。
【讨论】: