[英]Modifying the "Path to executable" of a windows service
我想修改我的应用程序的路径,但这样做会破坏它,因为该服务仍然指向旧位置。
通过转到Administrative Tools > Services
,您可以打开一个属性对话框并查看Path to executable
,但无法更改它。
有什么方法可以让用户无需重新安装应用程序就可以修改服务路径?
它涉及编辑注册表,但可以在HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services
找到服务信息。 找到要重定向的服务,找到ImagePath
子项并更改该值。
在 SuperUser 上也看到了这种方法,它使用sc
命令行而不是修改注册表:
sc config <service name> binPath= <binary path>
注意: binPath=
后面的binPath=
很重要。 您还可以使用以下方法查询当前配置:
sc qc <service name>
这将显示类似于以下内容的输出:
[SC] QueryServiceConfig 成功
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
你也可以用 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"
打开 Run(win+R) ,输入“Regedit.exe”,打开“注册表编辑器”,进入
HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services
找到“ Apache2.4 ”打开文件夹,在右侧找到“ ImagePath ”,打开“ value Data ”下的“ImagePath”放入如下路径:
"C:\\xampp\\apache\\bin\\httpd.exe" -k runservice foe XAMPP for others 指向安装Apache的位置,在里面找到bin文件夹"C:(Apache安装位置)\\bin\\httpd.exe" -k 运行服务
对这个@CodeMaker 的答案稍作修改,适用于像我这样试图修改 MongoDB 服务以使用身份验证的人。
当我查看“服务”中的“可执行文件路径”时,执行的行已经包含语音标记。 所以我不得不对他的例子做一些小的修改。
再具体一点。
对我来说路径是(注意语音标记)
"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"
您不能直接编辑执行服务的路径。 为此,您可以使用 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”命令更深入一点,我们能够提取所有“服务名称”并获得所有“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^_^
我只是想为 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
然后重新创建服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.