I am trying to change all vms from dhcp to static via a txt file. but I am stuck with the array for loop
the txt file is like this
NAME;IPADDR;NETMASK;GW;DNS1;DNS2
any idea here?
You can try this one
### set static IP addressing – save as setstatic.ps1 $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration ` | where{$_.IPEnabled -eq “TRUE”} Foreach($NIC in $NICs) { $NIC.EnableStatic(“192.168.1.5″, “255.255.255.0″) $NIC.SetGateways(“192.168.1.254″) $DNSServers = “198.168.1.1″,”198.168.1.1″ $NIC.SetDNSServerSearchOrder($DNSServers) $NIC.SetDynamicDNSRegistration(“FALSE”) }
### ### set dynamic addressing – save as setdynamic.ps1 $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration ` | where{$_.IPEnabled -eq “TRUE”} Foreach($NIC in $NICs) { $NIC.EnableDHCP() $NIC.SetDNSServerSearchOrder() }
### Then create shortcuts to the two .ps1 files using the following targets:
%windir%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\setdynamic.ps1
%windir%\system32\WindowsPowerShell\v1.0\powershell.exe c:\scripts\setstatic.ps1
Then save each shortcut to desktop, and now You can switch IP addresses in about 15 seconds.
If you change your main loop to
$array = Import-Csv "C:\servers.txt" -Delimiter ';'
foreach ($line in $array) { Set-STATIC -NAME $line.NAME -IPADDR $line.IPADDR -NETMASK $line.NETMASK -GW $line.GW -DNS1 $line.DNS1 -DNS2 $line.DNS2 }
it should work with your input file, provided of course that the first line contains the column headers.
Somethng like this
NAME;IPADDR;NETMASK;GW;DNS1;DNS2
vm1;192.168.1.1;255.255.255.0;192.168.1.254;192.168.1.100;192.168.1.101
vm2;192.168.1.2;255.255.255.0;192.168.1.254;192.168.1.100;192.168.1.101
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello,
Not sure if you have tried this on a single PC. What I noticed with setting the ip-address remotely with WMI is that you do loose connectivity in the script because the initial tcp connection is reset. This makes your script wait endlessly. I used the script below recently to achieve this task. The tricky bit with the script is with running it on 64 bit windows 2008 servers. The workaround for that is to run the script from powercli 5.0 and you should be fine.
Hope the script I used can be of use for you. This script sends the handle to vcenter to deal with the changes, that way you don't have to worry about tcp resets.
The script gets input from a csv file with fields Name, Ipaddr and Gateway. Connects to Hosts with the credentials below and ESXi makes the required changes.
$erroractionpreference = "SilentlyContinue"
Connect-VIServer #viservername
Get-Cluster #cluster-name
$serverlist = Import-Csv p:\ipnamechange.csv -useculture
foreach ($server in $serverlist)
{
Get-VMGuestNetworkInterface -VM $server.Name -HostCredential $hcred -GuestCredential $gcred |
where-object {$_.ippolicy -eq "dhcp"} |
Set-VMGuestNetworkInterface -HostCredential $hcred -GuestCredential $gcred -IPPolicy Static `
-Gateway $server.Gateway -ip $server.Ipaddr -Netmask "255.255.252.0" -WinsPolicy:Static `
-Wins @("10.6.1.2","10.6.1.2") -DnsPolicy:Static -Dns @("10.6.1.1","10.6.3.1") -ToolsWaitSecs 60
}
Hi Luc
I tried this but when i check the machine, It does change from dhcp to static but does not change the IP and leaves the default gateway empty.
I think its because it loses connectivity to the remote machine.
I tried using csv format like this
and I was able to change the IP address to static but still leaves the DG empty.
Can you try the SetGateways method with 2 parameters, the gateway IP and the metric ?
See the description for the SetGateways method at MSDN
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference