Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cycling through machines

Posted on 2015-02-19
6
Medium Priority
?
43 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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 2000 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

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

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.
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

705 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