0

I've been working on Few automation scripts post sysprep deployment the site i am currently working on dont use DHCP due to public interaction with machines so DHCP is a no go, we use a static address for each machine

The premise is to make a CSV with all the static IP information. The script, when run on any particular PC, will read the hostname, match it to a listing in the file, pull the information, and use it to set the IP address, mask, gateway and DNS servers on that machine.

I just can't quite get it sorted out?

Here's a sample of the .csv file

computerName,IPAddress,SubnetMask,Gateway,Dns1,Dns2
TestMachine2,10.1.0.57,255.255.255.0,10.1.0.1,10.1.0.18,10.1.0.13,

any chance someone can give me a hand on a working .bat,.Vbs. or .ps1 that will implement this?

3 Answers3

1

You can use this:

$CSV = import-csv "C:\temp\IPInfo.csv"

Foreach ($Item in $CSV)
{
$Computer = $Item.ComputerName
$IPaddress = $Item.IPAddress
$Subnet = $Item.SubnetMask
$GateWay = $Item.Gateway
$DNS1 = $Item.Dns1
$DNS2 = $Item.Dns2
$NicAdapter = Get-WmiObject win32_networkadapterconfiguration -Computer $Computer -Filter "ipenabled = 'true'"

$NicAdapter.EnableStatic($IPaddress, $Subnet)
$NicAdapter.SetGateways($GateWay, 1)
$NicAdapter.SetDNSServerSearchOrder(@($DNS1,$DNS2))
}

I did not checked if the the SetDNSServerSearchOrder Method get 2 DNS Servers or only one like $NicAdapter.SetDNSServerSearchOrder($DNS1) Check it...

Avshalom
  • 8,657
  • 1
  • 25
  • 43
0

This works for me:

$csv = @"
computerName,IPAddress,SubnetMask,Gateway,Dns1,Dns2
TestMachine2,10.1.0.57,255.255.255.0,10.1.0.1,10.1.0.18,10.1.0.13
"@

$config = ConvertFrom-Csv $csv | Where { $_.computerName -eq $env:COMPUTERNAME }

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
$wmi.EnableStatic($config.IPAddress, $config.SubnetMask)
$wmi.SetGateways($config.Gateway, 1)
$wmi.SetDNSServerSearchOrder(@($config.Dns1, $config.Dns2))

First we get the config for current computer and store it in the $config variable. Then we use WMI to set configuration for ip enabled adapters.

You will probably want to remove the $csv sample from the script and use Import-Csv instead of ConvertFrom-Csv.

lahell
  • 66
  • 3
0

Here you go:

Dim shell : Set shell = CreateObject("WScript.Shell")

    csv = "TestMachine2,10.1.0.57,255.255.255.0,10.1.0.1,10.1.0.18,10.1.0.13,"
    ip = split(csv,",")(1)
    mask = split(csv,",")(2)
    gateway = split(csv,",")(3)

    shell.run "netsh interface ip set address name=""Local Area Connection"" static " & _
        ip & " " & _
        mask & " " & _
        gateway
Azevedo
  • 2,059
  • 6
  • 34
  • 52