[英]How to find, stop and disable a Windows service using Powershell
I am trying to find a service, stop it and then disable it remotely using Powershell.我正在尝试查找服务,将其停止,然后使用 Powershell 远程禁用它。 It can find and stop but cannot disable.
它可以找到并停止但不能禁用。 For disabling, I have to run the
Set-Service
command separately.为了禁用,我必须单独运行
Set-Service
命令。 Can it be done in one line?可以一行完成吗?
The following code-snippet will stop the Print Spooler service, but will not disable it:以下代码片段将停止Print Spooler服务,但不会禁用它:
$ip = "10.10.10.10"
$svc_name = "Spooler"
get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Stop-Service | Set-Service -StartupType Disabled
The following code-snippet will stop and disable the Print Spooler service:以下代码片段将停止并禁用Print Spooler服务:
$ip = "10.10.10.10"
$svc_name = "Spooler"
get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Stop-Service
Set-Service $svc_name -StartupType Disabled
Powershell version is 5.1.14393.2969
. Powershell 版本为
5.1.14393.2969
。
Edit: The following line will also find and disable.编辑:以下行也将找到并禁用。 So, it looks like I can give two instructions with pipeline.
所以,看起来我可以用管道给出两条指令。
get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Set-Service -StartupType Disabled
You need to use Set-Service
to set the startup type, as outlined in your question:您需要使用
Set-Service
设置启动类型,如您的问题中所述:
Set-Service -StartupType Disabled $svc_name
If you want to do it in "one line", you can use the -PassThru
argument on Stop-Service
to return the service object which can then be sent down the pipeline (you also don't need a Where-Object
clause, Get-Service
can filter on service name as well):如果您想在“一行”中执行此操作,您可以使用
Stop-Service
上的-PassThru
参数返回服务 object 然后可以将其发送到管道中(您也不需要Where-Object
子句, Get-Service
也可以过滤服务名称):
Get-Service -ComputerName $ip $svc_name | Stop-Service -PassThru | Set-Service -StartupType Disabled
You had this close in your original question, but it didn't work because you didn't use the -PassThru
parameter on Stop-Service
.您在最初的问题中已经接近尾声,但它不起作用,因为您没有在
Stop-Service
上使用-PassThru
参数。 As a note, many cmdlets that don't return an object by default do include a -PassThru
parameter to return an object that can further processed if necessary, this isn't limited to Stop-Service
by any means.需要注意的是,许多默认情况下不返回 object 的 cmdlet 都包含一个
-PassThru
参数,以返回一个 object,如有必要,可以进一步处理,这不限于以任何方式Stop-Service
。
#I'm going to just put this here because I learned a VALUABLE lesson about results queries doing this: #我将把它放在这里,因为我学到了关于结果查询的宝贵课程:
Stopping both Windows Firewall and Windows Defender Firewall停止 Windows 防火墙和 Windows Defender 防火墙
$date = get-date -uformat "%m%d%y-%H"
$day = Get-Date -Format yyyyMMdd
$dayold = Get-Date -Format "%M%d%y"
$today = (Get-Date -Format yyyyMMdd)+"-"+(get-date -uformat %H) #<--
#$log = ".\Logs\$today-Automation-WindowsFirewall.log"
$ErrorActionPreference = "SilentlyContinue"
#Stop-Transcript | Out-Null
#$ErrorActionPreference = "Continue"
#Start-Transcript -Path $log -Append
#$ServerListFile = ".\input\list.txt"
$ServerList = (Get-adcomputer -SearchBase "OU=site,OU=servers,DC=subdomain,DC=domain,DC=root" -filter {name -like "*cont*ext*"} -SearchScope Subtree -Properties Name) |select name
#$ServerList = Get-Content $ServerListFile
$ServerList=$ServerList.name
(Test-Connection -ComputerName $env:LOGONSERVER.Remove(0,2) -Count 1 -quiet)|Out-Null
foreach ($server in $ServerList){
#$server = "testserverfromlist" #<-- use to manually test script from here
if(Test-Connection -ComputerName $server -Count 1 -quiet){
$result = (get-service -ComputerName $server -name MpsSvc |select *)
if($result.Status -eq "Running")
{
get-service -ComputerName $server -name MpsSvc |stop-service -Force
#get-service -ComputerName $server -name MpsSvc |stop-service -ErrorAction Inquire
get-service -ComputerName $server -name MpsSvc |set-service -ComputerName $server -StartupType Disabled
}
elseif($result.StartType -ne "Disabled"){
set-service -ComputerName $server -name MpsSvc -StartupType "Disabled"
}
$result = (get-service -ComputerName $server -name MpsSvc |select *)
$server+": "+"The "+$result.DisplayName+" is "+$result.Status+" and "+$result.StartType
}
}
HOORAH!万岁!
Bender's answer works in PowerShell 5.1, but the -ComputerName
parameter was removed from the Get-Service
cmdlet in PowerShell 6+. Bender 的答案在 PowerShell 5.1 中有效,但
-ComputerName
参数已从 PowerShell 6+ 中的Get-Service
cmdlet 中删除。 If you're trying to do this in pwsh.exe (ie PowerShell 6+), you can use code like the following:如果您尝试在 pwsh.exe 中执行此操作(即 PowerShell 6+),您可以使用如下代码:
[string[]] $servers = @('server1', 'server2, 'server3')
[scriptblock] $disableServiceScriptBlock = {
[string] $serviceName = 'SERVICE NAME TO DISABLE GOES HERE'
Stop-Service -Name $serviceName
Set-Service -Name $serviceName -StartupType Disabled
}
Invoke-Command -ComputerName $servers -ScriptBlock $disableServiceScriptBlock
Here's a longer code snippet with better error reporting so you know what server an error occurred on:这是一个更长的代码片段,具有更好的错误报告,因此您可以知道在哪个服务器上发生了错误:
[string[]] $servers = @('server1', 'server2, 'server3')
[scriptblock] $disableServiceScriptBlock = {
[string] $serviceName = 'SERVICE NAME TO DISABLE GOES HERE'
Stop-Service -Name $serviceName -ErrorVariable stopError -ErrorAction SilentlyContinue
Set-Service -Name $serviceName -StartupType Disabled -ErrorVariable disableError -ErrorAction SilentlyContinue
# If an error occurred, report which server it occurred on with the error message.
[string] $computerName = $Env:ComputerName
if ($stopError)
{
Write-Error "$computerName : Stop Error: $stopError"
}
if ($disableError)
{
Write-Error "$computerName : Disable Error: $disableError"
}
}
Invoke-Command -ComputerName $servers -ScriptBlock $disableServiceScriptBlock
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.