使用“ Get-Aduser”从广告中的用户帐户中检索一个空属性。返回null-但不是零吗?
我遇到麻烦,试图使用PowerShell脚本在我们的域控制器上自动化任务。
简而言之:我编写了一个PowerShell脚本,该脚本在Active Directory中在用户帐户中检索特定属性的内容,并取决于某些比较的结果。它使用“ Get-Aduser”和“ Select-Object” CMDLET检索值,然后使用“ Switch”语句来评估和采取一些比较。但是,如果没有“查询”检索值,则开关语句将无法完全工作。
首先,我初始化了一个名为“ $ currentAdvalue”的变量,并将其分配给Get-Aduser命令的输出。
$CurrentADValue = Get-ADUser "first.last" -Properties * | select-object -ExpandProperty altSecurityIdentities
然后,开关语句比较“ $ CurrentAdvalue”变量的内容,并根据比较结果执行操作。
Switch ($CurrentADValue) {
$null {Write-OutPut "Doing X." ; Break}
{$CurrentADValue -ne $Expected_Value} {Write-OutPut "Doing X."}
$Expected_Value {Write-OutPut "Doing Y."}
}
我的意图是,如果字段为空($ null)或意外的值,我想做X。如果是预期的值,我想做Y。不要大。
我对一个我知道的用户进行了脚本,该用户有一个空的“ altsecurityidenties”字段 - 瞧瞧,什么也没发生。 我认为$ currentAdvalue包含除空之外的其他内容,因此在变量初始化之后,我添加了一个快速比较,以确认它不是(或没有)null:
PS C:\Users\Me> @script.ps1
$CurrentADValue = Get-ADUser "first.last" -Properties * | select-object -ExpandProperty altSecurityIdentities
$CurrentADValue -eq $null
True
PS C:\Users\Me>
这使我陷入困境,因为我的开关语句应评估为<<<代码> True 在第一个比较中,如果它确实是$ null
,但这并没有发生! 因此,我拉起了powershell iSe,并写了一个小脚本以确认我并不疯狂:
PS C:\Windows\system32> $CurrentADValue = Get-ADUser "first.last" -Properties * | select-object -ExpandProperty altSecurityIdentities
Write-OutPut "CurrentADValue is:""$CurrentADValue""`n"
Write-Output '$CurrentADValue -eq $null ='($CurrentADValue -eq $null)
Write-Output "Switch output (Below):"
Switch ($CurrentADValue) {
$null {Write-OutPut "`nCurrentADValue is Null 1"}
{$CurrentADValue -eq $null} {Write-OutPut "`nCurrentADValue is Null 2"}
{$CurrentADValue -eq ""} {Write-Output "`nThere is nothing in CurrentADValue"}
{$CurrentADValue -ne $null} {Write-OutPut "`nCurrentADValue is NOT Null"}
}
输出是:
CurrentADValue is:""
$CurrentADValue -eq $null =
True
Switch output (Below):
PS C:\Windows\system32>
经过一些随机模糊之后,我将其添加到第四行中:if($ currentAdvalue -eq $ null) ){$ currentAdvalue = $ null}
再次运行:
PS C:\Windows\system32> $CurrentADValue = Get-ADUser "first.last" -Properties * | select-object -ExpandProperty altSecurityIdentities
Write-OutPut "CurrentADValue is:""$CurrentADValue""`n"
Write-Output '$CurrentADValue -eq $null ='($CurrentADValue -eq $null)
If ($CurrentADValue -eq $null) {$CurrentADValue = $null}
Write-Output "Switch output (Below):"
Switch ($CurrentADValue) {
$null {Write-OutPut "`nCurrentADValue is Null 1"}
{$CurrentADValue -eq $null} {Write-OutPut "`nCurrentADValue is Null 2"}
{$CurrentADValue -eq ""} {Write-Output "`nThere is nothing in CurrentADValue"}
{$CurrentADValue -ne $null} {Write-OutPut "`nCurrentADValue is NOT Null"}
}
CurrentADValue is:""
$CurrentADValue -eq $null =
True
Switch output (Below):
CurrentADValue is Null 1
CurrentADValue is Null 2
PS C:\Windows\system32>
那时,我已经失去了理智。
有人可以向我解释一下吗?除非是手动分配$ null
,否则在一个比较语句中,命令的零输出(在一个比较语句中等于null)如何等于null? 我很茫然,非常感谢一些澄清。
I'm running into trouble trying to automate a task on our Domain Controllers using a PowerShell script.
To make a long story short: I wrote a PowerShell script that retrieves the contents of specific attribute in a users account in Active Directory, and performs an action depending on result of some comparisons. It uses the "Get-ADUser" and "Select-Object" cmdlet to retrieve a value, then a "Switch" statement to evaluate and act on some comparisons. However, if no value is retrieved by the 'query', then the switch statement fails to work entirely.
First, I initialize a variable named "$CurrentADValue" and assign it the output of the the Get-ADUser command.
$CurrentADValue = Get-ADUser "first.last" -Properties * | select-object -ExpandProperty altSecurityIdentities
A switch statement then compares the contents of the "$CurrentADValue" variable, and performs an action depending on the results of the comparisons.
Switch ($CurrentADValue) {
$null {Write-OutPut "Doing X." ; Break}
{$CurrentADValue -ne $Expected_Value} {Write-OutPut "Doing X."}
$Expected_Value {Write-OutPut "Doing Y."}
}
My intention was if the field is empty ($null) or an unexpected value, I'd like to do X. If it's an expected value, I'd like to do Y. No biggie.
I ran the script against a user that I know has an empty "altSecurityIdentities" field - Lo and behold, nothing happened.
I figured the $CurrentADValue contained something other than null, so I added a quick comparison after the variable initialization to confirm it wasn't (or wasn't) null:
PS C:\Users\Me> @script.ps1
$CurrentADValue = Get-ADUser "first.last" -Properties * | select-object -ExpandProperty altSecurityIdentities
$CurrentADValue -eq $null
True
PS C:\Users\Me>
That threw me for a loop, as my Switch statement should evaluate to True
in it's first comparison if it really is $null
, but that just wasn't happening!
So, I pulled up PowerShell ISE and wrote a small script to confirm I wasn't crazy:
PS C:\Windows\system32> $CurrentADValue = Get-ADUser "first.last" -Properties * | select-object -ExpandProperty altSecurityIdentities
Write-OutPut "CurrentADValue is:""$CurrentADValue""`n"
Write-Output '$CurrentADValue -eq $null ='($CurrentADValue -eq $null)
Write-Output "Switch output (Below):"
Switch ($CurrentADValue) {
$null {Write-OutPut "`nCurrentADValue is Null 1"}
{$CurrentADValue -eq $null} {Write-OutPut "`nCurrentADValue is Null 2"}
{$CurrentADValue -eq ""} {Write-Output "`nThere is nothing in CurrentADValue"}
{$CurrentADValue -ne $null} {Write-OutPut "`nCurrentADValue is NOT Null"}
}
The output of which is:
CurrentADValue is:""
$CurrentADValue -eq $null =
True
Switch output (Below):
PS C:\Windows\system32>
After some random fuzzing, I added this to the 4th line: If ($CurrentADValue -eq $null) {$CurrentADValue = $null}
and ran it again:
PS C:\Windows\system32> $CurrentADValue = Get-ADUser "first.last" -Properties * | select-object -ExpandProperty altSecurityIdentities
Write-OutPut "CurrentADValue is:""$CurrentADValue""`n"
Write-Output '$CurrentADValue -eq $null ='($CurrentADValue -eq $null)
If ($CurrentADValue -eq $null) {$CurrentADValue = $null}
Write-Output "Switch output (Below):"
Switch ($CurrentADValue) {
$null {Write-OutPut "`nCurrentADValue is Null 1"}
{$CurrentADValue -eq $null} {Write-OutPut "`nCurrentADValue is Null 2"}
{$CurrentADValue -eq ""} {Write-Output "`nThere is nothing in CurrentADValue"}
{$CurrentADValue -ne $null} {Write-OutPut "`nCurrentADValue is NOT Null"}
}
CurrentADValue is:""
$CurrentADValue -eq $null =
True
Switch output (Below):
CurrentADValue is Null 1
CurrentADValue is Null 2
PS C:\Windows\system32>
By then, I'd lost my mind.
Can anyone explain this to me? How does the null output of the command, equal null in one comparison statement, but no in the switch statement, unless it's manually assigned $null
?
I'm at a loss, and would greatly appreciate some clarification.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论