PoshCode Archive  Artifact Content

Artifact 3906d41cd99726d6483391761107203daaf9e4904da6f54994142f5356cbfd71:

  • File vmware-guests-subnet.ps1 — part of check-in [354f12a96e] at 2018-06-10 13:32:22 on branch trunk — 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 :) (user: Ton Siemons size: 1408)

# 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: 3861
# x-archived: 2017-04-22T01:42:19
# x-published: 2013-01-04T03:04: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
			}
		}
	}
}