PowerShell test-connection, if service exists using get-service - powershell

Basically I want to check and see if the computers in the text file are online. If they aren't online then write-host "$computer is down". If the $computer is online then check to see if this service exists, if it exists then write-host "$computer installed, if not then write-host "$computer not installed". The Test-connection seems to work but if the computer is online they all return write-host "$computer installed" even though I have a test machine that I know doesn't have this service running.
function Get-RunService {
$service = get-service -name ABCService
Get-Content "C:\powershell\computers.txt" |
foreach {if (-not (Test-Connection -comp $_ -quiet))
{
Write-host "$_ is down" -ForegroundColor Red
}
if ($service )
{
write-host "$_ Installed"
}
else {
Write-host "$_ Not Installed"
}
}
}
get-RunService

Have a look at this cleaned up version of your code.
function Get-RunService {
Get-Content "C:\powershell\computers.txt" |
foreach {
if (-not (Test-Connection -comp $_ -quiet)){
Write-host "$_ is down" -ForegroundColor Red
} Else {
$service = get-service -name ABCService -ComputerName $_ -ErrorAction SilentlyContinue
if ($service ){
write-host "$_ Installed"
} else {
Write-host "$_ Not Installed"
}
}
}
}
get-RunService
I tried to clean up how the brackets were working. Your check if the host was alive did not have an Else to separate the case off the server being contactable or not. Side note is that ping could fail but the host could still be alive and that all depends on your environment but be aware of the possibility. Also moved the $service line into the foreach adding the -ComputerName $_
Currently you have no margin for error with this. That function is possible to not exist and you should account for that. Best advice would be to look into -ErrorAction of Get-Service and possibly a Try/Catch block.

It's been a while, but I think this version is a bit more clear. Why check for it to be offline instead of only performing the actions if the computer is online.
function Get-RunService {
Get-Content "C:\powershell\computers.txt" |
foreach
{
if (Test-Connection -comp $_ -quiet)
{
$service = get-service -name ABCService -ComputerName $_ -ErrorAction SilentlyContinue
if ($service ) { Write-Host "$_ Installed" }
else { Write-Host "$_ Not Installed" }
}
else
{ Write-Host "$_ is offline!" -ForegroundColor Red }
}
}

Related

Powershell Error Handling not working in Invoke-command block

