Execute powershell script with UNIX

Hi

I have a powershell script which is checking whether a windows service is running (as shown below)

function FuncCheckService

{
	$ServiceName = 'pgsql-9.2'
	$arrService = Get-Service -Name $ServiceName

		if ($arrService.Status -eq 'Running')
     			{ 
         			Write-Output "$ServiceName service is running"
     			}
		else
   			  { 
        			 Write-Output "$ServiceName service is not running"
     			}
			}

#start transscript to log details
 Start-Transcript -Path "C:\Users\sup_jelborough\Documents\CheckPostgresService.log" -Append -IncludeInvocationHeader

#Call function with EM service as a parameter
 FuncCheckService pgsql-9.2

#stop logging
 Stop-Transcript

This runs as I expect it too

I would like to have a script within UNIX to execute the powershell script and echo whether the pgsql-9.2 service is running or not.

Is this possible?

Cheers

It is possible, there is a Power Shell build for Linux available (on GitHubl)

However

 systemctl status postgresql

is the preferred method to test the service status on a Unix platform

Hi Skrynesaver

Thank you for the reply but I don't quite understand what you mean.

Did you mean:

postgres_db_check=`systemctl status postgresql`

if [ "postgres_db_check" -eq 'Running'
then
	postgres_db_count=1
else
	postgres_db_count=0

echo $postgres_db_count

cheers

Hi.

I recently installed Powershell (pwsh).

This script:

#!/usr/bin/env pwsh                                                                                                                                             function FuncCheckService                                                       

{
        $ServiceName = 'pgsql-9.2'
        $arrService = Get-Service -Name $ServiceName

                if ($arrService.Status -eq 'Running')
                        { 
                                Write-Output "$ServiceName service is running"
                        }
                else
                          { 
                                 Write-Output "$ServiceName service is not running"
                        }
                        }

#start transscript to log details
 Start-Transcript -Path "C:\Users\sup_jelborough\Documents\CheckPostgresService.log" -Append -IncludeInvocationHeader

#Call function with EM service as a parameter
 FuncCheckService pgsql-9.2

#stop logging
 Stop-Transcript

produces:

> ./s1                                    
Start-Transcript : Transcription cannot be started due to the error: Cannot 
find drive. A drive with the name 'C' does not exist.
At line:20 char:2
+  Start-Transcript -Path "C:\Users\sup_jelborough\Documents\CheckPostg ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Transcript], PSInva 
   lidOperationException
    + FullyQualifiedErrorId : CannotStartTranscription,Microsoft.PowerShell.Co 
   mmands.StartTranscriptCommand
 
Get-Service : The term 'Get-Service' is not recognized as the name of a 
cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the 
path is correct and try again.
At line:7 char:16
+     $arrService = Get-Service -Name $ServiceName
+                   ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-Service:String) [], Command 
   NotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
pgsql-9.2 service is not running
Stop-Transcript : An error occurred stopping transcription: The host is not 
currently transcribing.
At line:26 char:2
+  Stop-Transcript
+  ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Stop-Transcript], PSInval 
   idOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.S 
   topTranscriptCommand
 

Not surprising because I don't have your environment. The Get-service issue might be a problem.

This was run on a system like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.9 (jessie) 
pwsh PowerShell v6.0.0-beta.9

More details about pwsh:

pwsh    PowerShell command-line shell and .NET REPL (man)
Path    : /usr/bin/pwsh
Package : powershell
Version : v6.0.0-beta.9
Type    : ELF 64-bit LSB executable, x86-64, version 1 (SYS ...)
Home    : https://github.com/PowerShell/PowerShell/releases/ (doc)

Good luck ... cheers, drl

systemctl is a Linux utility for starting, stopping, restarting and checking on services on your Linux system. So if your aim is to write a set of utilities to check whether certain services are running or not, don't bother. Just use

systemctl status <service>

If, on the other hand, you want a library of scripts to check the status of services and, say, email you the result, consider this:

for service in <list of services>
do
   systemctl -q is-active "$service" && echo "$service running" || echo "$service not running"
done

More can be found in the man page, or just type

systemctl --help

on the command line.

Andrew

Hi.

At PowerShell/KNOWNISSUES.md at master * PowerShell/PowerShell * GitHub , several issues are noted, and items like Get-service being missing are said to be fixed in future versions.

Also a comment regarding Linux command service at PowerShell core on Linux - Get-Service should mimic linux "service" command. * Issue #3582 * PowerShell/PowerShell * GitHub notes:

Good luck ... cheers, drl

Hi Andrew

Thank you for your reply.

The service I am looking to check is a windows service - not on the Linux system

I'm confused. If it is to check for a Windows service why do you want to run it on Linux? My interpretation of your requirement was that you wanted to run your scripts on Linux to check the equivalent Linux services.

So are you actually trying to check your Windows services remotely on your Linux machine?

Andrew

1 Like

Hi Andrew

Yes I have written a PowerShell script to check whether the windows service psql-9.2 is running.

We have a monitoring tool which runs off Linux so I was wondering if their was away to get a Shell Script to call or execute the powershell script and echo out a result e.g if the shell script sees the service is running echo 1 and if not echo 0

Cheers