检查 Windows 服务是否存在并在 PowerShell 中删除 [英] Check if a Windows service exists and delete in PowerShell

查看:42
本文介绍了检查 Windows 服务是否存在并在 PowerShell 中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently writing a deployment script that installs a number of Windows services.

服务名称是版本化的,所以我想在安装新服务的过程中删除之前的 Windows 服务版本.

The services names are versioned, so I want to delete the prior Windows service version as part of the installs of the new service.

我怎样才能最好地在 PowerShell 中做到这一点?

How can I best do this in PowerShell?

推荐答案

您可以为此使用 WMI 或其他工具,因为在 Powershell 6.0 (参见 Remove-Service 文档)

You can use WMI or other tools for this since there is no Remove-Service cmdlet until Powershell 6.0 (See Remove-Service doc)

例如:

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

或者使用 sc.exe 工具:

sc.exe delete ServiceName

最后,如果您确实可以访问 PowerShell 6.0:

Finally, if you do have access to PowerShell 6.0:

Remove-Service -Name ServiceName

这篇关于检查 Windows 服务是否存在并在 PowerShell 中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