Same code - two different results

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Andrew Golebiowski Andrew Golebiowski 1 year, 5 months ago.

  • Author
  • #24918
    Profile photo of Andrew Golebiowski
    Andrew Golebiowski

    I have a script which I want to use to monitor the system of servers. One aspect of this task is to monitor the services on other Windows servers.

    In the script I have:

    $service = Get-Service -ComputerName $name -display $whatToMonitor -ErrorAction SilentlyContinue
    if [[$service.Status | Out-String].Trim[].ToUpper[] -eq "RUNNING"] {
    	WriteToFile [$name + " - Service [" + $service.DisplayName  + "] status is: " + $service.Status] $current_log 
    } else {
    	WriteToFile [$name + " - Service [" + $whatToMonitor + "] is not installed on the computer."] $current_log 


    Function WriteToFile[]
        $msg | Out-File -Encoding "ASCII" $file -Append

    In the sctipt above:
    $name is the host name of the machine on which teh service is to be probed, including the host name of the local host,
    $whatToMonitor is the display name of the service.

    When I execute this code from the Powershell window everything works as expected. For example, checking "Print Spooler" service on any hoss including the local host "server1" returns:

    server1 - Service [Print Spooler] status is: Running

    However, when I run the script as a scheduled task defined as:
    Program: [i]C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe[/i]
    Arguments:[i] -NoProfile -WindowStyle Hidden -ExecutionPolicy Unrestricted -File C:\Users\Public\scripts\Monitor.ps1[/i]
    I get correct result for remote machines but this for the check on local host:

    server1 - Service [Print Spooler] is not installed on the computer.

    Any idea what I am missing

  • #24919
    Profile photo of Dave Wyatt
    Dave Wyatt

    Your code is reporting that the service is "not installed" if it's in any state other than running. That might be an issue here. Try modifying it like this:

    $service = Get-Service -ComputerName $name -display $whatToMonitor -ErrorAction SilentlyContinue
    if ($service) {
    	WriteToFile ($name + " - Service (" + $service.DisplayName  + ") status is: " + $service.Status) $current_log 
    } else {
    	WriteToFile ($name + " - Service (" + $whatToMonitor + ") is not installed on the computer.") $current_log 
  • #24946
    Profile photo of Andrew Golebiowski
    Andrew Golebiowski

    Hi Dave

    You are right, Get-Service returns null when the service is not running – something I did not expecte. I was rather waiting for something like $service.Status = "Down". One of my test cases included the check for $whatToMonitor set to "My Imaginary Service". It seems strange that the returns for both valid service not running and service which does not exist are indistinguishable.

    But this was not my main problem The major issue was that the result of the script was different when it reported on services that were up and running when it was executed from the command line and when executed as a scheduled task. Moreover, when run as a scheduled task, it had problem only with reporting on sevices running on local machine identified by its host name (not "localhost: and not Services on remote machine identified by their host names were reported correctly.

    Well, I found the solution. I set the scheduled task to run with elevated privileges (turned on "Run with highest privileges" check box in General tab of the scheduler which, effectively, makes the task run as administrator) and the problem disapears. I have no idea why this is the solution – when I run the script from the command line, my Powershell console is run as local user, not as administrator. I guess only Windows creators can understand the (lack of) consistency behind it :-):-):-).

    This is probably a good lesson for the future: if you run Powershell script as a scheduled task, make sure you elevate priviledges,

You must be logged in to reply to this topic.