簡體   English   中英

如何使用 Powershell 查找、停止和禁用 Windows 服務

[英]How to find, stop and disable a Windows service using Powershell

我正在嘗試查找服務,將其停止,然后使用 Powershell 遠程禁用它。 它可以找到並停止但不能禁用。 為了禁用,我必須單獨運行Set-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 -StartupType  Disabled

以下代碼片段將停止並禁用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 版本為5.1.14393.2969

編輯:以下行也將找到並禁用。 所以,看起來我可以用管道給出兩條指令。

get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Set-Service -StartupType  Disabled

您需要使用Set-Service設置啟動類型,如您的問題中所述:

Set-Service -StartupType Disabled $svc_name

如果您想在“一行”中執行此操作,您可以使用Stop-Service上的-PassThru參數返回服務 object 然后可以將其發送到管道中(您也不需要Where-Object子句, Get-Service也可以過濾服務名稱):

Get-Service -ComputerName $ip $svc_name | Stop-Service -PassThru | Set-Service -StartupType Disabled

您在最初的問題中已經接近尾聲,但它不起作用,因為您沒有在Stop-Service上使用-PassThru參數。 需要注意的是,許多默認情況下不返回 object 的 cmdlet 都包含一個-PassThru參數,以返回一個 object,如有必要,可以進一步處理,這不限於以任何方式Stop-Service

#我將把它放在這里,因為我學到了關於結果查詢的寶貴課程:

停止 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
    }
    }

萬歲!

Bender 的答案在 PowerShell 5.1 中有效,但-ComputerName參數已從 PowerShell 6+ 中的Get-Service cmdlet 中刪除。 如果您嘗試在 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

這是一個更長的代碼片段,具有更好的錯誤報告,因此您可以知道在哪個服務器上發生了錯誤:

[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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM