首页 > 解决方案 > Powershell - Active Directory 不返回属性值

问题描述

我有一个 Powershell 脚本,它在登录时创建 Outlook 电子邮件地址(感谢 Joel Coelho)。除了检索手机号码外,一切正常:

$signature = $rawTemplate -replace $mobilePlaceHolder,$ADUser.mobile
$rawTemplate = $signature

这总是返回一个空白, $mobilePlaceHolder 已被替换,但什么也没有。它正在被替换,这由一行确认:

$logline = 'Replaced ' + $mobilePlaceHolder + ' with ' + $ADUser.mobile

这表明它已被任何东西取代。移动属性是正确的,它在AD的属性下。当我做一个 get-aduser 时,它也会出现。其他参数都有效,但我不知道为什么这个参数不起作用。非常感谢帮助。

完整代码是:-

$logfile = "D:\CreateSignaturelog.log"
# Gets the path to the user appdata folder
date > $logfile
$AppData = (Get-Item env:appdata).value
$logline = 'My appdata folder is at ' + $AppData
$logline >> $logfile
# This is the default signature folder for Outlook
$localSignatureFolder = $AppData+'\Microsoft\Signatures'
# This is a shared folder on your network where the signature template should be
$templateFilePath = "\\nz-sp-ap1\spgeneral\Standard Documents\templates"
$logline = 'Template file path is at ' + $templateFilePath
$logline >> $logfile

# Get the current logged in username
$userName = $env:username
$logline = 'My user name is ' + $username
$logline >> $logfile

# The following 5 lines will query AD and get an ADUser object with all information
$filter = "(&(objectCategory=User)(samAccountName=$userName))"
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.Filter = $filter
$ADUserPath = $searcher.FindOne()
$ADUser = $ADUserPath.GetDirectoryEntry()

$namePlaceHolder = "DISPLAY_NAME"
$FullName = $ADUser.DisplayName -replace " ", "&nbsp"
$rolePlaceHolder = "ROLE"
$emailPlaceHolder = "MAILTO"
$phonePlaceHolder = "PHONE1"
$mobilePlaceHolder = "PHONE2"
$addressPlaceHolder = "ADDRESS"
$entiremobilerow = '<tr><td colspan="2" class="style2">Mobile: PHONE2</td></tr>'
$rawTemplate = get-content $templateFilePath"\Emailtemplate.html"

$signature = $rawTemplate -replace $namePlaceHolder,$FullName
$rawTemplate = $signature
$logline = 'Replaced ' + $namePlaceHolder + ' with ' + $FullName
$logline >> $logfile

$signature = $rawTemplate -replace $rolePlaceHolder,$ADUser.description
$rawTemplate = $signature
$logline = 'Replaced ' + $rolePlaceHolder + ' with ' + $ADUser.description
$logline >> $logfile

$signature = $rawTemplate -replace $emailPlaceHolder,$ADUser.mail
$rawTemplate = $signature
$logline = 'Replaced ' + $emailPlaceHolder + ' with ' + $ADUser.mail
$logline >> $logfile

$signature = $rawTemplate -replace $phonePlaceHolder,$ADUser.telephonenumber
$rawTemplate = $signature
$logline = 'Replaced ' + $phonePlaceHolder + ' with ' + $ADUser.telephonenumber
$logline >> $logfile

If ($ADUser.mobile = ""){
$signature = $rawTemplate -replace $entiremobilerow,""
$rawTemplate = $signature      
$logline = 'Replaced ' + $entiremobilerow + ' with nothing.'
$logline >> $logfile
}

$signature = $rawTemplate -replace $mobilePlaceHolder,$ADUser.MobilePhone
$rawTemplate = $signature
$logline = 'Replaced ' + $mobilePlaceHolder + ' with ' + $ADUser.MobilePhone
$logline >> $logfile

$logline = 'ExtensionAttribute1 is "+ ADUser.ExtensionAttribute1
$logline >> $logfile

$signature = $rawTemplate -replace $addressPlaceHolder,$ADUser.physicalDeliveryOfficeName
$rawTemplate = $signature
$logline = 'Replaced ' + $addressPlaceHolder + ' with ' + $ADUser.physicalDeliveryOfficeName
$logline >> $logfile

# Save it as <username>.htm
$fileName = $localSignatureFolder + "\" + $ADUser.DisplayName + ".htm"
$signature > $fileName
$logline = 'Saved ' + $filename
$logline >> $logfile

#Change to first name
$signature = $rawTemplate -replace $FullName,$ADUser.givenName
$rawTemplate = $signature

# Save it as <firstname>.html
$fileName = $localSignatureFolder + "\" + $ADUser.givenName + ".htm"
$signature > $fileName
$logline = 'Saved ' + $fileName
$logline >> $logfile

标签: powershell

解决方案


默认情况下,该Get-ADUsercmdlet 仅返回一些属性。mobile默认情况下不返回该属性。如果您需要特定的,您应该在-Properties参数中定义它们:

$user = Get-ADUser "myUserID" -Properties mobile,myOtherCustomAttribute

# This will return a valid value (if it is populated in AD)
$user.mobile

推荐阅读