I am writing a bit of code and came across a situation where I have some output from foreach but I cannot figure out how I can export to a csv.
the code i have is as follows:
$servers = import-csv D:\server_list.csv
foreach ($server in $servers){
$servername = $server.computername
try {
Get-ADComputer $servername -properties description | select name, description -ErrorVariable SilentlyContinue
} catch{
$testpath = test-path d:\15393848.txt
if ($testpath -eq $false){
New-item -itemtype file -name 15393848.txt -path d:\
}
Add-Content -Path d:\15393848.txt -value "$servername"
}
}
It outputs correctly and puts the text in the error log if the computer doesnt exist but need to have the display output in a csv. I had a look around the web and tried to use whats there and i saw one example of one guy use objects (powershell - Gather info through several foreach and then export-csv at the end of script - Stack Overflow)
but I don’t think I am understanding the core concept in regards to this.
Any help appreciated.