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
}
}