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