2015-04-01 70 views
0

我已经创建了一个脚本,在vCenter中对虚拟机进行克隆,对其进行重命名,然后在VM(通过模板的IP)加电后更改VM的IP。我通过提示询问IP应该是什么来做到这一点。唯一的问题是,我需要输入两次所需的IP。一旦标记虚拟机,然后再次更改该框的IP。在PowerShell中使用PowerCLI更改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,但这不起作用

非常感谢您的帮助。

马克

+0

[Invoke-Command脚本块不生成输出]的可能的重复(http://stackoverflow.com/questions/29370042/invoke-command-script-block-not-generating-output)看看我的答案。调用该命令时,$ NewVMIP为null。 – Matt 2015-04-01 11:29:53

回答

1

是在你的第二个脚本缺少的一个重要的事情是,你不及格$NewVMIP作为参数。当在远程系统$NewVMIP上调用脚本块时,将是$null

粗糙的猜测是,你需要做这样的事情:

Invoke-Command -ComputerName 172.30.30.110 -ScriptBlock { 
    param($ip) 

    $NewVMIP = Read-Host "Enter IP" 
    $subnet = "255.255.255.0" 
    $gateway = "172.30.30.1" 

    netsh int ip set address "Local Area Connection" static "$ip" "255.255.255.0" "172.30.30.1" 

} -credential $cred -ArgumentList $NewVMIP