Home > Net >  Multiple same output triggering
Multiple same output triggering

Time:03-25

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"

      }
      }  

Outputs

CodePudding user response:

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

CodePudding user response:

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 object

  • remove -AsSecureString. You only need that to convert a given string (password) into a SecureString

  • the -Filter parameter 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"
    }
}
  • Related