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?