Call Powershell Script from Unix Shell Script - linux

I have a powershell script on my Windows server which does some active directory operations. I wish to call that script from another Linux machine using a shell script.
I have installed powershell server on my Windows server and can communicate with my linux machine using key based ssh authentication.
Since Windows does not support SSH connections on its own, I have installed a software called Powershell server (http://www.powershellserver.com/) on my Windows server.
The issue is that my script just stops once the command for ssh to Windows server is executed.
My shell script below:
#!/bin/bash
echo "script run"
GROUP=$1
ACCOUNT=$2
ssh Administrator#<Windowshostname>
sleep 10
echo "<pathtoscript>\scriptname.ps1 -group $GROUP -account $ACCOUNT"
sleep 5
echo "exit"
exit
I have tried without the echo command as well and it still hangs. Any idea what I must be doing wrong here? How can I make it work please?

You cannot ssh into a Windows box unless you install a SSH daemon.
You have two options:
1- You need to connect via Remote PowerShell (WINRM).
2- Install SSH daemon and connect via SSH (Check here)
3- You can also use PowerShell Web Access (Windows Servers.)
UPDATE: If you are using PowerShellServer.com then there are some things you can do to try to understand more.
Test scriptname.ps1 locally in Windows Server
Edit your scriptname.ps1 in the first line to create a file somewhere and after execution check if that file exists. This way you will know if script is being executed or not.
Execute Get-ExecutionPolicy and check you can run scripts. Remove constraints by temporary Set-ExecutionPolicy unrestricted

Tried the operation using an "expect" script and it all works well now.

Related

Connect to Linux Server from Powershell of windows server

I am trying to connect to a Linux server with sftp type in power shell but it's not getting connected. Tried many ways but couldn't figure out anyway as the power shell I have is restricted for using ssh, posh-ssh, and OpenSSH. But it gets connect via cmd prompt. one way to connect to it. Which is: Start-Process cmd - ArgumentList "/K sftp username#servername"
This opens cmd and runs the command but as it connects to Linux server it won't allow me to append another command to execute it. Like Start-Process cmd - ArgumentList "/K sftp username#servername & bye". The "bye" is command we use to come out Linux server but this is not letting to use it.
If anyone has any idea how we do it or any other way to connect please let me know.

running SSH script command from jenkins using SSH

To deploy application on linux ubuntu server I have bunch of SSH commands that i currently run using PuTTY. The server has local account serviceaccount1. In PuTTY i connect to server using serviceaccount1 and executes the following commands one by one
cd /home/serviceaccount1/cr-ml
script /dev/null
screen -S data_and_status
cd cr-ml/notebooks
source activate crml
unset XDG_RUNTIME_DIR
jupyter kernelgateway --api='kernel_gateway.notebook_http' --seed_uri='data_and_status_api.ipynb' --port 8894 --ip 0.0.0.0
...
...
and so on
Now i want automate this using Jenkins. I installed SSH plugin, configured credential using SSH Username serviceaccount1 with private key
Then created a new jenkins project and added a build step Execute shell scripts on remote host using ssh and then add all the above commands.
When i build the jenkins project, it get stuck at executing 2nd command script /dev/null
i see the following console output
To me, it seems the culprit is the screen -S data_and_status command. Once you start a screen, I don't think you would be able to execute the subsequent commands over the SSH connection.
Alternatively, you can try using a tool like Ansible to run a bunch of commands against a remote server.

How to execute a shell script after oracle started successfully

I would like to execute a shell script with non-root user while Linux system booting and oracle has been started successfully.
I tried to execute this shell in rc.local. But I found oracle start-up procedure took too much time. So when shell tried to connect to Oracle, it return failed.
On the user`s Linux server, it took several minutes to get Oracle running. I am not clear what technology they use. I found when Oracle didn`t startup finished, I can login Linux server through ssh.
In my opinion, Linux boot sequence should like following.
/etc/rc.d/rc.sysinit
/etc/rc.d/rc $RUNLEVEL
/etc/rc.d/rc.local
/bin/login
But If I add line to check if Oracle is running in rc.local, user cannot login Linux server for several minutes. I think it is improper.
Is there a better solution to solve this problem?
Any suggestion is welcome.
Use one of this solutions and configure it to "wait" for oracle listen port.
https://github.com/vishnubob/wait-for-it
Or
https://github.com/Eficode/wait-for

Executing node.js script remotely

I am setting continuous integration using Jenkins server for my Node.js application. For deployment I am using a powershell script and for that I have installed powershell plug-in. The script will need to perform the following tasks in the order.
# Step1
# Stop all the currently running services on web server. For that I am trying to
# execute maintenance.js remotely from the build server under node
node \\SharedWebServerFolder\Utilities\maintenance.js stopServices
# Step2
# Copies all the resources to server at \\SharedServerWebFolder
# Step3
# Start the services
node \\SharedWebServerFolder\Utilities\maintenance.js startServices
I have no problem executing Step2. My question is about Step1 and Step3.
Should I execute maintenance.js remotely from the build server? Is this even possible? ( Assume I have installed node.js on the build server)
Should I have one more PowerShell script on the web server which executes maintenance.js locally? So basically deployment script (from build server) will execute remote PowerShell script (which is on web server) and that remote PowerShell script will execute maintenance.js locally. In this scenario I don't have to install Node.js on the build server.
What is recommended?
People generally use ssh to execute commands remotely. I think you want to execute a command from your build server on your production / staging server.
see linux execute command remotely
this is simple where the remote box is a linux box.
If your remote is a Windows box then you can still run an ssh server on it but its more painful to setup. you might try Bitvise SSH Client.
Once your ssh client is setup correctly you should be able to execute remote commands from your build server.
I have used Jenkins SSH Plugin for deployment in my remote staging/production servers.I would recommend to execute your 'maintenance.js' in web server machine instead of your build server.You can just trigger 'maintenance.js' execution in staging/production servers from build server using ssh.I believe the execution of 'maintenance.js' remotely from the build server is hard to achieve.

How to make command run after exit the window in linux

I am establishing a connection between the linux machine and the another machine to run my scripts.but once i close the window the connection got disconnected.What should i do to make this connection contiue after closing the window.since multiple users execute the scripts in the other machine so its difficult to establish the connection each time..Any suggestions welcome.
the command i wrote in command line to establish connection is
java -jar /home/**/workspace/cli.jar -s https://******
-i /home/**/.ssh/id_rsa -description TESTS
-fsroot /home/**/Jenkins -labels test -name TESTS
Use "&" after the command to run it in background without need of open windows
If I've understood correctly screen will do. Try 'man screen' in google.

Resources