powershell在Invoke-Command中使用示波器修改器进行VM

发布于 2025-02-12 16:09:11 字数 813 浏览 2 评论 0 原文

我正在尝试将变量$ dnsclientserveraddr的价值传递到一个命令命令-vmname中。我的选择是“使用”范围修改器。我似乎无法传达其价值:

[int]$NumberOfVMs = Read-Host "NUMBER OF VMs TO BE CONFIGURED"
$VMName = Read-Host "VM NAME PREFIX                "
[int]$VMNameSuffix = Read-Host "STARTING NUMBER               "
$DNSClientServerAddr = Read-Host "DNS CLIENT SERVER IP ADDRESS  "
For ($Count = 1; $Count -le $NumberOfVMs; $Count++, $VMNameSuffix++) {
    $VMNameFull = "$VMName" + "$VMNameSuffix"
    Invoke-Command -VMName $VMNameFull -ScriptBlock {
        $Using:DNSClientServerAddr
        Write-Host $DNSClientServerAddr }
}

当我在Write-host $ dnsclientserveraddr中进行测试时,我得到的总是空白。与此一样简单的代码中的vm中“使用”对VM的命令命令是否有效?还是我真的必须使用new -pssession和-argumentList?另外,Hyper-V是否应该在我应该配置的VM中处于活动状态/安装?

顺便说一句,我正在使用PowerShell版本5.1。

谢谢你!

I'm trying to pass the value of the variable $DNSClientServerAddr inside an Invoke-Command -VMName. My option is the 'using' scope modifier. I can't seem to pass its value:

[int]$NumberOfVMs = Read-Host "NUMBER OF VMs TO BE CONFIGURED"
$VMName = Read-Host "VM NAME PREFIX                "
[int]$VMNameSuffix = Read-Host "STARTING NUMBER               "
$DNSClientServerAddr = Read-Host "DNS CLIENT SERVER IP ADDRESS  "
For ($Count = 1; $Count -le $NumberOfVMs; $Count++, $VMNameSuffix++) {
    $VMNameFull = "$VMName" + "$VMNameSuffix"
    Invoke-Command -VMName $VMNameFull -ScriptBlock {
        $Using:DNSClientServerAddr
        Write-Host $DNSClientServerAddr }
}

What I get is always blank when I test to see in Write-Host $DNSClientServerAddr. Does 'using' work with Invoke-Command to a VM in a code as straightforward as this? Or do I really have to use New-PSSession and -ArgumentList? Also, should Hyper-V be active/installed in the VMs that I'm supposed to configure?

By the way, I'm using Powershell version 5.1.

Thank you!

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

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

发布评论

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

评论(1

旧情勿念 2025-02-19 16:09:11

$ dnsclientserveraddr 在远程会话中未定义。
您可以直接使用直接通过 $使用本地变量:

Invoke-Command -VMName $VMNameFull -ScriptBlock {
    Write-Host $Using:DNSClientServerAddr
}

您也可以分配该变量,因此它成为远程会话的本机,并丢弃使用使用 $的需求范围修改器,但您仍然必须进行初始分配。

Invoke-Command -VMName $VMNameFull -ScriptBlock {
    $DNSClientServerAddr = $Using:DNSClientServerAddr
    Write-Host $DNSClientServerAddr
}

参考

About_remote_variables

$DNSClientServerAddr is not defined in your remote session.
You can use a local variable through $Using directly like this:

Invoke-Command -VMName $VMNameFull -ScriptBlock {
    Write-Host $Using:DNSClientServerAddr
}

You could also assign the variable so it become native to the remote session and discard the need of using the $using scope modifier but you still have to do an initial assignment.

Invoke-Command -VMName $VMNameFull -ScriptBlock {
    $DNSClientServerAddr = $Using:DNSClientServerAddr
    Write-Host $DNSClientServerAddr
}

References

About_remote_variables

About_Using

About_scopes

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