0

I'm looking for help with my script again :)

I have a script that will query list of servers to find if specific service is installed. This works fine. However, I know that there are some servers in my list that I don't have access to, or there are different credentials. How do I make this visible in output? Because I only get output that service is not installed, which is not true, I just don't have correct credentials.

$name = "BESClient"
$servers = Get-content C:\list.txt

function Confirm-WindowsServiceExists($name)
{   
   if (Get-Service -Name $name -Computername $server -ErrorAction Continue)
   {
       Write-Host "$name Exists on $server"
       return $true
   }
       Write-Host "$name does not exist on $server"
       return $false
}

ForEach ($server in $servers) {Confirm-WindowsServiceExists($name)}

Also, I'd like to have output formatted into the one line, e.g.:

Server1        Service running
Server2        Service not installed
Server3        no access
etc...

Thanks a lot for any help.

Martin_K
  • 123
  • 2
  • 2
  • 11
  • 1
    Did you check http://stackoverflow.com/questions/23421507/get-service-status-from-remote-server-using-powershell, the proposed solution, uses WMI instead of get-service so you can pass credentials – Micky Balladelli Nov 25 '14 at 10:01

2 Answers2

2

Here's a WMI solution. Any errors you get from attempting to connect to remote computers will be caught with the try/catch blocks. The result of each operation will be stored to a custom object and added to the array that holds the results of all the operations.

$result = @()

$name = "BESClient"
$servers = Get-Content C:\list.txt
$cred = Get-Credential

foreach($server in $servers) {
  Try {
    $s = gwmi win32_service -computername $server -credential $cred -ErrorAction Stop | ? { $_.name -eq $name }
    $o = New-Object PSObject -Property @{ server=$server; status=$s.state }
    $result += ,$o
  }
  Catch {
    $o = New-Object PSObject -Property @{ server=$server; status=$_.Exception.message }
    $result += ,$o
  }
}

$result | Format-Table -AutoSize

You should end up with something like this:

server state
------ -----
s1     running
s4     stopped
s2     The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
arco444
  • 22,002
  • 12
  • 63
  • 67
  • Thanks, but I get error executing this script: `Missing statement body in foreach loop. At C:\Users\jkollam\Desktop\process_check.ps1:8 char:2 + <<<< : Try { + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : MissingForeachStatement` – Martin_K Nov 25 '14 at 10:53
  • Try again now... Only a missing bracket – arco444 Nov 25 '14 at 10:55
  • Could've noticed that :) It works now and does what I wanted to. Thanks a lot. – Martin_K Nov 25 '14 at 10:57
2

Here's an option which just displays the content of the error on failure:

function Confirm-WindowsServiceExists($name)
{   
   if (Get-Service -Name $name -Computername $server -ErrorAction SilentlyContinue -ErrorVariable WindowsServiceExistsError)
   {
       Write-Host "$name Exists on $server"
       return $true
   }

   if ($WindowsServiceExistsError)
   {
       Write-Host "$server" $WindowsServiceExistsError[0].exception.message
   }

   return $false
}

As for the second part of the question @arco444 has described the correct approach.

David Martin
  • 11,764
  • 1
  • 61
  • 74