简体   繁体   English

从 Unix Shell 脚本调用 Powershell 脚本

[英]Call Powershell Script from Unix Shell Script

I have a powershell script on my Windows server which does some active directory operations.我的 Windows 服务器上有一个 powershell 脚本,它执行一些活动目录操作。 I wish to call that script from another Linux machine using a shell script.我希望使用 shell 脚本从另一台 Linux 机器调用该脚本。

I have installed powershell server on my Windows server and can communicate with my linux machine using key based ssh authentication .我已经在我的 Windows 服务器上安装了 powershell 服务器,并且可以使用基于密钥的 ssh 身份验证与我的 linux 机器进行通信。

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.由于 Windows 本身不支持 SSH 连接,我在我的 Windows 服务器上安装了一个名为 Powershell 服务器 ( http://www.powershellserver.com/ ) 的软件。

The issue is that my script just stops once the command for ssh to Windows server is executed.问题是,一旦执行到 Windows 服务器的 ssh 命令,我的脚本就会停止。

My shell script below:我的shell脚本如下:

#!/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.我也尝试过不使用 echo 命令,但它仍然挂起。 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.除非您安装 SSH 守护程序,否则您无法 ssh 进入 Windows 机器。 You have two options:您有两个选择:

1- You need to connect via Remote PowerShell (WINRM). 1- 您需要通过远程 PowerShell (WINRM) 进行连接。

2- Install SSH daemon and connect via SSH (Check here ) 2- 安装 SSH 守护进程并通过 SSH 连接(检查这里

3- You can also use PowerShell Web Access (Windows Servers.) 3- 您还可以使用PowerShell Web 访问(Windows 服务器。)

UPDATE : If you are using PowerShellServer.com then there are some things you can do to try to understand more.更新:如果您使用的是 PowerShellServer.com,那么您可以做一些事情来尝试了解更多信息。

  • Test scriptname.ps1 locally in Windows Server在 Windows Server 本地测试scriptname.ps1

  • Edit your scriptname.ps1 in the first line to create a file somewhere and after execution check if that file exists.编辑第一行中的scriptname.ps1以在某处创建一个文件,并在执行后检查该文件是否存在。 This way you will know if script is being executed or not.这样您就可以知道脚本是否正在执行。

  • Execute Get-ExecutionPolicy and check you can run scripts.执行Get-ExecutionPolicy并检查您是否可以运行脚本。 Remove constraints by temporary Set-ExecutionPolicy unrestricted通过临时Set-ExecutionPolicy unrestricted移除约束

使用“期望”脚本尝试了该操作,现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM