How to take output in a file for Script

Tagged: 

This topic contains 6 replies, has 5 voices, and was last updated by Profile photo of Nagendra Narembally Nagendra Narembally 6 months, 3 weeks ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #36142
    Profile photo of Dhilipan RamMani
    Dhilipan RamMani
    Participant

    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”)
    }

    #36143
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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
    
    #36144
    Profile photo of Dhilipan RamMani
    Dhilipan RamMani
    Participant

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

    #36151
    Profile photo of random commandline
    random commandline
    Participant

    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
    
    #36154
    Profile photo of Bob McCoy
    Bob McCoy
    Participant
     $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"
    #36227
    Profile photo of Dhilipan RamMani
    Dhilipan RamMani
    Participant

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

    #36314

    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

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.