Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7561883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:16:26+00:00 2026-05-30T13:16:26+00:00

I have a script that scans all my servers in my domains and outputs

  • 0

I have a script that scans all my servers in my domains and outputs to two separate CSV files – one simple and one extensive.

I write one line at the time to my csv.. This results in thousands of file-open and file-close.. I’ve lurked around and understand that I should first gather all the info and write it all in one sweep at the end of the script. But how do I do this with export-csv (preferably using a function)?

And is there a way I can use the same function for short and long list?

The script performs numerous tasks on each domain/server, but I’ve trimmed it down to this for your viewing pleasure:

$domains = "no","se","dk"

# Loop through specified forests
foreach ($domain in $domains) {

    # List all servers
    $servers = Get-QADComputer 

    # Looping through the servers
    foreach ($server in $servers) {

        # GENERATE LONGLIST #
        # Ping server
        if (Test-Connection -ComputerName $server.name -count 1 -Quiet )
        {
            $Reachable = "Yes"

            # Fetch IP address
            $ipaddress = [System.Net.Dns]::GetHostAddresses($Server.name)|select-object IPAddressToString -expandproperty IPAddressToString

            # Formatting output and export all info to CSV
            New-Object -TypeName PSObject -Property @{
                SystemName = ($server.name).ToLower()
                Reachable = $Reachable
                Domain = $server.domain
                IPAddress = $IPAddress
                } | Select-Object SystemName,Domain,IPAddress| Export-Csv -Path "longexport.csv" -append    
        } 
        else # Can't reach server
        {
            $reachable = "No"
            $IPAddress = "Unknown"

            # Formatting output and export all info to CSV
            New-Object -TypeName PSObject -Property @{
                SystemName = ($server.name).ToLower()
                Reachable = $Reachable
                Domain = $server.domain
                } | Select-Object SystemName,Domain,IPAddress| Export-Csv -Path "shortexport.csv" -append
        }
    }
}

(and let me just say that I know that I cannot do -append with export-csv, but I am using a function that let’s me do this..)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:16:28+00:00Added an answer on May 30, 2026 at 1:16 pm

    You are exporting the same amount of properties to each file so I’m not sure I understand why one of them is considered long and one short. Anyway, I suggest the following, don’t assign all computers to a variable, it can take up a lot of RAM, instead use a streaming way (one object at a time) and use foreach-object. Also, since I find no difference in the files I output to the file at the end of each domain operation (one per domain). And with another twick you could write only once to the file.

    $domains = "no","se","dk"
    
    foreach ($domain in $domains) {
    
        Get-QADComputer -Service $domain -SizeLimit 0 | Foreach-Object {
    
            $reachable = Test-Connection -ComputerName $_.Name -count 1 -Quiet
    
            if($reachable)
            {
                $IPAddress = [System.Net.Dns]::GetHostAddresses($_.Name)|select-object IPAddressToString -expandproperty IPAddressToString
            }
            else
            {
                $IPAddress = $null
            }
    
    
            New-Object -TypeName PSObject -Property @{
                    SystemName = $_.Name.ToLower()
                    Reachable = $reachable 
                    Domain = $_.Domain
                    IPAddress = $IPAddress
            } | Select-Object SystemName,Domain,IPAddress 
    
        } | Export-Csv -Path export.csv -Append  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following script that executes all the .reg files in the current directory.
I have a Python script that scans my logs and reports all its findings.
I have a very simple python script that should scan a text file, which
I have a script that recursively loops through all the sub directories and compresses
I have an elaborate script that spans multiple functions (and files). For debugging purposes
I have a small script which scans all the ips ranging from 192.168.190.xxx to
I have to make a script in PHP that will scan other PHP files
I have a script that: creates a new folder scans an InDesign document for
I have a script that is attempting to: scan an InDesign document for all
I have a Matlab script consisting of several cells that should all be able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.