I want to print error message when service is not found or machine is offline
$csv = Import-Csv "C:\Users\user\Desktop\computers.csv" -delimiter ","
foreach ($cs in $csv){
$computers = $cs.DNSHostname
foreach ($computer in $computers){
Try{
Invoke-Command -ComputerName $computer -ScriptBlock {
$ProcessCheck = Get-Process -Name agentid-service -ErrorAction Stop
if ($null -eq $ProcessCheck) {
Write-output "agentid-service IS not running $env:computername"
}
else {
Write-output "agentid-service IS running $env:computername"
}
}
}
catch{
Write-Warning $Error[0]
}
}
}
Instead, when service name is not found, i'm getting error:
Cannot find a process with the name "agentid-service". Verify the process name and call the cmdlet again.
And if connection to computer is not possible then getting:
[vm.domain.local] Connecting to remote server vm.domain.local failed with the following error message : The WinRM client cannot process the request because the server name cannot be resolved.
And script continue execution (as it should).
How can i get "Catch" block to be executed ?
You can do this in one foreach loop.
Try
$computers = (Import-Csv "C:\Users\user\Desktop\computers.csv").DNSHostname
foreach ($computer in $computers) {
if (!(Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
Write-Warning "Computer $computer cannot be reached"
continue # skip this one and proceed with the next computer
}
try{
Invoke-Command -ComputerName $computer -ScriptBlock {
$ProcessCheck = Get-Process -Name 'agentid-service' -ErrorAction Stop
if (!$ProcessCheck.Responding) {
"agentid-service is NOT not running on computer '$env:computername'"
}
else {
"agentid-service is running on computer '$env:computername'"
}
}
}
catch{
Write-Warning $_.Exception.Message
}
}
Thanks to #Theo's answer, managed to fix it:
$csv = Import-Csv "C:\Users\user\Desktop\computers.csv" -delimiter ","
foreach ($cs in $csv){
$error.Clear()
$computers = $cs.DNSHostname
foreach ($computer in $computers){
Try{
Invoke-Command -ComputerName $computer -ScriptBlock {
$ProcessCheck = Get-Process -Name agentid-service -ErrorAction Stop
if ($null -ne $ProcessCheck) {
Write-output "agentid-service IS running $env:computername"
}
} -ErrorAction Stop
}
catch{
if ($null -eq $ProcessCheck){
Write-warning "agentid-service IS not running $env:computername"
}
Write-Warning $Error[0]
}
}
}

Powershell not reading .txt lines

I cannot get PowerShell to run this .txt file, what am I doing wrong? I tried changing the name of the .txt and checked passed scripts and everything seems to be the same but I keep getting an error saying that the ".txt in invalid"
$POSName = "$PSScriptRoot\Bex.txt"
foreach ($POS in (Get-Content $POSName)) {
$Bex = Get-Service -ComputerName $POSName | Where-Object { $_.name -eq "BexServ" }
}
If ($Bex -eq $null) {
# Service does not exist
Write-Host " doesn't exist." -ForegroundColor Red
}
Else {
# Service does exist
Write-Host "The $($Bex.Name) service found." -ForegroundColor Green
If ($Bex.Status -eq "Running") {
# Stop Service
Set-Service -status stopped -ComputerName $POSName -name $Box.Name -ErrorAction Stop
Write-Host "The $($Bex.Name) successfully stopped." -ForegroundColor Green
}
else {
#service already stopped
If ($Bex.Status -eq "Stopped") {
Write-Host "The $($Bex.Name) service already Stopped." -ForegroundColor Green
}
}
}
As commented, you are using the wrong variable in the loop. The code is reading the text file just fine, it is Get-Service that cannot deal with a path to a file in the -ComputerName parameter.
Also, the placing of the if..else should be inside the loop, not after.
Try
$POSName = "$PSScriptRoot\Bex.txt"
foreach ($POS in (Get-Content $POSName)) {
$Bex = Get-Service -ComputerName $POS | Where-Object { $_.name -eq "BexServ" }
If (!$Bex) {
# Service does not exist
Write-Host " doesn't exist." -ForegroundColor Red
}
Else {
# Service does exist
Write-Host "The $($Bex.Name) service found." -ForegroundColor Green
If ($Bex.Status -eq "Running") {
# Stop Service
Set-Service -status stopped -ComputerName $POSName -name $Box.Name -ErrorAction Stop
Write-Host "The $($Bex.Name) successfully stopped." -ForegroundColor Green
}
else {
#service already stopped
If ($Bex.Status -eq "Stopped") {
Write-Host "The $($Bex.Name) service already Stopped." -ForegroundColor Green
}
}
}
}
It might also be a good idea to output the computername ($POS) in the Write-Host lines too

PowerShell Loop Ping Online Status

I'm trying to modify a Powershell to do the following work, but I got into some errors.
My goal is to monitor multiple servers online status from the the Powershell window. I would need to open a Powershell and run the ps1 script from the Powershell window then monitor the online status result.
1. Ping servers from a list from hostname.txt
2. If server pingable and online return hostname and ip in Green
3. If server timeout return hostname and ip in red
4. Loop back to the first server and ping again
Please help. Thank You !
$name=Get-content "c:\temp\hostname.txt"
foreach($name in $names) {
if(Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue) {
Write-Host "$name is up" -foregroundColor Green
$output+="$name is up,"+"'n"
}
else {
Write-Host "$name is up" -foregroundColor Red
$output+="$name is down,"+"'n"
}
}
Here's an old script I have called pinger that does something similar. It uses a hashtable, so it only reports changes in status, and sleeps a little at the end of the loop. It also beeps. If I output an object instead of text, there would be a delay until 2 objects were output.
# pinger.ps1
# example: pinger comp1
# pinger comp1,comp2,comp3
# $list = cat comps.txt; pinger $list
param ($hostnames)
$pingcmd = 'test-connection'
$sleeptime = 1
$sawup = #{}
$sawdown = #{}
foreach ($hostname in $hostnames) {
$sawup[$hostname] = $false
$sawdown[$hostname] = $false
}
while ($true) {
foreach ($hostname in $hostnames) {
if (& $pingcmd -count 1 $hostname -ea 0) {
if (! $sawup[$hostname]) {
echo "$([console]::beep(500,300))$hostname is up $(get-date)"
$sawup[$hostname] = $true
$sawdown[$hostname] = $false
}
} else {
if (! $sawdown[$hostname]) {
echo "$([console]::beep(500,300))$hostname is down $(get-date)"
$sawdown[$hostname] = $true
$sawup[$hostname] = $false
}
}
}
sleep $sleeptime
}
pinger microsoft.com,yahoo.com
microsoft.com is down 07/22/2021 09:55:15
yahoo.com is up 07/22/2021 09:55:15
If you want it to repeat in perpetuity, wrap the whole thing in a while($true){...} loop:
while($true){
$names = Get-content "c:\temp\hostname.txt"
foreach ($name in $names) {
if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue) {
Write-Host "$name is up" -foregroundColor Green
$output += "$name is up," + "'n"
}
else {
Write-Host "$name is up" -foregroundColor Red
$output += "$name is down," + "'n"
}
}
}
If you want to show the resolved IP address, make sure you save the output from Test-Connection to a variable:
if($ping = Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$name [IP: $($ping.ProtocolAddress)] is up" -ForegroundColor Green
$output += "$name [IP: $($ping.ProtocolAddress)] is up `n"
}

