Solved

Cycling through machines

Posted on 2015-02-19
6
36 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
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

861 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

24 Experts available now in Live!

Get 1:1 Help Now