powershell - 检查 Windows 服务是否存在并在 PowerShell 中删除

标签 powershell windows-services

我目前正在编写一个安装许多 Windows 服务的部署脚本。

服务名称已进行版本控制,因此我想在安装新服务时删除先前的 Windows 服务版本。

如何在 PowerShell 中最好地执行此操作?

最佳答案

您可以使用 WMI 或其他工具来执行此操作,因为在 Powershell 6.0 之前没有 Remove-Service cmdlet ( See Remove-Service doc)

例如:

$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
$service.delete()

或者使用sc.exe工具:

sc.exe delete ServiceName

最后,如果您确实有权访问 PowerShell 6.0:

Remove-Service -Name ServiceName

关于powershell - 检查 Windows 服务是否存在并在 PowerShell 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4967496/

相关文章:

powershell - Windows Server 2008 R2 PowerShell和Internet Explorer 9

c# - "Cannot Invoke this function because the current host does not implement it"

c# - 安装 .NET 开发的 Windows 服务时出错

entity-framework - 如何改进此 F# 代码以使其更加惯用?

如果匹配,powershell 比较 2 个数组输出

Powershell Hyper-V 脚本不起作用? + 自动安装 Windows

c# - 优雅地停止 .net 启动的 Java 进程

.net - 检测代码是否正在作为服务运行

c# - 如何允许 IErrorHandlers 处理 WCF 中的 AccessViolationExceptions?

tfs - 我可以通过 tfpt.exe、tf.exe 或通过 TFS API(来自 powershell)查询变更集吗?