问题描述
我想检查是否useraccount已存在于系统中。
$马茨=读主机你叫什么名?
$ USER =获取-ADUser便有-Filter {sAMAccountName赋当量$马茨}
我不知道为什么,但$用户将总是返回null即使{sAMAccountName赋当量$马茨}被假设是正确的。
我是什么在这里失踪?
编辑:
这是什么缺少的:
$ USER =获取-ADUser便有筛选器sAMAccountName赋-eq'$马茨'
这一位我,当我第一次开始与ActiveDirectory中模块的工作,这是一个痛苦才能体现出来。
在 -Filter
参数在ActiveDirectory模块的cmdlet实际上是在寻找一个字符串。当你这样做 {sAMAccountName赋当量$马茨}
的值,它实际上是在寻找sAMAccountName赋当量
$马茨`
基本上,Powershell的解析参数,并把它的值转换成字符串,并且不会插变量。试试手之前建立字符串,它应该工作。
事情是这样的:
$马茨=读主机你叫什么名?
$过滤器=SAM帐户-eq$马茨
$ USER =获取-ADUser便有-Filter $过滤器
I'd like to check if a useraccount already exists in the system.
$SamAc = Read-Host 'What is your username?'
$User = Get-ADUser -Filter {sAMAccountName -eq "$SamAc"}
I'm not sure why but $User will always return null even if "{sAMAccountName -eq "$SamAc"}" is suppose to be true.
What am I missing here?
Edit:
This is what was missing:
$User = Get-ADUser -Filter "sAMAccountName -eq '$SamAc'"
This one bit me when I first started to work with the ActiveDirectory module, and it was a pain to figure out.
The -Filter
parameter for the ActiveDirectory module cmdlets is actually looking for a string. When you do {sAMAccountName -eq "$SamAc"}
as the value, it is actually looking for "sAMAccountName -eq ""
$SamAc"""`
Basically, Powershell parses the parameter and turns it's value into a string, and will not interpolate the variable. Try building the string before hand, and it should work.
Something like this:
$SamAc = Read-Host 'What is your username?'
$filter = "sAmAccountname -eq ""$SamAc"""
$User = Get-ADUser -Filter $filter
这篇关于GET-ADUser便有-Filter不会接受一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!