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

  • Question

  • 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 

    Friday, May 16, 2014 8:58 AM

Answers

  • Hi,

    in the final line you call Export-Csv. which is a great command at exporting information. However, for it to work it needs a path to export to (D:\PowerShellforAD\test2.csv in your case) and Information (InputObject) to export.

    You do not give that command any Information, so it throws an error.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Proposed as answer by jrv Friday, May 16, 2014 2:09 PM
    • Marked as answer by noobycy Sunday, May 18, 2014 10:42 PM
    Friday, May 16, 2014 9:30 AM

All replies

  • Hi,

    in the final line you call Export-Csv. which is a great command at exporting information. However, for it to work it needs a path to export to (D:\PowerShellforAD\test2.csv in your case) and Information (InputObject) to export.

    You do not give that command any Information, so it throws an error.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Proposed as answer by jrv Friday, May 16, 2014 2:09 PM
    • Marked as answer by noobycy Sunday, May 18, 2014 10:42 PM
    Friday, May 16, 2014 9:30 AM
  • On top of not using Export correctly the rest of the script generates no exportable objects.


    ¯\_(ツ)_/¯

    Friday, May 16, 2014 2:09 PM