Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell - determine if service exists on remote computer

Posted on 2010-08-25
8
Medium Priority
?
7,313 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Accepted Solution

by:
soostibi earned 500 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 500 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

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

A brief introduction to what I consider to be the best editor for PowerShell.
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 …
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…
Loops Section Overview

610 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