Execute powershell script with UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute powershell script with UNIX
# 1  
Old 11-13-2017
Execute powershell script with UNIX

Hi

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

Code:
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
# 2  
Old 11-13-2017
It is possible, there is a Power Shell build for Linux available (on GitHubl)

However
Code:
 systemctl status postgresql

is the preferred method to test the service status on a Unix platform
# 3  
Old 11-13-2017
Hi Skrynesaver

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

Did you mean:

Code:
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
# 4  
Old 11-13-2017
Hi.

I recently installed Powershell (pwsh).

This script:
Code:
#!/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:
Code:
> ./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:
Code:
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:
Code:
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
# 5  
Old 11-13-2017
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
Code:
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:
Code:
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
Code:
systemctl --help

on the command line.

Andrew
# 6  
Old 11-13-2017
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:
Quote:
Except that service is only available on systemd machines. That's why this is tricky. We basically need to implement this behavior two (or three or four) times.
Good luck ... cheers, drl
# 7  
Old 11-13-2017
Hi Andrew

Thank you for your reply.

The service I am looking to check is a windows service - not on the Linux system
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

powershell script to unix shell script conversion.

Here is a powershell script to use restful API to create ticket in our ticketing tool. Can anyone please convert it to a shell script sothat, I can run it in Unix servers, below is the code: $body = @{ Customer= ''test' Summary= 'test summary' Impact= '4-Minor/Localized' ... (2 Replies)
Discussion started by: pandeybhavesh18
2 Replies

2. UNIX for Advanced & Expert Users

Running Powershell Script from Linux through Cygwin

Hello Experts, I am creating a run time powershell script on Linux machine and copying that powershell script to Windows machine. To connect to windows through "ssh", I am using Cygwin tool. To make the connection password less I copied my public in authorized_keys in windows Administrator... (5 Replies)
Discussion started by: shekhar_4_u
5 Replies

3. Shell Programming and Scripting

Powershell script to monitor windows process

Hello Friend, I am not expert in power shell scripting. I nee custom powershell script which check if given process is running on windows machine or not. will use it in nagios to monitor that process. (0 Replies)
Discussion started by: ghpradeep
0 Replies

4. Shell Programming and Scripting

Batch script to execute shell script in UNIX server

Hi team, My requirement is to transfer pdf files from windows machine to unix server and then from that unix server we should sftp to another server. I have completed the first part i.e From windows to using to unix server with the help of psftp.exe code: psftp user@host -pw password <... (1 Reply)
Discussion started by: bhupeshchavan
1 Replies

5. Windows & DOS: Issues & Discussions

Powershell Script Help

Need some help getting this script to work. can someone help. I am trying to create a script that moves log files to another location and only keeps 60 days worth. please see script I started but having issue getting to work.. $TODAY=GET-DATE GET-CHILDITEM C:\test\*.LOG | Where {... (9 Replies)
Discussion started by: freedavis
9 Replies

6. Shell Programming and Scripting

Dos batch script to execute unix shell script

Can anyone help me with a dos batch script to execute a shell script residing in an unix server. I am not able to use ssh. Thanks in advance (2 Replies)
Discussion started by: Shri123
2 Replies

7. UNIX for Dummies Questions & Answers

commands to execute unix script

commands to execute unix script (1 Reply)
Discussion started by: sunilamarnadh
1 Replies

8. Shell Programming and Scripting

Can't execute unix script from SAP BO DI

Dear all, I am very new to UNIX but and need some help in order to make BO DI Job Server execute a .sh script. My unix script has set permissions as under: -rwxrwxrwx. What I m trying to do is move a .txt file from one directory to another on UNIX server. I have full access to Feed... (7 Replies)
Discussion started by: sapbibodi
7 Replies

9. Shell Programming and Scripting

Execute unix shell script to text file using the script

Hi all, I am beginner in UNIX...I want to use unix shell script to create text.file...I know how to use using by command...can anybody tell me for the script? Thanks i changed the threads title from "tex file" to "text file", because "tex" would probably be misunderstood as reference to... (4 Replies)
Discussion started by: mastercar
4 Replies

10. Shell Programming and Scripting

Execute SQL query in unix script

Hi I am new in unix. oracle and unix are installed in my sytem.i need the script which could connect to the oracle using username ,password and schema and can run the select * from tab query. Thanks vijay (8 Replies)
Discussion started by: vijays3
8 Replies
Login or Register to Ask a Question