I have a new client who was using a third-party DNS provider for filtering, but we're switching to firewall-based filtering. The issue is that their previous support provider set hard-coded DNS servers on their systems, even though they are using DHCP. Now, I need to reset the DNS settings to be obtained via DHCP instead.
The challenge is that there are laptops and devices with multiple network adapters (like docking stations and Wi-Fi). I've compiled the InterfaceIndex for each adapter that is currently using the old DNS server and exported this list to a CSV file. However, when I try to import the CSV and use a ForEach loop to update the DNS settings, I'm encountering errors. Here's what I have so far:
```powershell
Get-DnsClientServerAddress -AddressFamily IPv4 | Where-Object { $_.ServerAddresses -like "10.0.1.*"} | ForEach {$_.InterfaceIndex} | Export-CSV c:windowstempifdns.csv
$intindex = Import-Csv -Path c:windowstempifdns.csv
foreach ($interface in $intindex) {Set-DNSClientServerAddress -InterfaceIndex $intindex -ResetServerAddresses}
```
The first command correctly outputs the InterfaceIndex values, and the second command seems to import them well. However, in the third command, I'm getting an error suggesting that the value being passed is not in the expected format. Any advice on how to resolve this?
2 Answers
It looks like you're overcomplicating things with the CSV import. You don't actually need to export the data to a CSV for this task. Instead, you can directly use the output of the `Get-DnsClientServerAddress` command inside your `ForEach-Object`. Here's a simplified version of the command:
```powershell
Get-DnsClientServerAddress -AddressFamily IPv4 | Where-Object { $_.ServerAddresses -like '10.0.1.*' } | ForEach-Object { Set-DNSClientServerAddress -InterfaceIndex $_.InterfaceIndex -ResetServerAddresses }
```
This should let you reset the DNS settings without the weird issues of importing and exporting data.
Just a quick thought: if your goal was to keep track of which interfaces were affected, you might want to store that data in a variable instead of a CSV. This way, you can manage it all in memory. Using a direct approach like:
```powershell
$interfaceIndexes = Get-DnsClientServerAddress -AddressFamily IPv4 | Where-Object { $_.ServerAddresses -like '10.0.1.*' } | Select-Object -ExpandProperty InterfaceIndex
foreach ($index in $interfaceIndexes) { Set-DNSClientServerAddress -InterfaceIndex $index -ResetServerAddresses }
```
This should help you avoid the type conversion errors you’re experiencing too!
Thanks! That approach sounds much cleaner and easier to manage. I'll give it a try!