Get a list of existing and enabled users from -csv file

Hi all,

i have a csv file With samaccountname Field and a list of users.

Would like to use this list to Query if users exist and if account is enabled.

I tried this,but its not working:

Import-Csvlistofusers.csv|ForEach-Object{

   

New-Object-TypeNamePSCustomObject-Property@{

        samaccountname

=$_.samaccountname

        exist

=[bool]($account=([adsisearcher]"(samaccountname=$($_.samaccountname))").findone())

        enabled

=[bool]($account.properties.useraccountcontrol[0]-band2)

    }

}

|Export-CsvOutput.csv-NoTypeInformation

Any ideas?

thanks!

May 28th, 2015 3:59am

Something like this:


$csv = Import-Csv C:\SamAccountName.csv

Foreach
($Account in $csv){
    $Sammy = $account.SamAccountName
        Get-Aduser $Sammy | Select-Object Enabled,Name,SamAccountname | Export-Csv -Path C:\Output.csv -Delimiter ";" -NoTypeInformation
}


Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 4:55am

Hi Ventrex,

thanks for Your reply,but it didnt work as expected.

Output file only listed out 1 user With status enabled and its not correct,since many more are enabled.

It also doesnt list non existing users.

CSV output should be like:

sammaccountname,enabled,exist

thanks!

May 28th, 2015 5:10am

What does not working mean?

Why does everyone fail to understand why the pipeline works and why using export or output statements inside of a foreach loop will not work well.

You were close. Try it this way:

Import-Csv listofusers.csv | 
    ForEach-Object{
        if($account=([adsisearcher]"(samaccountname=$($_.samaccountname)").findone()){
            $exists=$true
            $enabled=[bool]($account.properties.useraccountcontrol[0]-band 2)
        }else{
            $exists=$false
            $enabled=$false
        }
        [PSCustomObject]@{
            Samaccountname=$_.samaccountname
            Exists=$exists
            Enabled=$enabled
        }
    }
    | 
    Export-CsvOutput.csv-NoTypeInformation

The pipeline was the correct approach you just need to get the logic correct.

Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 5:16am

Hi,

still get error With Your Version:

Exception calling "FindOne" with "0" argument(s): "The (samaccountname= search filter is invalid."

At line:3 char:55

+         if($account=([adsisearcher]"(samaccountname=$($_.samaccountname)").findo ...

+                                                       ~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ArgumentException

Exception calling "FindOne" with "0" argument(s): "The (samaccountname= search filter is invalid."

At line:3 char:55

+         if($account=([adsisearcher]"(samaccountname=$($_.samaccountname)").findo ...

+                                                       ~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

May 28th, 2015 7:57am

You have an invalid CSV file.

try this test:

Import-Csvlistofusers.csv |
   
ForEach-Object{
        $_.samaccountname
        $a=(
[adsisearcher]"samaccountname=$($_.samaccountname)").FindOne()
    }

What dies this output?

Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 9:44am

sry,still no go:

Exception calling "FindOne" with "0" argument(s): "The (samaccountname=username search filter is invalid."
At line:3 char:43
+         ([adsisearcher]"(samaccountname=$($_.samaccountname)").FindOne()}| Expor ...
+                                           ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException
 
Exception calling "FindOne" with "0" argument(s): "The (samaccountname=username search filter is invalid."

Where username=user name from csv file

May 28th, 2015 12:56pm

that is not what I posted.  Doon't retype - copy and paste.  You will only type it wrong as you can see.
Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 1:32pm

i did a copy paste....
May 28th, 2015 1:50pm

This:  ([adsisearcher]"(samaccountname=$($_.samaccountname)").FindOne()}| Expor ...

Is not what is poted above.

This is what I posted:

Import-Csv listofusers.csv |
   
ForEach-Object{
        $_.samaccountname
        $a=(
[adsisearcher]"samaccountname=$($_.samaccountname)").FindOne()
    }

Note the $a and the lack of parens.

Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 2:32pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics