powershell在Invoke-Command中使用示波器修改器进行VM
我正在尝试将变量$ 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。
谢谢你!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$ dnsclientserveraddr
在远程会话中未定义。您可以直接使用直接通过
$使用本地变量:
您也可以分配该变量,因此它成为远程会话的本机,并丢弃使用使用
$的需求范围修改器,但您仍然必须进行初始分配。
参考
About_remote_variables
$DNSClientServerAddr
is not defined in your remote session.You can use a local variable through
$Using
directly like this: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.References
About_remote_variables
About_Using
About_scopes