【发布时间】:2015-04-01 11:24:25
【问题描述】:
我创建了一个脚本,在 vCenter 中克隆虚拟机,重命名它,然后在启动后更改虚拟机的 IP(从模板的 IP)。我通过提示询问 IP 应该是什么来做到这一点。唯一的问题是我需要输入两次所需的 IP。一次标记VM,然后再次更改框的IP。
有没有办法继承此处输入的变量“$NewVMIP”:
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer -Server vcenter01 -User myusername -Password Password1
Start-Sleep -s 6
$TemplateVM = "Windows QA - 172.30.30.110 - TEMPLATE"
$NewVMIP = Read-Host "Please input IP of new VM"
$NewVMFor = Read-Host "Please input Who This VM is For?"
$NewVMDate = Read-Host "Please input todays date"
$NewVMName = "Windows QA - $NewVMIP - $NewVMFor - $NewVMDate"
New-VM -Name $NewVMName -VM $TemplateVM -VMHost "esx01.coname.local"
Move-VM -VM $NewVMName -Destination Testing
Start-VM -VM $NewVMName
Start-Sleep -s 200
到脚本的第二部分:
$username = "vmadmin"
$password = "Password1"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
Invoke-Command -ComputerName 172.30.30.110 -ScriptBlock {
$NewVMIP = Read-Host "Enter IP"
$subnet = "255.255.255.0"
$gateway = "172.30.30.1"
netsh int ip set address "Local Area Connection" static "$NewVMIP" "255.255.255.0" "172.30.30.1"
} -credential $cred
所以不用第二次输入IP了?
我认为这与在 VM/vCenter 上而不是远程位置上运行的脚本的第二部分有关,因此变量不会被传递。我试图指定一个全局变量,例如 $Global:NewVMIP 但这不起作用
提前感谢您的帮助。
马克
【问题讨论】:
-
Invoke-Command script block not generating output 的可能重复项看我的回答。调用命令时
$NewVMIP为空。
标签: powershell powercli