Invoke-VMScript - 如何将VM的网关变为变量?

时间:2018-02-21 13:27:50

标签: powershell powercli

我目前正在处理脚本,这些脚本将用于使用模板和CSV自动创建VMWare上的VM过程。

更改VM的网络参数后,我想将我的CSV与VM的网络配置进行比较,以确认它是否匹配。

与IP进行比较但不与网关进行比较。

我没有设法让它发挥作用。你能帮帮我吗?

脚本的一部分:

$vm= import-csv C:\temp\create_vm.csv -Delimiter ';'

foreach($vms in $vm){

 $ipmodif = "New-NetIPAddress -InterfaceIndex 12 -ipaddress $($vms.ip) -PrefixLength 24 -DefaultGateway $($vms.gateway)" 

   [void](Invoke-VMScript -VM $vms.machine -ScriptType Powershell -ScriptText $ipmodif -GuestUser Administrator -GuestPassword password)


   Write-host "INSTALLATION DE $($vms.machine)..." -ForegroundColor Yellow


 $ipget= Get-VM -name $vms.machine | Select @{N="IP";E={@($_.guest.IPAddress[0])}}

 $gateway = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | select-object DefaultIPGateway

 $gatewayget = Invoke-VMScript -VM $vms.machine -ScriptType Powershell -ScriptText "$($gateway)"  -GuestUser Administrator -GuestPassword Password

   if ($vms.ip -eq $ipget.ip){Write-host  "Adresse IP ($($vms.ip))      : " -NoNewline
                              Write-host "OK" -foreground green}

        else {Write-host  "Adresse IP ($($vms.ip)) : " -NoNewline
              Write-host "NOK" -foreground red}

   if ($vms.gateway -eq $gatewayget.scriptoutput){Write-host  "Gateway ($($vms.gateway)): " -NoNewline
              write-host "OK" -foreground green}

        else {Write-host  "Gateway ($($vms.gateway)): " -NoNewline
              Write-host "NOK" -foreground red}}

我知道问题与变量$ gatewayget有关,但我无法找到它。

感谢您的帮助!

0 个答案:

没有答案
相关问题