Reading a CSV using Powershell Script

I am trying to get my script to do the following things:

- read a CSV script

- compare what is in the CSV script to AD

- Determine which of the accounts in the CSV are active in AD

- Put those results into a CSV in a designated folder

- Notify me when done via email (I know how to do this).

This is what I have:

Import-Csv -Path [path to csv]" |
Foreach-Object  { $PSItem | Get-Member } |
Get-ADUser -Filter * -Searchbase "DC=xxx,DC=xxx" -Properties ipPhone,EmailAddress |
Where-Object {$_.Enabled -eq $true} | 
Select-Object SAMAccountName,ipPhone,EmailAddress |
Export-Csv -Path [export location] -NoTypeInformation

Any assistance is really appreciated. I've removed sensitive information due to confidentiality reasons.

Also, it works to print out all the enabled users in AD, but it is not comparing the imported CSV. I think the error is in the foreach-object line, but I cannot figure out how to fix it.

Parents
No Data
Reply
  • You do not show or explain what is in your Csv, thus leaving us to guess.

     

    What do you mean by compare?

     

    You have not compare code in this post. What you do have is a read of a Csv file and a create of a new Csv file of information relative to what you passed in. That is not a compare, that is an extract.

     

    The cmdlet for compare is Compare-Object, yet, I don’t see how that will help you.

     

    And why are you doing this…

     

    $PSItem | Get-Member

     

    … it has nothing to do with your end goal, and will bring back unnecessary information.

     

    Get-Member is used for getting all properties and methods of an object, not anything specific about ADS or users. So, IMHO, get rid of that.

     

    You have specific bullets as to what you are trying to accomplish, but you have not addressed them point by point t make sure you are getting the results and final outcome that you are after.

     

    Ultimately, you really only have to end goals.

     

    1 - Determine which of the accounts in the CSV are active in AD

    2 - Put those results into a CSV in a designated folder

    3 - Notify me when done via email

     

    Ultimately, you really only have to end goals.

     

    So …

    1 – There are plenty of pre-built scripts / script samples that already do this on this MS powershellgallery.com that you can use as is and or tweak as needed.

    2 – The script mentioned above, most of them already do this as well.

    3 – Plenty of scripts that show how to do this as well.

     

    Powershell Script to export Active Directory users to CSV

    https://gallery.technet.microsoft.com/scriptcenter/Powershell-script-to-5edcdaea

     

    1 – As for searching for disabled accounts, there is a cmdlet for that.

    https://docs.microsoft.com/en-us/powershell/module/activedirectory/search-adaccount

    https://ss64.com/ps/search-adaccount.html

     

     

    As for …

     

    Also, it works to print out all the enabled users in AD, but it is not comparing the imported CSV.

     

    … that is because you are not writing and y compare code, and based on what you are after, it is really moot, as all you’d end up doing is this…

     

    Compare-Object -ReferenceObject 'Input.csv' -DifferenceObject 'Output.csv'

     

    See –

    # get function / cmdlet details

    (Get-Command -Name Compare-Object).Parameters

    Get-help -Name Compare-Object -Examples

    Get-help -Name Compare-Object -Full

    Get-help -Name Compare-Object -Online

     

    Yet, what you’d get back is that they are identical, because you are only going to get back what you passed in.

    You are trying to pass collections on the pipeline, without iterating thru them. That's really not a thing.

    Step back and rethink this, by taking your bullets one at a time.

Children