본문 바로가기
파워쉘(Powershell)/Active Directory

Get-ADComputer - Active Directory에서 컴퓨터 정보 읽기

by 예배파 2024. 6. 27.

Get-ADComputer를 사용하면 액티브 디렉토리에 있는 컴퓨터 정보를 읽을 수 있습니다

 

 

computers.txt에 저장된 컴퓨터 이름을 하나씩 읽어서 Name, CanonicalName을 out.csv 파일에 저장하는 방법

computers.txt 

computer1
computer2
computer3

 

New-Item .\out.csv -Force
Add-Content -Path .\out.csv -Value "Name,canonicalname"

Get-Content .\computers.txt | %{
    $comName = $_
    try{
        Get-ADComputer $comName -Properties canonicalname | Select-Object Name, canonicalname | Export-Csv -Path .\out.csv -Append
    }catch{
        $a = [PSCustomObject]@{
            Name = $comName
            canonicalName = "Not exist in AD"
        }
        $a | Export-Csv -Path .\out.csv -Append  -NoTypeInformation
    }
}