I am hoping you can help me with creating a simple Powershell script to query Active Directory attributes. I basically need to extract a users Logon Name (samAccountName), using an input file with Last Name, First Name and export this data to a CSV file. The biggest challenge I have with this script is to incorporate error handling. I would like to report on any errors if the user was not found in AD. The script below works for the most part but does not report on users not found in AD. It simply skips the error and does not report it in the export. Please feel free to start with the script below and rewrite it entirely.<\/p>\n
ForEach ($User in (Get-Content D:\\ADTools\\Scripts\\input.txt | ConvertFrom-CSV -Header LastName, FirstName))<\/p>\n
{ $Filter = “givenName -like “”$($User.FirstName)<\/em>”\" -and sn -like “”$($User.LastName)“”\"<\/p>\n Get-ADUser -Filter $Filter | Select name,samAccountName | Export-Csv “D:\\ADTools\\Scripts\\ExportNames.csv” -Append<\/p>\n }<\/p>\n Sample Input.txt file:<\/strong><\/p>\n Amsden,Lisa<\/p>\n Archer,Trina<\/p>\n Baker,Richard<\/p>\n Balderston,Daniel<\/p>\n Sample Export file: (If the account is not found, it simply excludes it. I would like an entry in the samAccountName column stating ‘Acct not found’ or something similar.<\/strong><\/p>\n how about like so:<\/p>\n I am hoping you can help me with creating a simple Powershell script to query Active Directory attributes. I basically need to extract a users Logon Name (samAccountName), using an input file with Last Name, First Name and export this data to a CSV file. The biggest challenge I have with this script is to incorporate error handling. I would like to report on any errors if the user was not found in AD. The script below works for the most part but does not report on users not found in AD. It simply skips the error and does not report it in the export. Please feel free to start with the script below and rewrite it entirely.<\/p>\n ForEach ($User in (Get-Content D:\\ADTools\\Scripts\\input.txt | ConvertFrom-CSV -Header LastName, FirstName))<\/p>\n { $Filter = “givenName -like “”$($User.FirstName)<\/em>”\" -and sn -like “”$($User.LastName)“”\"<\/p>\n Get-ADUser -Filter $Filter | Select name,samAccountName | Export-Csv “D:\\ADTools\\Scripts\\ExportNames.csv” -Append<\/p>\n }<\/p>\n Sample Input.txt file:<\/strong><\/p>\n Amsden,Lisa<\/p>\n Archer,Trina<\/p>\n Baker,Richard<\/p>\n Balderston,Daniel<\/p>\n Sample Export file: (If the account is not found, it simply excludes it. I would like an entry in the samAccountName column stating ‘Acct not found’ or something similar.<\/strong><\/p>\n Welcome!<\/p>\n If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n\n
\n \n
name
<\/th>\n
samAccountName
<\/th>\n<\/tr>\n<\/thead>\n\n
LISA AMSDEN
<\/td>\n
user1
<\/td>\n<\/tr>\n\n
TRINA ARCHER
<\/td>\n
user2
<\/td>\n<\/tr>\n\n
TRINA ARCHER 1
<\/td>\n
user3
<\/td>\n<\/tr>\n\n
RICHARD BAKER
<\/td>\n
user4
<\/td>\n<\/tr>\n\n
DANIEL BALDERSTON
<\/td>\n
user5
<\/td>\n<\/tr>\n\n
RICHARD BAUGHMAN
<\/td>\n
user6
<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>","upvoteCount":6,"answerCount":5,"datePublished":"2018-04-04T14:09:06.000Z","author":{"@type":"Person","name":"mirkhan","url":"https://community.spiceworks.com/u/mirkhan"},"acceptedAnswer":{"@type":"Answer","text":"ForEach ($User in (Get-Content D:\\ADTools\\Scripts\\input.txt | ConvertFrom-CSV -Header LastName, FirstName)) {\n $Filter = \"givenName -like \"\"*$($User.FirstName)*\"\" -and sn -like \"\"$($User.LastName)\"\"\"\n\n $aduser = Get-ADUser -Filter $Filter | Select name, samAccountName\n\n if ($aduser) {\n $aduser | Export-Csv \"D:\\ADTools\\Scripts\\ExportNames.csv\" -Append\n }\n else {\n new-object PSCustomObject -Property @{name = \"$($User.FirstName) $($User.LastName)\";samaccountname = 'User not found'} |\n Export-Csv \"D:\\ADTools\\Scripts\\ExportNames.csv\" -Append\n }\n}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2018-04-04T14:47:43.000Z","url":"https://community.spiceworks.com/t/powershell-script-with-error-handeling/644304/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"
\n\n
\n \n
name
<\/th>\n
samAccountName
<\/th>\n<\/tr>\n<\/thead>\n\n
LISA AMSDEN
<\/td>\n
user1
<\/td>\n<\/tr>\n\n
TRINA ARCHER
<\/td>\n
user2
<\/td>\n<\/tr>\n\n
TRINA ARCHER 1
<\/td>\n
user3
<\/td>\n<\/tr>\n\n
RICHARD BAKER
<\/td>\n
user4
<\/td>\n<\/tr>\n\n
DANIEL BALDERSTON
<\/td>\n
user5
<\/td>\n<\/tr>\n\n
RICHARD BAUGHMAN
<\/td>\n
user6
<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>","upvoteCount":6,"datePublished":"2018-04-04T14:09:06.000Z","url":"https://community.spiceworks.com/t/powershell-script-with-error-handeling/644304/1","author":{"@type":"Person","name":"mirkhan","url":"https://community.spiceworks.com/u/mirkhan"}},{"@type":"Answer","text":"