2016-09-18 62 views
0

我想停止Windows服務並掛起或掛起狀態爲Stopping。我們正在使用混合操作系統,例如Windows 2008/2012R2。如何強制停止處於「停止等待」狀態的一個或多個服務

$ServerList = 'C:\Powershell\SCXAgentDSC\list.txt' 
$SCXAgents = Get-Content -Path $ServerList 

foreach ($scxagent in $SCXAgents) { 
    $Hostname = $scxagent 
    Write-Host $Hostname 
    #Ping host 
    $IP = (Test-Connection $hostname -Count 1 -ErrorAction SilentlyContinue | 
     Select-Object -ExpandProperty ProtocolAddress) 
    if ($IP -eq $null) { 
    continue 
    } 
    #Get state of service 
    $servicestate = Get-Service -ComputerName $Hostname service_name -ErrorAction SilentlyContinue 
    #If service is stopped or does not exist, continue otherwise set to stopped and disabled 
    if ($servicestate.status -eq 'Stopped' -or $servicestate.status -eq $null) { 
    continue 
    } else { 
    (Get-Service -ComputerName $Hostname service_name).Stop() 
    Set-Service -ComputerName $Hostname -Name service_name -StartupType Disabled 
    Write-Host "stopped services" -BackgroundColor yellow 
    $RPMLines = Get-Service -ComputerName $Hostname service_name 

    if ($RPMLines.status -match "Stopped") { 
     $ServiceStatus = "STOPPED" 
    } else { 
     $ServiceStatus = "RUNNING" 
    } 
    # Port testing (22 and 1270) 
    try { 
     if ((New-Object System.Net.Sockets.TcpClient("$scxagent", "1270")).Connected -eq $true) { 
     $AgentPortStatus = "OK" 
     } else { 
     $AgentPortStatus = "NOT OK" 
     } 
    } catch { 
     $AgentPortStatus = "NOT OK" 
    } 
    try { 
     if ((New-Object System.Net.Sockets.TcpClient("$scxagent", "22")).Connected) { 
     $sshstatus = "OK" 
     } else { 
     $sshstatus = "NOT OK" 
     } 
    } catch { 
     $sshstatus = "NOT OK" 
    } 

    Write-Output "$scxagent | SSH : $sshstatus | AgentPort : $AgentPortStatus | AgentServStatus : $ServiceStatus" 
    } 
} 

此外,我的代碼上面我想輸出格式化爲一行,我是正確的嗎?例如:

scxagent  sshstatus  AgentPortStatus AgentServStatus 
Server1  OK   OK     STOPPED 
Server2  NOT OK  NOT OK    RUNNING 
etc.

最後更新:

if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) { 
    $arguments = "& '" + $myinvocation.mycommand.definition + "'" 
    Start-Process powershell -Verb runAs -ArgumentList $arguments 
    break 
} 

$ServerList = 'C:\Powershell\SCXAgentDSC\list.txt' 
$SCXAgents = Get-Content -Path $ServerList 

foreach ($scxagent in $SCXAgents) { 
    $Hostname = $scxagent 
    Write-Host $Hostname 
    #Ping host 
    $IP = (Test-Connection $hostname -Count 1 -ErrorAction SilentlyContinue | 
     Select-Object -ExpandProperty ProtocolAddress) 
    if ($IP -eq $null) { 
    continue 
    } 
    #Get state of service 
    $servicestate = Get-Service -ComputerName $Hostname service_name -ErrorAction SilentlyContinue 
    #If service is stopped or does not exist, continue otherwise set to stopped and disabled 
    if ($servicestate.Status -eq 'Stopped' -or $servicestate.Status -eq $null) { 
    continue 
    } else { 
    $pid = Get-WmiObject -Computer $hostname -Class Win32_Service -Filter "Name='service_name'" | 
      Select-Object -Expand ProcessID 

    (Get-Process -Computer $hostname -PID $pid).Kill() 

    (Get-Service -ComputerName $Hostname service_name).Stop() 
    Set-Service -ComputerName $Hostname -Name service_name -StartupType Disabled 
    Write-Host "stopped services" -BackgroundColor yellow 

    $RPMLines = Get-Service -ComputerName $Hostname service_name 

    if ($RPMLines.Status -match "Stopped") { 
     $ServiceStatus = "stop" 
    } else { 
     $ServiceStatus = "start" 
    } 
    # Port testing (22 and 1270) 
    try { 
     if ((New-Object System.Net.Sockets.TcpClient("$scxagent", "1270")).Connected -eq $true) { 
     $AgentPortStatus = "OK" 
     } else { 
     $AgentPortStatus = "NOT OK" 
     } 
    } catch { 
     $AgentPortStatus = "NOT OK" 
    } 
    try { 
     if ((New-Object System.Net.Sockets.TcpClient("$scxagent", "22")).Connected) { 
     $sshstatus = "OK" 
     } else { 
     $sshstatus = "NOT OK" 
     } 
    } catch { 
     $sshstatus = "NOT OK" 
    } 

    Write-Output "$scxagent | SSH : $sshstatus | AgentPort : $AgentPortStatus | AgentServStatus : $ServiceStatus" 

    Read-Host -Prompt "Press Enter to continue" 
    } 
} 

錯誤消息:

ERROR: Cannot overwrite variable PID because it is read-only or constant. 
getservice.ps1 (32): ERROR: At Line: 32 char: 3 
ERROR: +   $pid = Get-WmiObject -Computer $hostname -Class Win32_Service -Filter "Name='A ... 
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
ERROR:  + CategoryInfo   : WriteError: (PID:String) [], SessionStateUnauthorizedAccessException 
ERROR:  + FullyQualifiedErrorId : VariableNotWritable
+1

通常這發生在與服務「卡住」相關的進程時。您需要找到該進程並使用停止進程終止進程,然後嘗試再次停止該服務。 – autosvet

回答

1

如果接收到停止請求「逼停」它是殺害的唯一途徑後服務掛起該過程如@autsvet已經提到。使用PID來標識進程:

$processID = Get-WmiObject -Computer $hostname -Class Win32_Service -Filter "Name='service_name'" | 
      Select-Object -Expand ProcessID 

(Get-Process -Computer $hostname -PID $processID).Kill() 

請注意,某些服務配置爲不允許篡改其進程。在這種情況下,您可以嘗試change the process settings。如果這不起作用(或者出於某種原因你不能這麼做),你將不得不重新啓動系統。

+0

我知道了最後我是否正確的輸出格式在我的代碼?再次感謝 – Arbelac

+0

只有你可以知道你的代碼的輸出是你想要的。也請不要混淆問題。一次只針對一個問題集中您的問題。 –

+0

我非常認真地對您進行了詳細說明但是我只是不想打開與此相關的新問題。嗯,我可以採取一些想法嗎? – Arbelac