Quantcast
Channel: Tech Support Guy - Networking
Viewing all articles
Browse latest Browse all 4615

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

$
0
0
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:

Code:

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:

Code:

#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.

Viewing all articles
Browse latest Browse all 4615

Trending Articles