Solved

Cycling through machines

Posted on 2015-02-19
6
35 Views
Last Modified: 2015-02-19
I have two code snippets that work separately, but I am trying to figure out how to combine them, being very new to Power shell, this is something I could use some help with.

Snippet #1:
(Find if a service is running on a machine)
function FuncCheckService{
param($ServiceName)
$arrService = Get-Service -Name $ServiceName
if ($arrService.Status -ne "Running"){
Write-Host $env:COMPUTERNAME $ServiceName " is NOT Running" 
}
if ($arrService.Status -eq "running"){ 
 Write-Host $env:COMPUTERNAME "$ServiceName service is Running"
}
}
FuncCheckService -ServiceName "The_Service_Name"

Open in new window



Snippet #2:
(Find all Machines in OU)
Import-Module ActiveDirectory
Get-ADComputer -Filter * -SearchBase "OU=<Deeper_OU>,OU=<Deep_OU>,Dc=<Domain>,dc=<Company>,dc=com"

Open in new window



Snippet #2 does get a list of all of the machines that I am interested in. But it gets lots of information about those machines, not just the Computer's name, that Snippet #1 would be looking for.

I imagine that this is fairly straight forward, if you know powershell, but I do not yet. Anyone know how to get a list of machines using snippet #2, and feed the list to snippet #1?
0
Comment
Question by:stakor
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
Hi Stakor,

Just to be clear, you would like to check all the machines listed in snippet 2 for a specific service that should be running?

Regards

Guy
0
 

Author Comment

by:stakor
Comment Utility
yes, I would like to check all the machines that are listed in snippet#2, using snippet #1.
0
 

Author Comment

by:stakor
Comment Utility
I should have reversed their numbers...
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 13

Expert Comment

by:Guy Lidbetter
Comment Utility
Give the below code a go...

A couple of notes though...

1. Snippet #1 is a function. This is normally a functional bit in a larger script that can be called later on, sometimes many times, so you only have to write it once. So this is an odd usage, this script can be run without it, but I've left it as its what you wanted to use.
2. In the Get-Computer part I added "-SearchScope 2", this will cause it to look in any subfolders as well. you can remove this if you only want the named folder.
3. I changed the If statement from two IFs to an IF\Else, this way if the service is in a stalled (Stopping\Starting) state it will also report as not running.
4. I also gave the output some color...


function FuncCheckService{
param($ServiceName)
Import-Module ActiveDirectory
$Servers = Get-ADComputer -Filter * -SearchBase "OU=<Deeper_OU>,OU=<Deep_OU>,Dc=<Domain>,dc=<Company>,dc=com" -SearchScope 2 | Select Name
ForEach ($Server in $Servers) {
	$arrService = Get-Service -ComputerName $Computer -Name $ServiceName
	if ($arrService.Status -ne "Running"){
		Write-Host "$ServiceName on $Computer is NOT Running" -ForegroundColor Red
		}	
	ELSE{ 
		Write-Host "$ServiceName service on $Computer is Running" - ForeGroundColor Green
		}
	}
}
FuncCheckService -ServiceName "The_Service_Name"

Open in new window


Regards

Guy
0
 

Author Comment

by:stakor
Comment Utility
When I test it, I get an error:

Get-Service : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At line:6 char:41
+     $arrService = Get-Service -ComputerName <<<<  $Computer -Name $ServiceName
    + CategoryInfo          : InvalidData: (:) [Get-Service], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetServiceCommand
0
 
LVL 13

Accepted Solution

by:
Guy Lidbetter earned 500 total points
Comment Utility
That' s because I'm a dolt....

function FuncCheckService{
param($ServiceName)
Import-Module ActiveDirectory
$Servers = Get-ADComputer -Filter * -SearchBase "OU=<Deeper_OU>,OU=<Deep_OU>,Dc=<Domain>,dc=<Company>,dc=com" -SearchScope 2 | Select Name
ForEach ($Server in $Servers) {
	$arrService = Get-Service -ComputerName $Server -Name $ServiceName
	if ($arrService.Status -ne "Running"){
		Write-Host "$ServiceName on $Server is NOT Running" -ForegroundColor Red
		}	
	ELSE{ 
		Write-Host "$ServiceName service on $Server is Running" - ForeGroundColor Green
		}
	}
}
FuncCheckService -ServiceName "The_Service_Name"

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now