Home > Back-end >  Need to get the data for multiple input data
Need to get the data for multiple input data

Time:07-21

I am having a script to fetch the data for given input. In this case I am trying to fetch the data from multiple input. Example I am trying to provide the input using text file. In that text file if I had provide only one input. output will displayed. While I am given multiple input data. it is not working.

Script

$users = Get-Content D:\host.txt

$Result = Import-Csv -Path "\\iblhpn1\wallpaper\AD_Disabled_Users_Report\Reports\aduserreport.csv" -Header "Name", "samAccountName", "Enabled" | 
        Where-Object {$_.samAccountName -match $users} | Select-Object "samAccountName", "Name", "Enabled"
      
if ( $Result -eq $null)
{
   Write-Output "'$users' USER ID not available in AD"
    }
  else
  {
   $Result | Out-GridView -Title 'AD User Status finder'
  }

Single input data
Single input data

Multiple input data
Multiple input data

CodePudding user response:

To check if a SamAccountName can be found in an array of SamAccountNames, use either $users -contains $_.SamAccountName or $_.SamAccountName -in $users in the Where-Object clause:

$users  = Get-Content D:\host.txt
$Result = Import-Csv -Path "\\iblhpn1\wallpaper\AD_Disabled_Users_Report\Reports\aduserreport.csv" -Header "Name", "samAccountName", "Enabled" | 
          Where-Object {$users -contains $_.SamAccountName} | Select-Object "samAccountName", "Name", "Enabled"
  
if (@($Result).Count -eq 0) {
   Write-Warning "Not one USER ID from the text file found in AD"
}
else {
   $Result | Out-GridView -Title 'AD User Status finder'
}

CodePudding user response:

Unfortunatly i have not the time right now to test this code, but i think you are missing sort of a loop to iterate through input file if there are multiple values. I would try something like this:

$users = Get-Content D:\host.txt

foreach ($user in $users)
{ $Result = Import-Csv -Path "\\iblhpn1\wallpaper\AD_Disabled_Users_Report\Reports\aduserreport.csv" -Header "Name", "samAccountName", "Enabled" | Where-Object {$_.samAccountName -match $user} | Select-Object "samAccountName", "Name", "Enabled"
      

if ( $Result -eq $null)
{
   Write-Output "'$users' USER ID not available in AD"
    }
  else
  {
   $Result | Out-GridView -Title 'AD User Status finder'
  } 


}

As mentioned earlier, i have not tried this code, but hopefully this may gets you on the right path to find the correct solution.

  • Related