简体   繁体   English

带有变量的Get-ADUser过滤器失败

[英]Get-ADUser filter with variable fails

I have a sql query that returns the Employee's supervisor email address and stores it in a variable. 我有一个SQL查询,该查询返回员工的主管电子邮件地址并将其存储在变量中。 When I use that variable in the Get-ADUser query it returns nothing. 当我在Get-ADUser查询中使用该变量时,它不返回任何内容。

I've tried supplying the actual data in the Get-ADUser query and it works I've tried convert the variable returned from the SQL query to string and that doesn't return any results either. 我尝试在Get-ADUser查询中提供实际数据,并且它尝试将SQL查询返回的变量转换为字符串,并且也不返回任何结果。

$SupvisorID="CFGFBT0000K0"
$QueryFmt3 = @"
Select  eepAddressEmail
 from empcomp
 join emppers on eeceeid = eepeeid
 join Location on EecMailstop = LocCode
 join company on eeccoid = cmpcoid
 join jobcode on jbcjobcode = eecjobcode
 join OrgLevel on EecOrgLvl2 = OrgCode
 Where eepEEID = '$SupvisorID'
"@

$SVEmail=Invoke-Sqlcmd   -ServerInstance $server -Database  $dbname -Query $QueryFmt3
$SVEmail

## This will not return any results
get-aduser -Filter "EmailAddress -like '$SVEmail'" | Select Name

#However, if I insert the actual value of the $SVEmail variable I get results
get-aduser -Filter "EmailAddress -eq 'RPolley@cc-md.org'" | Select Name

My apologies if my post is not formatted correctly. 抱歉,如果我的帖子格式不正确。 I've never reached out for help online before. 我以前从未在线寻求帮助。 My dilemma is how to get the Get-ADUser string to recognize the data contained in the variable returned by the SQL query. 我的难题是如何获取Get-ADUser字符串以识别SQL查询返回的变量中包含的数据。

Invoke-Sqlcmd returns DataRow object (or array of DataRows, if the query returns more than one row). Invoke-Sqlcmd返回DataRow对象(如果查询返回多行,则返回DataRows数组)。 So you have to specify which field of returned row you want to use. 因此,您必须指定要使用返回行的哪个字段。 Try $SVEmail.eepAddressEmail 试试$SVEmail.eepAddressEmail

Get the property from the object. 从对象获取属性。 Since you're not using wildcards, you might as well use "-eq". 由于您没有使用通配符,因此不妨使用“ -eq”。

$email = $SVEmail.eepAddressEmail
get-aduser -Filter "EmailAddress -eq '$email'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM