【问题标题】:Changing IP with PowerCLI in Powershell在 Powershell 中使用 PowerCLI 更改 IP
【发布时间】: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 但这不起作用

提前感谢您的帮助。

马克

【问题讨论】:

标签: powershell powercli


【解决方案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

【讨论】:

    【解决方案2】:

    Set-VMGuestNetworkInterface 可能是另一种剥猫皮的方法?

    或者更好的是,通过Set-OSCustomizationNicMapping 使用 sysprep 更改 IP 地址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多