Solved

Powershell - determine if service exists on remote computer

Posted on 2010-08-25
8
6,196 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
This article will help you understand what HashTables are and how to use them in PowerShell.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now