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.
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
#!/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.
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
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?
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