简体   繁体   English

Hyper-V PowerShell - 在 Linux VM 中运行 bash 命令并获取输出

[英]Hyper-V PowerShell - Run a bash command in Linux VM and get output

Suppose you have假设你有

  1. Windows Server with Hyper-V and PowerShell 5.1.带有 Hyper-V 和 PowerShell 5.1 的 Windows Server。 You can add and use any PowerShell modules such as "Hyper-V".您可以添加和使用任何 PowerShell 模块,例如“Hyper-V”。

  2. Linux virtual machine. Linux 虚拟机。 It's generic so you are not allowed to install uncommon applications such as PowerShell Core.它是通用的,因此不允许您安装不常见的应用程序,例如 PowerShell Core。

Now you are using (1) Hyper-V where you can run a PowerShell script.现在您正在使用 (1) Hyper-V,您可以在其中运行 PowerShell 脚本。

Question.题。 How do you run Bash commands on (2) Linux VM and get the output of it using that PowerShell from Hyper-V?如何在 (2) Linux VM 上运行 Bash 命令并使用来自 Hyper-V 的 PowerShell 获取它的输出?

I believe this question is equivalent to create and run question.ps1 that runs bash /tmp/linux.sh in the Linux VM and get the desired output 100 where /tmp/linux.sh is shown in the image.我相信这个问题相当于创建并运行 question.ps1,它在 Linux VM 中运行 bash /tmp/linux.sh 并获得所需的输出 100 ,其中 /tmp/linux.sh 显示在图像中。

在 Hyper-V 中运行 PowerShell 以运行 Bash 命令并获取输出

|          | Hyper-V (host)  | Linux VM (guest) |
|----------|-----------------|------------------|
| IPv4     | 10.0.0.1        | 10.0.0.200       |
| hostname | hyperv          | vm               |
| Username | userH           | userL            |
| Password | passwordH       | passwordL        |
| Script   | C:\question.ps1 | /tmp/linux.sh    |

Recall.记起。 In the Linux VM, you have /tmp/linux.sh :在 Linux VM 中,您有/tmp/linux.sh

#!/bin/bash

val=100
echo "$val"
exit $?

Answer (Confirmed) .回答(确认) Store PuTTY in Windows Hyper-V host.将 PuTTY 存储在 Windows Hyper-V 主机中。

Tips.提示。 PuTTY Portable suffices: https://portableapps.com . PuTTY Portable 就足够了: https : //portableapps.com

Suppose you have stored PuTTY Portable in C:\\PortableApps\\PuTTYPortable.假设您已将 PuTTY Portable 存储在 C:\\PortableApps\\PuTTYPortable 中。

Create a PowerShell script file C:\\Users\\userH\\Desktop\\question.ps1创建 PowerShell 脚本文件C:\\Users\\userH\\Desktop\\question.ps1

C:\PortableApps\PuTTYPortable\App\putty\PLINK.EXE 10.0.0.200 -l userL -pw passwordL -batch bash /tmp/linux.sh

^^ Put this line somewhere in this PowerShell script file. ^^ 将此行放在此 PowerShell 脚本文件中的某个位置。

In PowerShell, run the script file question.ps1 :在 PowerShell 中,运行脚本文件question.ps1

PS C:\> & "C:\Users\userH\Desktop\question.ps1"
100
PS C:\>

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

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