Hi there,<\/p>\n
I have been working on a small script for active directory users. I have most of it written, but one stipulation was to pull the mail attribute and assign it to a variable, and if it doesn’t exist, create a prompt in powershell to add it manually. I added an If/else statement but regardless if the mail property is set or not, it never makes it to the else portion, and overall it just doesn’t work.<\/p>\n
$User = Read-Host -Prompt 'Input SamAccountName'\n\nif(Get-Aduser $User -Properties Mail | Select-Object mail){\n $Email = Get-Aduser $Users -Properties 'Mail' | select-Object mail\n}else{$Email = Read-host -Prompt 'Email not found. Please Enter Manually'}\n<\/code><\/pre>\n
Advertisement
my thought process was that if the mail property was set, that it would store it in $Email, else prompt the tech to add it manually. Any feedback would be much appreciated. Thanks!<\/p>","upvoteCount":5,"answerCount":4,"datePublished":"2020-04-16T21:25:29.000Z","author":{"@type":"Person","name":"scism","url":"https://community.spiceworks.com/u/scism"},"acceptedAnswer":{"@type":"Answer","text":"
\n- \n
Are you sure you are looking at the right attribute? There is ‘mail’ and there is ‘emailaddress’ in AD.<\/p>\n<\/li>\n
- \n
If you want to actual value, you have to expand the propery, there are 2 ways to do this:
\nLook at the output of each<\/p>\n<\/li>\n<\/ol>\n
Get-Aduser $User -Properties Mail\nGet-Aduser $User -Properties Mail | Select-Object -expandproperty mail\n(Get-Aduser $User -Properties Mail).mail\n<\/code><\/pre>\n\n- You want to query AD as little as possible, to it’s silly to query twice.<\/li>\n<\/ol>\n
$User = Read-Host \"Input SamAccountName\"\n\n$Email = (Get-Aduser $Users -Properties 'Mail').mail\n\nif(-not($email)){\n $Email = Read-host -Prompt 'Email not found. Please Enter Manually'\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-04-16T21:52:07.000Z","url":"https://community.spiceworks.com/t/get-aduser-mail-attribute/759366/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"Hi there,<\/p>\n
I have been working on a small script for active directory users. I have most of it written, but one stipulation was to pull the mail attribute and assign it to a variable, and if it doesn’t exist, create a prompt in powershell to add it manually. I added an If/else statement but regardless if the mail property is set or not, it never makes it to the else portion, and overall it just doesn’t work.<\/p>\n
$User = Read-Host -Prompt 'Input SamAccountName'\n\nif(Get-Aduser $User -Properties Mail | Select-Object mail){\n $Email = Get-Aduser $Users -Properties 'Mail' | select-Object mail\n}else{$Email = Read-host -Prompt 'Email not found. Please Enter Manually'}\n<\/code><\/pre>\nmy thought process was that if the mail property was set, that it would store it in $Email, else prompt the tech to add it manually. Any feedback would be much appreciated. Thanks!<\/p>","upvoteCount":5,"datePublished":"2020-04-16T21:25:29.000Z","url":"https://community.spiceworks.com/t/get-aduser-mail-attribute/759366/1","author":{"@type":"Person","name":"scism","url":"https://community.spiceworks.com/u/scism"}},{"@type":"Answer","text":"
Welcome!<\/p>\n
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n