Answers:
您可以为此使用WMI或其他工具,因为在Remove-Service
Powershell 6.0之前没有cmdlet(请参阅Remove-Service文档)
例如:
$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
$service.delete()
或使用sc.exe
工具:
sc.exe delete ServiceName
最后,如果您确实有权使用PowerShell 6.0:
Remove-Service -Name ServiceName
$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
$service | Remove-WmiObject
$service.delete()
”
Stop-Service 'servicename'; Get-CimInstance -ClassName Win32_Service -Filter "Name='servicename'" | Remove-CimInstance
我发现运行正确的工具没有任何危害,我发现运行(来自Powershell)
sc.exe \\server delete "MyService"
没有很多依赖关系的最可靠的方法。
.exe
我看到你的评论之前,我已经省略了。现在它为我工作。
\\server
如果服务是本地的,则将服务器名称()省略。
%
和$_
如果只想检查服务是否存在:
if (Get-Service "My Service" -ErrorAction SilentlyContinue)
{
"service exists"
}
我使用了“ -ErrorAction SilentlyContinue”解决方案,但后来遇到了一个问题,即它留下了ErrorRecord。因此,这是仅使用“ Get-Service”检查服务是否存在的另一种解决方案。
# Determines if a Service exists with a name as defined in $ServiceName.
# Returns a boolean $True or $False.
Function ServiceExists([string] $ServiceName) {
[bool] $Return = $False
# If you use just "Get-Service $ServiceName", it will return an error if
# the service didn't exist. Trick Get-Service to return an array of
# Services, but only if the name exactly matches the $ServiceName.
# This way you can test if the array is emply.
if ( Get-Service "$ServiceName*" -Include $ServiceName ) {
$Return = $True
}
Return $Return
}
[bool] $thisServiceExists = ServiceExists "A Service Name"
$thisServiceExists
但是ravikanth是最好的解决方案,因为如果Service不存在,则Get-WmiObject不会引发错误。所以我决定使用:
Function ServiceExists([string] $ServiceName) {
[bool] $Return = $False
if ( Get-WmiObject -Class Win32_Service -Filter "Name='$ServiceName'" ) {
$Return = $True
}
Return $Return
}
因此,提供更完整的解决方案:
# Deletes a Service with a name as defined in $ServiceName.
# Returns a boolean $True or $False. $True if the Service didn't exist or was
# successfully deleted after execution.
Function DeleteService([string] $ServiceName) {
[bool] $Return = $False
$Service = Get-WmiObject -Class Win32_Service -Filter "Name='$ServiceName'"
if ( $Service ) {
$Service.Delete()
if ( -Not ( ServiceExists $ServiceName ) ) {
$Return = $True
}
} else {
$Return = $True
}
Return $Return
}
Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
和之间进行速度比较Get-Service $serviceName -ErrorAction Ignore
(如果服务不存在,则会完全隐藏错误),以确保完整性。我预计Get-WmiObject可能会更快,因为它不会引发错误。我错了 每个循环运行100次,Get-Service花了0.16秒,而Get-WmiObject花了9.66秒。因此,Get-Service比Get-WmiObject快60倍。
PS的较新版本具有Remove-WmiObject。小心$ service.delete()的静默失败...
PS D:\> $s3=Get-WmiObject -Class Win32_Service -Filter "Name='TSATSvrSvc03'"
PS D:\> $s3.delete()
...
ReturnValue : 2
...
PS D:\> $?
True
PS D:\> $LASTEXITCODE
0
PS D:\> $result=$s3.delete()
PS D:\> $result.ReturnValue
2
PS D:\> Remove-WmiObject -InputObject $s3
Remove-WmiObject : Access denied
At line:1 char:1
+ Remove-WmiObject -InputObject $s3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Remove-WmiObject], ManagementException
+ FullyQualifiedErrorId : RemoveWMIManagementException,Microsoft.PowerShell.Commands.RemoveWmiObject
PS D:\>
对于我的情况,我需要运行“作为管理员”
结合Dmitri和dcx的答案,我做到了:
function Confirm-WindowsServiceExists($name)
{
if (Get-Service $name -ErrorAction SilentlyContinue)
{
return $true
}
return $false
}
function Remove-WindowsServiceIfItExists($name)
{
$exists = Confirm-WindowsServiceExists $name
if ($exists)
{
sc.exe \\server delete $name
}
}
要检查Windows服务是否MySuperServiceVersion1
存在,即使您不确定其确切名称,也可以使用通配符,使用如下所示的子字符串:
if (Get-Service -Name "*SuperService*" -ErrorAction SilentlyContinue)
{
# do something
}
对于单台PC:
if (Get-Service "service_name" -ErrorAction 'SilentlyContinue'){(Get-WmiObject -Class Win32_Service -filter "Name='service_name'").delete()}
else{write-host "No service found."}
PC列表的宏:
$name = "service_name"
$list = get-content list.txt
foreach ($server in $list) {
if (Get-Service "service_name" -computername $server -ErrorAction 'SilentlyContinue'){
(Get-WmiObject -Class Win32_Service -filter "Name='service_name'" -ComputerName $server).delete()}
else{write-host "No service $name found on $server."}
}
PowerShell Core(v6 +)现在具有一个Remove-Service
cmdlet。
我不知道将其反向移植到Windows PowerShell的计划,该版本从v5.1开始不可用。
例:
# PowerShell *Core* only (v6+)
Remove-Service someservice
请注意,如果服务不存在,则调用将失败,因此,仅在当前存在服务时将其删除,您可以执行以下操作:
# PowerShell *Core* only (v6+)
$name = 'someservice'
if (Get-Service $name -ErrorAction Ignore) {
Remove-Service $name
}
对于v6之前的PowerShell版本,您可以执行以下操作:
Stop-Service 'YourServiceName'; Get-CimInstance -ClassName Win32_Service -Filter "Name='YourServiceName'" | Remove-CimInstance
对于v6 +,可以使用Remove-Service cmdlet。
请注意,从Windows PowerShell 3.0开始,该cmdlet Get-WmiObject已被Get-CimInstance取代。
对此进行了修改,以获取服务器的输入列表,指定主机名并提供一些有用的输出
$name = "<ServiceName>"
$servers = Get-content servers.txt
function Confirm-WindowsServiceExists($name)
{
if (Get-Service -Name $name -Computername $server -ErrorAction Continue)
{
Write-Host "$name Exists on $server"
return $true
}
Write-Host "$name does not exist on $server"
return $false
}
function Remove-WindowsServiceIfItExists($name)
{
$exists = Confirm-WindowsServiceExists $name
if ($exists)
{
Write-host "Removing Service $name from $server"
sc.exe \\$server delete $name
}
}
ForEach ($server in $servers) {Remove-WindowsServiceIfItExists($name)}
Windows Powershell 6将具有Remove-Service cmdlet。截至目前,Github版本显示PS v6 beta-9
来源: https : //docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.management/remove-service?view=powershell-6