Solved

Cycling through machines

Posted on 2015-02-19
6
39 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
This article will help you understand what HashTables are and how to use them in PowerShell.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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