Tech Support Guy banner

Powershell Script to pull Active Directory fields and make an Outlook signature

1680 Views 1 Reply 1 Participant Last post by  Valkrist
I've been trying to set the company I work for up with a standardized Outlook signature. I found a great Powershell script to take care of it. Here's a pastebin of the entire script. Well, I've set the script up and have it run by a logon batch file set to run in each user's Active Directory profile. I figured we were using one anyway, so might as well just add to it for this functionality. Well, the batch file loads the script every time without fail, and the script is capable of working, but doesn't always. While testing I restarted/logged in and out several times to make sure the script was doing it's job. I didn't change the logon batch file, and I didn't change the Powershell script. Well, sometimes the script works and Outlook applies an accurate new signature to my emails with information pulled from AD just like it should, and sometimes it doesn't. When it doesn't, the error I get is:

Exception calling "FindOne" with "0" arguement(s): "Logon Failure: unknown user name or     bad password."
At C:\Users\myUserName\Documents\Signature Deployment\SetOutlookSignature.ps1:105 char:32
+ $ADUserPath = $Searcher.FindOne <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorID : DotNetMethodException
Because of this one error, all of the AD fields that are queried later in the script return null values, which messes the outlook signature up if it was already set up correctly from a successful run of the script. The chunk of Powershell that this error is occurring in is:

#Get Active Directory information for current user
$UserName = $env:username
$Filter = "(&(objectCategory=person)(objectClass=user)(samAccountName=$UserName))"
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.Filter = $Filter
$ADUserPath = $Searcher.FindOne()
$ADUser = $ADUserPath.GetDirectoryEntry()
$ADDisplayName = $ADUser.DisplayName
$ADEmailAddress = $ADUser.mail
Now, I have no idea why this works sometimes, and other times it doesn't. I've tested extensively, and I can find no rhyme or reason to it. Any help would be greatly appreciated.
See less See more
Not open for further replies.
1 - 2 of 2 Posts
Anyone out there have any ideas? I usually don't bump my own threads, but this really has me stumped. Waiting five days to bump should be more than acceptable. =D
1 - 2 of 2 Posts
Not open for further replies.