Solved

Cycling through machines

Posted on 2015-02-19
6
42 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Guy Lidbetter
ID: 40618886
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
ID: 40618895
yes, I would like to check all the machines that are listed in snippet#2, using snippet #1.
0
 

Author Comment

by:stakor
ID: 40618896
I should have reversed their numbers...
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 13

Expert Comment

by:Guy Lidbetter
ID: 40618936
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
ID: 40619026
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
ID: 40619251
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

628 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