Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

How to make the result showing one time and make the result to csv file?

$
0
0

My script having problem of showing out all the AD computer name result  from Active Directory once when running it and storing the result to csv file. I am a newbie in AD and powershell.

This is the code:

$strADPathFile = "D:\Powershell for AD\ADPathList.txt"
$strErrLogFile = "D:\powershell for AD\errorADComputer.log"

$arrADPath = Get-Content -Path $strADPathFile

foreach ($strADPath in $arrADPath)
{        
    
    try
    {
    
        $objSearch = New-Object DirectoryServices.DirectorySearcher
        $objSearch.Filter = '(objectCategory=computer)'
        $objSearch.SearchRoot = $strADPath
        $objSearch.SearchScope = "Subtree"    
        $objSearch.PageSize = 1000    
        $objResults = $objSearch.Findall()
        write-host "Retrieving AD computer information in the domain $strDomain" 

        write-host "Number of AD Computer Name result: "$objResults.Count

        foreach($objResult in $objResults){  

            $objComputer = $objResult.GetDirectoryEntry()  
            write-host $objComputer.dNSHostName
            try
            {               
              #  InsertSQLTable $objComputer
            }
            catch
            {
                write-host $error[0]
                $strError = $objComputer.dNSHostName + "  " + $dateNow + $error[0]
                Add-Content $strErrLogFile $strError
            }

        }
    }
    catch
    {
        write-host $error[0]
    }

}

Export-Csv D:\PowerShellforAD\test2.csv

This is the error that I had get is that it will get the result for 2nd time again.

By the way, what is InputObject about? I had research some of it and still did not quite get it. Can anyone explain? Thanks 


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>