I was asked to help write a script to query all machines that do not have the SCCM agent and produce their email address in a CSV file.
Import-Module 'D:\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' -Verbose
set-location "SITEID:"
$Members = Get-CMDevice -CollectionName "Inactive AD computers with SCCM 2012 Client"
$Members | foreach-object {
$Username=$_.UserName
$Computername=$_.Name
If($Username -ne 'Administrator'){
If($Username){
Try{
$email = (Get-ADUser $Username -properties mail).mail
New-Object -TypeName PSCustomObject -Property @{
Username = $Username
Computername = $Computername
Email = $email} | Export-Csv -Path "Machines.csv" -NoTypeInformation -Append
}Catch{
#who cares
}
#write-host $name
}
}#end if
}#end for each