VMware Cloud Community
tdubb123
Expert
Expert

changing dynamic to static ip

I am trying to change all vms from dhcp to static via a txt file. but I am stuck with the array for loop

function Set-STATIC {
param($NAME,$IPADDR,$NETMASK,$GW,$dns1,$dns2)
#Get NICS via WMI
$NICs = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $NAME | where-object{$_.IPEnabled -eq $true -and $_.DHCPEnabled -eq $true})
foreach($NIC in $NICs) {
$DNSServers = $dns1,$dns2
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration("TRUE")
$IPADDR = ($NIC.IPAddress[0])
$GW = $NIC.DefaultIPGateway
$NETMASK = $NIC.IPSubnet[0]
$NIC.EnableStatic($IPADDR, $NETMASK)
$NIC.SetGateways($GW)
$NIC.Put()
}
}
$array = get-content "C:\servers.txt"
ForEach (($NAME,$IPADDR,$NETMASK,$GW,$dns1,$dns2)) in $array {
Set-STATIC -NAME $_.NAME -IPADDR $_.IPADDR -NETMASK $_.NETMASK -GW $_.GW -DNS1 $_.DNS1 -DNS2 $_.DNS2
}

the txt file is like this

NAME;IPADDR;NETMASK;GW;DNS1;DNS2

any idea here?

Reply
0 Kudos
5 Replies
srnhpp
Enthusiast
Enthusiast

                               

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.

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
BPCUE1
Contributor
Contributor

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
}

Reply
0 Kudos
tdubb123
Expert
Expert

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

function Set-Static {
  param($name,$Ipaddr,$subnetmask,$defaultGW,$dns1,$dns2)
#Get NICS via WMI
$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $Name -Filter "IPEnabled=TRUE"
foreach($NIC in $NICs) {
$DNSServers = $dns1,$dns2
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration("TRUE")
$NIC.EnableStatic($Ipaddr,$subnetmask)
$NIC.SetGateways($defaultGW)
$NIC.put()
  }
}
Import-Csv "C:\servers.csv" -UseCulture | %{
  Set-Static -Name $_.Name -Dns1 $_.dns1  -Dns2 $_.dns2 -ipaddr $_.ipaddr -subnetmask $_.subnetmask -defaultGW $_.defaultGW | Out-Null
}

and I was able to change the IP address to static but still leaves the DG empty.

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos