Solved

Powershell - determine if service exists on remote computer

Posted on 2010-08-25
8
6,621 Views
Last Modified: 2012-05-10
The attached code checks the status of a service on remote computers to determine if the service exists or not.  For output I get
Installed - for computers that have IISAdmin
not installed - for computers that do not have IISAdmin
not installed - for computers that can not be accessed

The last group I would like to change.  How can it have powershell scripts determine if the service module is even accessible?  I do not want a 'not installed' if the system can not be checked.

Thanks in advance...again.

C
$colComputers = get-content "c:\powershell\outputfiles\AccessPass.Txt"
foreach ($strComputer in $colComputers)
    {
     
     $iisAdminSVC = [System.ServiceProcess.ServiceController]::GetServices($strComputer) | where{$_.name -eq 'IISADMIN'}
     switch ($iisAdminSVC.status)
        {     
        "Running" {$InstallStatus = "Installed"}
        "Stopped" {$InstallStatus = "Installed"}
        "Paused" {$InstallStatus = "Installed"}
        "Starting" {$InstallStatus = "Installed"}
        "Stopping" {$InstallStatus = "Installed"}
        "" {$InstallStatus = "NOT Installed"}
        }   
     write-host $strComputer $InstallStatus
    }

Open in new window

0
Comment
Question by:kabaam
[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
  • 2
8 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33522389

Use Test-Connection to verify whether or not the host can be reached prior to asking it for services.

Chris
0
 
LVL 11

Author Comment

by:kabaam
ID: 33522660
Thanks Chris,

I have another portion of the script that will ping the remote workstations but that will not verify if the connection can be made to the services module based on firewalls, routing, or permissions.  I want to be able to test to ensure those items will pass or not.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33522761

I'm not quite sure why you're not using Get-Service to be honest.

However, this will throw an error if it fails to connect:

[ServiceProcess.ServiceController]::GetServices($strComputer)

So you can either catch the error, or test the last command for success or failure.

For example, this tests for the success / failure state of the last command. $? is a boolean indicator for that.


$Services = [ServiceProcess.ServiceController]::GetServices($strComputer)
If ($?) {
  # This command was successful

  $iisAdminSVC = $Services | Where-Object {$_.name -eq 'IISADMIN'}

  If ($iisAdminSVC) {
    # Service is listed

    switch ...


Chris
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

Accepted Solution

by:
soostibi earned 125 total points
ID: 33522772
I would do this. (there is a bug with get-service, that would be the most elegant, but remotely it does not handle silentlycontinue)
$colComputers = get-content "c:\powershell\outputfiles\AccessPass.Txt"  
foreach ($strComputer in $colComputers)  
    {  
     $IISAdminSVC = Get-WmiObject -Class win32_service -Filter "name = 'IISAdminSVC'" -ErrorAction SilentlyContinue
     if($?) { 
	 	if($iisAdminSVC) {$InstallStatus = "Installed"}
		else{$InstallStatus = "NOT Installed"}
	}
	 else {$InstallStatus = "Inacccessible"}
     write-host $strComputer $InstallStatus  
    }

Open in new window

0
 
LVL 11

Author Comment

by:kabaam
ID: 33523431
Thanks folks.

Soostibi, I ran this script and all workstations came back as a positive and only one should.  I added -computername $strComputer to the get-WMIobject and it fixed it up.  Thanks it is cleaner than the switch I was using. I may go back to the switch later on once the scripts get more advanced.

"For example, this tests for the success / failure state of the last command. $? is a boolean indicator for that."
what exactly does ($?) do?  where can I find more info on it?

thanks again.
0
 
LVL 71

Assisted Solution

by:Chris Dent
Chris Dent earned 125 total points
ID: 33524262

It's a pre-defined variable that stores the how successful the last command was. $? in particular is one of the automatic variables in PowerShell.

See:

Get-ChildItem Variable:

You'll see $? second from the top.

And this for the built-in documentation:

Get-Help about_automatic_variables | more

I'm struggling to find a better reference than that, most just re-iterate the contents of the help file.

It's also worth looking at preference variables:

Get-Help about_preference_variables

My favourite in that set would be $OFS (Output Field Seperator).

Chris
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33525137
Sorry, I left from the copy the -computername parameter...
0
 
LVL 11

Author Comment

by:kabaam
ID: 33525448
no worries.  Thanks Gents.
I am going to do an even split on this.  

Soos, thanks for the script edit
Chris, thanks for the additional learning.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…

730 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