Here you go bud...you were almost there - you just needed to get your server name into a form where it could be output using calculated values
# =====================================================================================================================================
#
# AUTHOR: yournamehere
# LAST UPDATE : mm/dd/yyyy
# COMMENT: Script to search all AD servers for their installed roles and features and output the info to a csv
#
# =====================================================================================================================================
#
$doneflag="*****************************Done*****************************"
#Build our source file in case it doesn't already exist or is out of date and pull in the result
import-module activedirectory
Get-ADComputer -Filter {OperatingSystem -Like "Windows Server*"} | `
Select-Object -ExpandProperty Name | `
Out-file "\\yoursevername\c$\All_Servers.txt"
#Pull your results back in
$computers= get-content '\\yoursevername\c$\All_Servers.txt'
#delete the existing file we're about to created if it's there since we're running an append...otherwise, we'd need to manually delete the file before each script run
del "\\yoursevername\c$\All_Servers_Installed_Roles_and_Features.csv"
#Iterate through each server and pull desired info - we have to use a calculated value to prepend the server name to each output row since Get-WindowsFeature doesn't supply it
foreach($computer in $computers){
Get-WindowsFeature -ComputerName $computer | Where-Object {$_. installstate -eq "installed"} |
Select @{Name='Server';Expression={$computer}}, DisplayName, Name, FeatureType, Parent, Path |
export-csv "\\yoursevername\c$\All_Servers_Installed_Roles_and_Features.csv" -Append -NoTypeInformation -Encoding UTF8
}
$doneflag