如何从 Linux 计算机挂起 Hyper-V VM?

发布于 2024-11-24 00:05:20 字数 284 浏览 6 评论 0原文

我在运行 Ubuntu 的计算机上设置了一个 NMS 系统,该系统通过调用 Perl 脚本来遍历所有 VMWare 主机并挂起所有 VM 来响应各种 UPS 事件。 VMWare 很聪明,提供了一组 Perl 模块,这使得这变得相对容易。然而,我们还有三台 Hyper-V 主机,但我似乎找不到一种非特定于某些 Microsoft 技术(例如 PowerShell 脚本)的方法来控制它们。

我希望有人能建议一种从 Linux 机器控制 Hyper-V 主机的方法。我宁愿它不涉及使用 Wine,但如果没有其他可行的方法,我愿意走这条路。

I've set up an NMS system on a machine running Ubuntu that responds to various UPS events by calling a Perl script to go through all of our VMWare hosts and suspend all of the VMs. VMWare was smart and provided a set of Perl modules which made this relatively easy. We also have three Hyper-V hosts, however, and I can't seem to find a way to control them that isn't specific to some Microsoft technology (e.g. a PowerShell script).

I'm hoping somebody could suggest a way to control the Hyper-V hosts from a linux box. I'd rather it didn't involve using Wine, but I'm willing to go that route if there's nothing else that will work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2024-12-01 00:05:20

我发现了一种丑陋的方法来做到这一点,但至少它不需要在虚拟机主机上安装或配置任何东西。

首先,我得到了一个名为 winexe 的实用程序,它可以让您打开到窗户机器。

然后我编写了一个长而难看的 Perl 脚本,将一些 PowerShell 代码通过管道传输到机器以暂停任何正在运行的机器:

sub hv_suspend_host {
    my $host = $_[0];
    my $code = <<'END';
        echo '===BEGIN'
        $query = "SELECT * FROM Msvm_ComputerSystem WHERE EnabledState != 3 AND EnabledState != 32769" #Exclude off and saved VMs
        $VMs = get-wmiobject -query $query -namespace "root\virtualization" -computername "."
        foreach ($VM in $VMs) {
            if ($VM.name -ne $VM.ElementName) { # Exclude the host itself
                if ($VM.RequestStateChange(32769).ReturnValue -eq 4096) { # Put the VM in a saved state
                    # It worked, log success
                } else {
                    # It didn't, log failure
                }
            }
        }
        echo '===END'

        exit

END
    my $recv;
    run(["winexe", '-U', "DOMAIN/$win_user%$win_pass", '--interactive=0', "//$host", 'powershell -command -'], \$code, \$recv);
    $recv =~ tr/\r//d;      # Convert to UNIX line endings
    $recv =~ /===BEGIN\n(.+)===END/s;   # Now recv contains anything you logged
}

您可能需要对此进行一些修改才能使其正常工作。我必须破解一些特定于实现的东西,但我留下了部分输出捕获代码。这需要名为 $win_user 和 $win_pass 的全局变量,其中包含目标 VM 主机的管理员帐户登录信息。它还要求您使用 IPC::Run

I found an ugly way to do it, but at least it doesn't require anything to be installed or configured on the VM host.

First I got a utility called winexe, which lets you open a terminal connection to a windows machine.

Then I wrote a long an ugly Perl script to pipe some PowerShell code to the machine to suspend any running machines:

sub hv_suspend_host {
    my $host = $_[0];
    my $code = <<'END';
        echo '===BEGIN'
        $query = "SELECT * FROM Msvm_ComputerSystem WHERE EnabledState != 3 AND EnabledState != 32769" #Exclude off and saved VMs
        $VMs = get-wmiobject -query $query -namespace "root\virtualization" -computername "."
        foreach ($VM in $VMs) {
            if ($VM.name -ne $VM.ElementName) { # Exclude the host itself
                if ($VM.RequestStateChange(32769).ReturnValue -eq 4096) { # Put the VM in a saved state
                    # It worked, log success
                } else {
                    # It didn't, log failure
                }
            }
        }
        echo '===END'

        exit

END
    my $recv;
    run(["winexe", '-U', "DOMAIN/$win_user%$win_pass", '--interactive=0', "//$host", 'powershell -command -'], \$code, \$recv);
    $recv =~ tr/\r//d;      # Convert to UNIX line endings
    $recv =~ /===BEGIN\n(.+)===END/s;   # Now recv contains anything you logged
}

You might have to mess with this a bit to get it to work. I had to hack out some of the implementation-specific things, but I left in part of the output capturing code. This requires global variables named $win_user and $win_pass containing administrator account login info for the target VM host. It also requires that you use IPC::Run.

〃温暖了心ぐ 2024-12-01 00:05:20

Hyper-V 可以使用 WMI 界面进行远程管理。 Linux 上有一个 WMI 客户端,它应该允许您进行相关的 API 调用来管理 Hyper-V。我不必自己执行此操作,但 Microsoft 提供了特定的 WMI 调用:https://msdn.microsoft.com/en-us/library/hh850319%28v=vs.85%29.aspx

Hyper-V can be managed remotely using the WMI interfaces. There is a WMI Client for Linux, which should allow you to make the relevant API calls to manage Hyper-V. I have not had to do this myself, but the specific WMI calls are available at Microsoft: https://msdn.microsoft.com/en-us/library/hh850319%28v=vs.85%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文