我想在 Windows 7(是的...旧版)中创建一个包含简短命令行脚本的桌面快捷方式,通过反复运行该脚本,将某个服务从启动类型切换到启动类型manual
并切换回启动类型。从 切换到时,服务也应该被终止,而从 切换到 时则不执行任何操作。基本上,该脚本应该充当相关服务的“电源开/关”开关,就像您电脑电源上的开关一样。disabled
manual
disabled
disabled
manual
一个可能的 Pythonesque 版本可能如下所示:
if service.startup_type = 'manual':
service.startup_type = 'disabled'
service.stop()
print(f'{service.name} disabled.')
time.sleep(1.0)
else:
service.startup_type = 'manual'
print(f'{service.name} enabled.')
time.sleep(1.0)
注意:由于我还不熟悉 PowerShell,所以我想使用 CMD,但我愿意接受建议。
答案1
这是为您提供的示例脚本。用于切换 Windows 更新和受信任的安装程序服务的批处理脚本。.wuauserv
用您选择的服务替换,并TrustedInstaller
相应地删除引用。
备份链接(互联网档案馆)
答案2
我们每天都是从某个地方开始的。;-} 您所需要的一切都在 PowerShell 帮助文件中。
Get-Help -Name Get-Service -Full
Get-Help -Name Get-Service -Examples
Get-Help -Name Start-Service -Full
Get-Help -Name Start-Service -Examples
Get-Help -Name Stop-Service -Full
Get-Help -Name Stop-Service -Examples
Get-Service | Select-Object -First 1 | Get-Member
# Results
<#
TypeName: System.ServiceProcess.ServiceController
Name MemberType Definition
---- ---------- ----------
Name AliasProperty Name = ServiceName
RequiredServices AliasProperty RequiredServices = ServicesDependedOn
Disposed Event System.EventHandler Disposed(System.Object, System.EventArgs)
Close Method void Close()
Continue Method void Continue()
CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
ExecuteCommand Method void ExecuteCommand(int command)
GetHashCode Method int GetHashCode()
GetLifetimeService Method System.Object GetLifetimeService()
GetType Method type GetType()
InitializeLifetimeService Method System.Object InitializeLifetimeService()
Pause Method void Pause()
Refresh Method void Refresh()
Start Method void Start(), void Start(string[] args)
Stop Method void Stop()
WaitForStatus Method void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desiredStatus), void WaitForStatus(System.ServiceProc...
CanPauseAndContinue Property bool CanPauseAndContinue {get;}
CanShutdown Property bool CanShutdown {get;}
CanStop Property bool CanStop {get;}
Container Property System.ComponentModel.IContainer Container {get;}
DependentServices Property System.ServiceProcess.ServiceController[] DependentServices {get;}
DisplayName Property string DisplayName {get;set;}
MachineName Property string MachineName {get;set;}
ServiceHandle Property System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}
ServiceName Property string ServiceName {get;set;}
ServicesDependedOn Property System.ServiceProcess.ServiceController[] ServicesDependedOn {get;}
ServiceType Property System.ServiceProcess.ServiceType ServiceType {get;}
Site Property System.ComponentModel.ISite Site {get;set;}
StartType Property System.ServiceProcess.ServiceStartMode StartType {get;}
Status Property System.ServiceProcess.ServiceControllerStatus Status {get;}
MSDN ScriptMethod System.Object MSDN();
ToString ScriptMethod System.Object ToString();
#>