# encoding: utf-8
# api: powershell
# title: vmware guests subnet
# description: Quick and dirty script retrieves vmware host with a specific network and change the subnet of each guest. Does not work if there are 2 ip addresses defined on one NIC, but since there was only one of those servers i could not bother to adapt the script :)
# version: 0.1
# type: script
# author: Ton Siemons
# license: CC0
# x-poshcode-id: 6837
# x-archived: 2017-05-26T18:36:22
# x-published: 2017-04-11T18:46:00
#
#
$snapins = "vmware.vimautomation.core"
foreach ($snapin in $snapins){if (!(Get-PSSnapin $snapin -ErrorAction SilentlyContinue)){Add-PSSnapin $snapin}}
$vserver = "vmware vCenter Server"
$vNetwork = "General_Services"
$logfile = "d:\Scripts\log\vm.log"
$subnet = "255.255.255.128"
connect-viserver -Server $vserver
$vms = Get-VM
foreach ($vm in $vms){
$nw = $vm|Get-NetworkAdapter
if (($nw.networkname) -like $vNetwork){
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $vm.name| where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
try{
$NIC.EnableStatic($nic.ipaddress, $subnet)
$string = "$vm is adapted"
$string
$string|Out-File -FilePath $logfile -Append -Encoding OEM
}
Catch{
$string = "$vm is not adapted"
$string
$string|Out-File -FilePath $logfile -Append -Encoding OEM
}
}
}
}