简体   繁体   English

在远程 SSH 服务器上运行本地 PowerShell 脚本

[英]Running local PowerShell script on remote SSH server

I am wondering if it is possible to run a local PowerShell script on the remote server that I SSH into.我想知道是否可以在我 SSH 进入的远程服务器上运行本地 PowerShell 脚本。 This preferably has to be done over a script like Paramiko where I can just run the Paramiko Python script and it does everything – SSH and run the local PowerShell script all at once.这最好通过像 Paramiko 这样的脚本来完成,我可以在其中运行 Paramiko Python 脚本,它可以完成所有操作 - SSH 并一次运行本地 PowerShell 脚本。

I've searched around and there doesn't seem to be any conclusive answers.我四处搜寻,似乎没有任何确凿的答案。

I've tried Python Paramiko and it does work.我试过 Python Paramiko,它确实有效。 However, all I can do is SSH into the remote server and copy the PowerShell script files over using SFTP to run the the script.但是,我所能做的就是将 SSH 复制到远程服务器并使用 SFTP 复制 PowerShell 脚本文件来运行脚本。 Is it possible to not use SFTP at all?是否可以根本不使用 SFTP? Where I can just run the script locally without sending it over?我可以在哪里只在本地运行脚本而不发送它?

Thanks.谢谢。

Codes that I tried:我尝试过的代码:

cmd4 = "powershell; echo './script.ps1' | powershell -File C:\\Users\\Desktop\\script.ps1"
cmd5 = "C:\\Users\\Desktop\\script.ps1"
stdin, stdout, stderr = ssh.exec_command(cmd4)
stdin.write(cmd5 + '\n')
stdin.flush()
time.sleep(5)

lines = stdout.readlines()
print (stdout.readlines())

I've tried printing the stdout but it is empty.我试过打印stdout ,但它是空的。

Also tried this:也试过这个:

cmd5 = "C:\\Users\\Desktop\\script.ps1"
cmd6 = "powershell; echo './script.ps1;' | powershell -noprofile -noninteractive -command { $input | iex }"
stdin, stdout, stderr = ssh.exec_command(cmd6)
stdin.write(cmd5 + '\n')
stdin.flush()
time.sleep(5)

lines = stdout.readlines()
print (stdout.readlines())

Run powershell on the server and feed the script to its input.在服务器上运行powershell并将脚本提供给它的输入。

Just combine these two together:只需将这两者结合在一起:

A trivial (untested) example:一个简单(未经测试)的例子:

with open("C:\\Users\\Desktop\\script.ps1", "r") as f:
    script = f.read() + "\n"

cmd = "powershell -noprofile -noninteractive -"
stdin, stdout, stderr = ssh.exec_command(cmd)
stdout.channel.set_combine_stderr(True)
stdin.write(script)
stdin.flush()
stdin.close()

lines = stdout.readlines()
print(lines)

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

相关问题 通过 PowerShell 在远程服务器上运行批处理脚本 - Running Batch Script on remote Server via PowerShell Powershell 脚本:通过 SSH 连接到服务器 - Powershell Script: SSH into Server 如何使用Powershell脚本将本地变量写入远程服务器中的文件? - How to write local variable to a file in remote server using Powershell script? 在可通过本地 IP 访问的远程服务器上运行 Powershell 脚本 - Run Powershell script on remote server accessible through local IP 使用WinRM在远程Windows服务器上运行PowerShell脚本 - Running a PowerShell script on a remote Windows server using WinRM Powershell脚本SSH到Unix服务器 - Powershell Script to SSH to unix server Powershell脚本从本地服务器运行,但不在活动目录中的客户端运行 - powershell script running from local server but not from client in active directory Powershell脚本,用于通过远程Powershell连接在服务器上创建SQL Server数据库的本地* .bak备份 - Powershell Script for creating a local *.bak backup of a SQL Server database on a server via a remote Powershell connection Powershell 远程服务器上的脚本失败 - Powershell Script failure on Remote Server 借助 Posh-SSH 模块 (W10) 在远程服务器上运行本地 python 脚本 - Run local python script on remote server thanks to Posh-SSH module (W10)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM