'Multiple same output triggering
I wrote a script to match the two values. One is from my input and another one is i had taken from the query in domain domain server to get the certain values. i need match the my input values and queried output values. But if run the script the means it gives multiple same outputs. please find the attached output screenshots and my scripts.
$Inputui = Read-Host "Enter" –AsSecureString
$results = Get-ADUser -Filter {Enabled -eq $false} | FT samAccountName
foreach ($result in $results)
{
if($Inputui -match $result)
{
Write-output "ID Available"
}
else {
Write-output "ID Not Available"
}
}
Solution 1:[1]
So it seems you want to test if a SamAccountname given through Read-Host can be found in the domain, correct?
Then:
do not use
Format-*cmdlets if you need to process the result further, because these cmdlets are for display purposes only
your code now tries to compare a string against a Microsoft.PowerShell.Commands.Internal.Format objectremove
-AsSecureString. You only need that to convert a given string (password) into a SecureStringthe
-Filterparameter should actually be a string, not a scriptblock
$Inputui = Read-Host "Enter user SamAccountName"
Get-ADUser -Filter "Enabled -eq '$false'" | ForEach-Object {
# using -match results in a partial match, just like
# $Inputui -like "*$($_.SamAccountName)*" would do
# if you want an exact match, use the -eq operator instead
if ($Inputui -match $_.SamAccountName) {
Write-Host "ID Available"
}
else {
Write-Host "ID Not Available"
}
}
Solution 2:[2]
Because the match is failed and entering into else block. Try below code
$Inputui = Read-Host "Enter" –AsSecureString
Get-AdUser -filter -Filter {Enabled -eq $false} | where-object {$_ -contains "$Inputui"} | FT samAccountName
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Narayana Lvsl |

