简体   繁体   中英

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. You can add and use any PowerShell modules such as "Hyper-V".

  2. Linux virtual machine. It's generic so you are not allowed to install uncommon applications such as PowerShell Core.

Now you are using (1) Hyper-V where you can run a PowerShell script.

Question. How do you run Bash commands on (2) Linux VM and get the output of it using that PowerShell from Hyper-V?

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.

在 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 :

#!/bin/bash

val=100
echo "$val"
exit $?

Answer (Confirmed) . Store PuTTY in Windows Hyper-V host.

Tips. PuTTY Portable suffices: https://portableapps.com .

Suppose you have stored PuTTY Portable in C:\\PortableApps\\PuTTYPortable.

Create a PowerShell script file 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.

In PowerShell, run the script file question.ps1 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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