PoshCode Logo PowerShell Code Repository

vmware guests subnet by Rallo 6 weeks ago (modification of post by Ton Siemons view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6837"></script>download | new post

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 :)

  1. $snapins = "vmware.vimautomation.core"
  2. foreach ($snapin in $snapins){if (!(Get-PSSnapin $snapin -ErrorAction SilentlyContinue)){Add-PSSnapin $snapin}}
  3.  
  4. $vserver = "vmware vCenter Server"
  5. $vNetwork = "General_Services"
  6. $logfile = "d:\Scripts\log\vm.log"
  7. $subnet = "255.255.255.128"
  8.  
  9. connect-viserver -Server $vserver
  10. $vms = Get-VM
  11. foreach ($vm in $vms){
  12.         $nw = $vm|Get-NetworkAdapter
  13.         if (($nw.networkname) -like $vNetwork){
  14.                 $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -ComputerName $vm.name| where{$_.IPEnabled -eq “TRUE”}
  15.                 Foreach($NIC in $NICs) {
  16.                         try{
  17.                                 $NIC.EnableStatic($nic.ipaddress, $subnet)
  18.                                 $string = "$vm is adapted"
  19.                                 $string
  20.                                 $string|Out-File -FilePath $logfile -Append -Encoding OEM
  21.                         }
  22.                         Catch{
  23.                                 $string = "$vm is not adapted"
  24.                                 $string
  25.                                 $string|Out-File -FilePath $logfile -Append -Encoding OEM
  26.                         }
  27.                 }
  28.         }
  29. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me