Author Posts

March 7, 2016 at 1:46 am

I Want to take output in a file for the below script...

$computer = get-content C:\DNS.txt
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer |where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
$DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration(“TRUE”)
}

March 7, 2016 at 2:39 am

you can pipe it to the cmdlet out-file. i.e

$computer = get-content C:\DNS.txt
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $computer |where{$_.IPEnabled -eq “TRUE”}
Foreach($NIC in $NICs) {
$DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration(“TRUE”)
} | out-file c:\temp\DNS-Results.txt

March 7, 2016 at 2:49 am

Hi Graham Beer, Am getting error as an empty pipe element is not allowed...

March 7, 2016 at 6:02 am

Use the pipeline like this.

$computer = get-content C:\DNS.txt
$NICs = Get-WmiObject -Class win32_networkadapterconfiguration -Filter "ipenabled = 'True'" -ComputerName $computer
$NICs | ForEach-Object {
    $DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
    $_.SetDNSServerSearchOrder($DNSServers)
    $_.SetDynamicDNSRegistration(“TRUE”)} | out-file .\NICs.txt -Append

March 7, 2016 at 6:16 am

 $DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”

You will need to change the quotes back to standard text double quotes.

 $DNSServers = "0.0.0.0","0.0.0.0","0.0.0.0","0.0.0.0"

March 7, 2016 at 10:24 pm

Still am getting same error as "an empty pipe element is not allowed" with Line details

March 9, 2016 at 5:24 am

You can use the variable to store the results then pipe those results to a file.

$computer = get-content C:\DNS.txt
$NICs = Get-WmiObject -Class win32_networkadapterconfiguration -Filter "ipenabled = 'True'" -ComputerName localhost
$result = $NICs | ForEach-Object {
$DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
$_.SetDNSServerSearchOrder($DNSServers)
$_.SetDynamicDNSRegistration(“TRUE”)

$result | out-file c:\dns.txt -append

You can wrap the foreach loop in a subexpression

$computer = get-content C:\DNS.txt
$NICs = Get-WmiObject -Class win32_networkadapterconfiguration -Filter "ipenabled = 'True'" -ComputerName localhost
$($NICs | ForEach-Object {
$DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
$_.SetDNSServerSearchOrder($DNSServers)
$_.SetDynamicDNSRegistration(“TRUE”)}) | out-file .\NICs.txt -Append

Or you can try like below

$computer = get-content C:\DNS.txt
$NICs = Get-WmiObject -Class win32_networkadapterconfiguration -Filter "ipenabled = 'True'" -ComputerName localhost
$result = @()
$NICs | ForEach-Object {
$DNSServers = “0.0.0.0",”0.0.0.0",”0.0.0.0",”0.0.0.0”
$_.SetDNSServerSearchOrder($DNSServers)
$_.SetDynamicDNSRegistration(“TRUE”)
$result += $_
}

$result | out-file c:\dns.txt -append