PowerShell:在表达式或语句中查询AD属性“意外令牌” [英] PowerShell: Querying AD attribute "unexpected token in expression or statement
问题描述
最近,我不得不更改powershell脚本以查询AD中其他属性中的SIP地址。但是我不确定如何编写它,因此可以查询Active Directory中的 msRTCSIP-PrimaryUserAddress。这是由于msRTCSIP-PrimaryUserAddress属性中的-引起的。当我将鼠标悬停在弯曲的线条上时,它表示表达式或语句中出现意外的标记
I recently had to change my powershell script to query a SIP address in a different attribute in AD. But I'm not sure how to write it so I can query the "msRTCSIP-PrimaryUserAddress" in Active Directory. This is due to the '-' in the msRTCSIP-PrimaryUserAddress attribute. When I hover my mouse over the squiggly line, it says "unexpected token in expression or statement"
我可以从AD查询其他内容,例如名称,mailnickname等。但是我特别需要这个,因为我们正在更改SIP地址。
I can query other things from AD suchs as name, mailnickname etc. But I need this one specifically because of the way we are changing our SIP address.
Function CheckSIP {
$loggedOnUser = (get-WmiObject win32_process -Filter "Name='explorer.exe'"|Select -First 1).GetOwner().User
$strFilter = "(&(objectCategory=User)(mailnickname=$loggedOnUser))"
$objDomain = New-Object
System.DirectoryServices.DirectoryEntry("LDAP://OU=Offices,dc=OurNetwork,dc=net")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = "name", "mail", "mailnickname", "msRTCSIP-PrimaryUserAddress"
foreach ($i in $colPropList){$null = $objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults){
$objItem = $objResult.Properties
[string]$UserName = $objItem.name
[string]$mail = $objItem.mail
[string]$mailnickname = $objItem.mailnickname
[string]$sipaddress = $objItem.msRTCSIP-PrimaryUserAddress
}
$theSIP = $sipaddress.Split("@")[0]
return $theSIP
}
$mySIPaddress = CheckSip
我知道 Get-ADUser命令,但并非我们所有的计算机都将在本机上本机安装此Commandlet。所以这就是为什么我要这样做。
I'm aware of the "Get-ADUser" command, but not all of our machines will have this commandlet natively on their machine. So that is why I'm doing it like this.
推荐答案
记住,连字符是运算符。尝试:
Remember, the hyphen is an operator. Try:
[string]$sipaddress = $objItem.'msRTCSIP-PrimaryUserAddress'
您还需要为搜索者指定属性:
You also need to specify the property for the searcher:
$colProplist = "name", "mail", "mailnickname","msRTCSIP-PrimaryUserAddress";
$objSearcher.PropertiesToLoad.AddRange($colProplist);
我也消除了不必要的循环。
I eliminated the unnecessary loop as well.
这篇关于PowerShell:在表达式或语句中查询AD属性“意外令牌”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!