admin管理员组文章数量:1320785
I'm trying to get the script below to do the following.
- Ping each server making sure it's up. (Working)
- Check multiple services on multiple servers to make sure they are running (Working...Kinda). For # 2 the script executes and seems to run successful but always returns that the service is not running although it is. I pretty new to PS so any guidance is appreciated.
$Servers = "Server1", "Server2"
$services = "Service1", "Service2"
$Cred = Get-Credential
foreach ($Server in $Servers)
{
$ip = $Server.Split(" - ")[0]
if (Test-Connection $ip -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$ip is up"
}
else{
Write-Host "$ip is down"
}
}
foreach ($server in $servers) {
Write-Host "Checking services on $server..."
foreach ($service in $services) {
$status = Invoke-Command -ComputerName $servers -Credential $Cred -ScriptBlock {
Get-Service -Name $services -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Status
}
if ($status -eq "Running") {
Write-Host "$service is running on $server"
} else {
Write-Host "$service is not running on $server"
}
}
}
I have tried multiple ways to get this to work, but unfortunately nothing has.
I'm trying to get the script below to do the following.
- Ping each server making sure it's up. (Working)
- Check multiple services on multiple servers to make sure they are running (Working...Kinda). For # 2 the script executes and seems to run successful but always returns that the service is not running although it is. I pretty new to PS so any guidance is appreciated.
$Servers = "Server1", "Server2"
$services = "Service1", "Service2"
$Cred = Get-Credential
foreach ($Server in $Servers)
{
$ip = $Server.Split(" - ")[0]
if (Test-Connection $ip -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$ip is up"
}
else{
Write-Host "$ip is down"
}
}
foreach ($server in $servers) {
Write-Host "Checking services on $server..."
foreach ($service in $services) {
$status = Invoke-Command -ComputerName $servers -Credential $Cred -ScriptBlock {
Get-Service -Name $services -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Status
}
if ($status -eq "Running") {
Write-Host "$service is running on $server"
} else {
Write-Host "$service is not running on $server"
}
}
}
I have tried multiple ways to get this to work, but unfortunately nothing has.
Share Improve this question asked Jan 17 at 22:01 Dwilson76Dwilson76 232 bronze badges1 Answer
Reset to default 2When you do:
$status = Invoke-Command -ComputerName $servers -Credential $Cred -ScriptBlock {
Get-Service -Name $services ....
}
$services
isn't defined in the context of your Invoke-Command
, you should use either $using:
or -ArgumentList
to pass-in the values to the remote scope.
I'd personally also discard the ping tests in your code, a server responding to icmp requests isn't an indication that you can connect to them via Invoke-Command
, this cmdlet can also handle parallel invocation, meaning that the loop on $servers
isn't required.
In summary you could do this:
Invoke-Command -ComputerName $servers -Credential $Cred -ScriptBlock {
Get-Service -Name $using:services -ErrorAction SilentlyContinue |
Select-Object Name, Status
} -ErrorAction SilentlyContinue -ErrorVariable failedToConnect |
Select-Object PSComputerName, Name, Status
And then if you wanted to check which servers you couldn't connect, you can check the variable used in -ErrorVariable
:
# Gives you a list of all servers
$failedToConnect.TargetObject
本文标签: powershellScript to Check Services on Remote Servers Not WorkingStack Overflow
版权声明:本文标题:powershell - Script to Check Services on Remote Servers Not Working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742089506a2420179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论