locked
Script to find some Attributes from Servers RRS feed

  • Question

  • Hello,

    I am trying to find Description, OS, and DNSHOstname from some servers in my list.

    I know that some servers are not longer valid, so I am trying to build a script (below) that provides me that information and maybe saying something like FOUND no FOUND...

    Could someone help me seeing my error?

    I get an error message when the script does not find the script

    Thanks

    $Servers = Get-Content 'C:\Jim Reports\ServerOwner.txt'
    foreach ($Server in $Servers) {
    
    
        if ($test = Get-ADComputer -Identity $server -ErrorAction Stop) {
    
            Get-ADComputer "$server" -Properties description , operatingsystem, dnshostname |
            Select Name, description, operatingsystem, dnshostname |
            Export-CSV "C:\Jim Reports\ServerInfo.csv" -NoTypeInformation -Encoding UTF8
       
    }}


    • Edited by mcubac Monday, May 20, 2019 8:31 PM
    Monday, May 20, 2019 8:31 PM

Answers

  • You are still guessing and doing it badly.

    Get-Content 'C:\Jim Reports\ServerOwner.txt' | 
        ForEach-Object{
            Get-AdComputer -Filter "Name -eq '$_'" -Properties description, operatingsystem, dnshostname
        } |
        Select-Object Name, description, operatingsystem, dnshostname |
        Export-CSV 'C:\Users\Jim Reports\ServerInfo.csv' -NoTypeInformation -Encoding UTF8  


    \_(ツ)_/

    • Marked as answer by mcubac Tuesday, May 21, 2019 12:20 PM
    Tuesday, May 21, 2019 12:06 PM

All replies

  • You cannot use stop with this. 

    To find computers froma  file:

    Get-Content 'C:\Jim Reports\ServerOwner.txt'  | Where-Object{ Get-AdComputer -Filter "Name -eq '$_'" }

    This will find all valid servers by name with no errors


    \_(ツ)_/



    Monday, May 20, 2019 8:49 PM
  • Thank you so much for your help

    With the line that you used i was able to do the following:

    $servers = Get-Content 'C:\Jim Reports\ServerOwner.txt'  | Where-Object{ Get-AdComputer -Filter "Name -eq '$_'" }
    
    foreach ($Server in $Servers) {
    
            Get-ADComputer "$server" -Properties description , operatingsystem, dnshostname |
            Select Name, description, operatingsystem, dnshostname |
            Export-CSV "C:\Users\Jim Reports\ServerInfo.csv" -NoTypeInformation -Encoding UTF8
       }    
    If i do not have the export tab, it works.. but when I used EXPORT, it does not build the CSV, it gives me the information of only 1 server.  Should I be using another Export?

    Tuesday, May 21, 2019 12:00 PM
  • You are still guessing and doing it badly.

    Get-Content 'C:\Jim Reports\ServerOwner.txt' | 
        ForEach-Object{
            Get-AdComputer -Filter "Name -eq '$_'" -Properties description, operatingsystem, dnshostname
        } |
        Select-Object Name, description, operatingsystem, dnshostname |
        Export-CSV 'C:\Users\Jim Reports\ServerInfo.csv' -NoTypeInformation -Encoding UTF8  


    \_(ツ)_/

    • Marked as answer by mcubac Tuesday, May 21, 2019 12:20 PM
    Tuesday, May 21, 2019 12:06 PM