Solved

Powershell - determine if service exists on remote computer

Posted on 2010-08-25
8
6,417 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 70

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 70

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

 
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 70

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

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A brief introduction to what I consider to be the best editor for PowerShell.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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