Using test-connection and IF = "True" Statement, ELSE not working when no connection

I am trying to test a network connection and if it's good, tell me it is found. ELSE write-host it is not found, and export that computer to a CSV. However, I am just getting the error "Testing connection to computer failed: error due to lack of resources." on computers that cannot get a connection. Does test-connection not work this way? The = "True" is working for me. Below is my code:
foreach ($computer in $Computers)
$computername = $computer.Computers
$testConnection = Test-Connection $computername
IF ($testConnection = "True")
{
{
Write-Host "$computername found"
}
ElSE
{
Write-Host "$computername does not have CCleaner installed." #| Export-Csv C:\Users\jcheng\Desktop\Scripts\PingTestLog.txt -Append
}
{
Give this a try
foreach ($computer in $Computers)
$computername = $computer.Computers
$testConnection = Test-Connection $computername
If (($testConnection -ne "") -or ($testconnection -ne $null){
Write-Host "$computername found"
}
Else{
Write-Host "$computername does not have CCleaner installed." #| Export-Csv C:\Users\jcheng\Desktop\Scripts\PingTestLog.txt -Append
}
I would check on this: $computer.Computers, to see if it is actually returning the property that you are looking for.
As a side note, make sure your CCleaner version is newer than 5.33 :) it was recently breached
Use this
If ((Test-Connection $computername).PingSucceeded)
{ ... }

Is there any Faster method to do WMI query from Powershell ..?

Wrote a small script to find the number Multipaths from the windows servers using WMI query. It works well for the servers which can connect directly without any issue. But if one server is pingable but not able to reach through WMI script, it takes long time to return the error ( for example if a linux server hostname is present in the servers.txt list).. Can somebody help me to do the same in a faster way..?
$Servers = Get-Content .\Servers.txt
$ErrorActionPreference = ‘SilentlyContinue’
FOREACH ($Server in $Servers) {
Write-Host $Server -nonewline
if (test-connection -computername $Server -Count 1 -quiet) {
$Name = $null
$NoPath =$null
$MPIODisks =$null
$MPIODisks = Get-WmiObject -Namespace root\wmi -Class mpio_disk_info -ComputerName "$Server" |Select-Object "DriveInfo"
if ($MPIODisks -eq $Null) {
write-host "`t - Unable to connect" -fore "RED"
} else {
write-host ""
write-host "Drive Name `tNo.Path" -fore "yellow"
Foreach ($Disk in $MPIODisks) {
$mpiodrives = $disk.DriveInfo
foreach ($Drive in $mpiodrives) {
$Name = $Drive.Name
$NoPath = $Drive.Numberpaths
If ($NoPath -lt 4) {
Write-Host $Name `t -nonewline
write-host $NoPath -fore "Red"
} else {
Write-Host $Name `t -nonewline
write-host $NoPath -fore "Green"
}
}
}
}
write-host ""
} else {
write-host "`t- Unknown Host" -fore "Red"
write-host ""
}
}
There is a connect item for Get-WmiObject to add a timeout parameter. A workaround noted in that item is to just pipe your WMI command to Wait-Job and specify a timeout period in seconds.
As long as your on PS version 3.0 or higher, this should work for you:
Get-WmiObject win32_computersystem -ComputerName <hostname> -AsJob | Wait-Job -Timeout 10 | Receive-Job
As an alternative, you could ask all servers for the result at once by passing them all into the query and avoiding the slow loop querying one server at a time. I don't have any MPIO drives to test with, but it could look something like this (using Get-Ciminstance which takes a timeout parameter):
$servers = Get-Content .\Servers.txt
# Get data from all servers with timeout
$servers_ok = Get-CimInstance -computername $servers -Namespace root\wmi -Class mpio_disk_info -ErrorAction SilentlyContinue -OperationTimeoutSec 1 | group pscomputername
# Output which servers gave no result back
foreach($no_result in $($servers | where { $_ -NotIn $servers_ok.Name })) {
write-host "No result for $no_result" -ForegroundColor Red
}
# Loop over the results and output
foreach($server in $servers_ok) {
Write-Host $server.Name
foreach($mpiodisk in $server.group)  {
$mpiodrives = $mpiodisk.DriveInfo
foreach ($mpiodrive in $mpiodrives) {
$name = $mpiodrive.Name
$noPath = $mpiodrive.NumberPaths
If ($NoPath -lt 4) {
write-host $name `t -nonewline
write-host $noPath -fore "Red"
} else {
write-host $name `t -nonewline
write-host $noPath -fore "Green"
}
}
}
}