Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7585
  • Last Modified:

Powershell - determine if service exists on remote computer

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
chad
Asked:
chad
  • 3
  • 3
  • 2
2 Solutions
 
Chris DentPowerShell DeveloperCommented:

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

Chris
0
 
chadAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
soostibiCommented:
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
 
chadAuthor Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
soostibiCommented:
Sorry, I left from the copy the -computername parameter...
0
 
chadAuthor Commented:
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now