Friends, Can you please help me with this?
I cannot figure out how to export the values of a Variable to .CVS
The Script imports a list of supposed SamAccountNames, butI I want to Export the ones that are not good.
The script works except for the Exporting.
foreach ($User in $ADUsers)
.
.
If (Get-ADUser -F {SamAccountName -eq $Input})
{
Do Something
}
Else
{
$Input | export-csv C:\FolderA\NotFound.csv -NoTypeInformation -append
}
The Resulting Exported Excel File looks like this
Length
2
3
8
11
7
14
But I want something like this because these are the values of $Input
You do not share how you populate the variable $ADUsers. If it’s not just a simple list of plain text strings you may specify the property of the objects you may have stored in $ADUsers.
Then you define the variable $User as your loop variable but you use $Input inside your loop.
And BTW: in the CSV data you share you have comma in one item. If that’s the case your real input data you should provide a delimiter for Import-Csv when there are really sAMAccountNames with commata in it.
Olaf, thank you for your reply. I have lots of studying to do because I have never used TRY or CATCH. But I think I get it. I tried your script, it works great.
But unfortunately, I am stuck with using the -Filter instead of -Identity. Because I did not tell you the whole story, I did not think it was necessary.
I only want to export if it does NOT pass any Five, (5) of these tests.
If it is NOT any of these: Good SamAccountname or CN, or Name, or Mail, or PesonGuid…
Here is some pseudo-code that will show you what logic I have in my script
By the way, my logic and script work alright, except for the Export Statement.
If (Get-ADUser -F {SamAccountName -eq $Input})
Do something
Else
If (Get-ADUser -F {Name -eq $Input })
Do something
Else
If (Get-ADUser -F {CN -eq $Input })
Do Something
Else
If (Get-ADUser -F {mail -eq $Input })
Do Something
Else
If (Get-ADUser -F {PersonGuid -eq $Input })
Do Something
Else
Export Right Here
You should urgently consider using another approach. Instead of querying every single name 5 times directly from your AD you should read ALL possible accounts from your AD in advance, save it in a variable and query only this list.
Olaf, Thank you, but “…read ALL possible accounts…” is just not the approach I need.
The idea is to see if each item in the Import CSV file can match an AD Record.
Each item in the Import CSV file might be:
SamAccountName
CN
Name
Email Address
PersonGuid
If an item in the Import CSV file is any of the above the script must Export to one .CSV.
If the Import CSV file input is none of the above I am trying to export to a totally different .CSV file to include the VALUE of the input. This would essentially say “This one did not find an AD Record!”
These managers request changes containing several hundred names. The managers are lazy. Sometimes the list of names are good, sometimes they are not good. Yes, yes, I know, in a perfect world they would supply good names. Then it is our job to make these changes, everyone else uses GUI. I am one of the first at my shop to try to develop some PowerShell tools. So if I am to use Powershell for hundreds and hundreds of changes, I MUST KNOW WHICH NAMES ON THE LIST ARE GOOD AND THEN GET THE SamAccountName. And I MUST KNOW WHICH ONES ARE BAD TOO.
Using your approach you query each individual element of your CSV against your AD and this 5 times. That is time consuming and stressful for your AD.
A faster and way less stressful way would be to query the proper OU from your AD and save the accounts with all needed properties in a variable/array. Now you can run your checks against this variable/array and export all CSV elements you cannot find in the variable/array to another CSV. Why would that not be the approach you need?
There’s no need to scream at me. I did understand this completely the first time.
Olaf, Thank you for your reply. I can see at a glance that your approach is compact, elegant and likey works very well. I can only dream about writing stuff like that. I truly mean to study it because I know from experience that when I understand something like that I will be able to do much more.
( I do need PersonGuid).
I have to get to work, I will study your words very carefully. Yesterday I worked over 12 hours.
By the way, I saved my co-workes hours and hours of woking witht the GUI with this simple thing:
But I had to get the good SamAccountNames from a list the boss gave me. There were hundreds. Some were good, some were bad. For some, all they had was an email address.