Hyper-V PowerShell-在Linux VM中运行bash命令并获取输出 [英] Hyper-V PowerShell - Run a bash command in Linux VM and get output

查看:90
本文介绍了Hyper-V PowerShell-在Linux VM中运行bash命令并获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有

  1. 具有Hyper-V和PowerShell 5.1的Windows Server.您可以添加和使用任何PowerShell模块,例如"Hyper-V".

  2. Linux虚拟机.它是通用的,因此不允许您安装不常见的应用程序,例如PowerShell Core.

现在您正在使用(1)Hyper-V,您可以在其中运行PowerShell脚本.

问题.如何在(2)Linux VM上运行Bash命令并使用该PowerShell从Hyper-V中获得其输出?

我相信这个问题等同于创建并运行问题.ps1,该问题在Linux VM中运行bash/tmp/linux.sh并获得所需的输出100 ,其中/tmp/linux.sh是如图所示.

  ||Hyper-V(主机)|Linux VM(来宾)|| ---------- | --------------------------------------------------- ||IPv4 |10.0.0.1 |10.0.0.200 ||主机名|hyperv |vm ||用户名|用户H |userL ||密码|密码H |密码L ||剧本|C:\ question.ps1 |/tmp/linux.sh | 

解决方案

调用.在Linux VM中,您具有/tmp/linux.sh :

 <代码>#!/bin/bashval = 100回声"$ val"退出$? 

答案(已确认).将PuTTY存储在Windows Hyper-V主机中.

提示.PuTTY便携式打印机足以满足需要: https://portableapps.com .

假设您已将PuTTY Portable存储在C:\ PortableApps \ PuTTYPortable中.

创建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 

^^将此行放在PowerShell脚本文件中的某个位置.

在PowerShell中,运行脚本文件 question.ps1 :

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

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 (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:\>

这篇关于Hyper-V PowerShell-在Linux VM中运行bash命令并获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