locked
Powershell script freezes after a few machines RRS feed

  • Question

  • Hey guys,

    I'm running a script to pull the DNS configuration settings and it seems to freeze at the same point, I've removed a number of servers from my servers.txt and that doesn't seem to work. I've also tried running as administrator.

    Can you see any issues with my script at all?

    $Computernames = Get-content -path c:\servers.txt
        foreach($objItem in $Computernames)
           {
            Get-WmiObject -ComputerName $objItem -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | Select-Object -Property DNSHostName, DNSServerSearchOrder >>c:\results.csv
            }

    Thanks

    Alex

    Tuesday, May 6, 2014 2:16 PM

Answers

  • Hi Alex,

    I generally use Export-Csv instead of using >> if you're trying to get CSV output.

    That said, this is how I'd do something like you're trying to do:

    Get-Content .\servers.txt | ForEach {
    
        If (Test-Connection -ComputerName $_ -Count 1 -Quiet) {
    
            $nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $_ -Filter 'IPEnabled=True'
    
            foreach ($nic in $nics) {
    
                $props = @{
                    NIC = $nic.Caption
                    DNSHostName = $nic.DNSHostName
                    DNSServerSearchOrder = $nic.DNSServerSearchOrder -join ', '
                }
    
                New-Object PsObject -Property $props
    
            }
    
        }
    
    } | Sort DNSHostName | Export-Csv .\DNSResults.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    • Edited by Mike Laughlin Tuesday, May 6, 2014 3:37 PM
    • Proposed as answer by jrv Tuesday, May 6, 2014 3:46 PM
    • Marked as answer by Bill_Stewart Friday, July 25, 2014 9:50 PM
    Tuesday, May 6, 2014 3:36 PM

All replies

  • I would add some verbose logging to see what system is freezing the script up. From there try a manual query and see if you run into the same issue. It is possible that there is an issue on one or more remote systems with WMI queries.

    $Computernames = Get-content -path c:\servers.txt
    foreach($objItem in $Computernames) {
        Write-Verbose $Computer -Verbose
        Get-WmiObject -ComputerName $objItem -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | 
        Select-Object -Property DNSHostName, DNSServerSearchOrder Export-Csv -Append c:\results.csv -NoType
    }


    Boe Prox
    Blog | Twitter
    PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
    PowerShell Deep Dives Book




    • Edited by Boe ProxMVP Tuesday, May 6, 2014 3:41 PM Added Export-Csv in my example vs. original code (>>)
    Tuesday, May 6, 2014 3:30 PM
  • Hi Alex,

    I generally use Export-Csv instead of using >> if you're trying to get CSV output.

    That said, this is how I'd do something like you're trying to do:

    Get-Content .\servers.txt | ForEach {
    
        If (Test-Connection -ComputerName $_ -Count 1 -Quiet) {
    
            $nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $_ -Filter 'IPEnabled=True'
    
            foreach ($nic in $nics) {
    
                $props = @{
                    NIC = $nic.Caption
                    DNSHostName = $nic.DNSHostName
                    DNSServerSearchOrder = $nic.DNSServerSearchOrder -join ', '
                }
    
                New-Object PsObject -Property $props
    
            }
    
        }
    
    } | Sort DNSHostName | Export-Csv .\DNSResults.csv -NoTypeInformation


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    • Edited by Mike Laughlin Tuesday, May 6, 2014 3:37 PM
    • Proposed as answer by jrv Tuesday, May 6, 2014 3:46 PM
    • Marked as answer by Bill_Stewart Friday, July 25, 2014 9:50 PM
    Tuesday, May 6, 2014 3:36 PM
  • I generally use Export-Csv instead of using >> if you're trying to get CSV output.

    Nice catch!  Also, if running V3+, you can append to a CSV using -Append.

    Boe Prox
    Blog | Twitter
    PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
    PowerShell Deep Dives Book


    Tuesday, May 6, 2014 3:40 PM