I'm attempting to create a script, to create AD Accounts. One portion using an IF statement is giving me troubles. I've tried so many variations using ForeEach and ForeEach-Object. But I get nothing, not even errors.
My current script is....
$UList=(Import-Csv "\\SERVER\SHARE\Folder\File.csv")
ForEach-Object {
If ($_Department -eq "APD"){
Write-Host -b White -f Red "This is if Statement for Dewy.Seemore"
Add-QADGroupMember -Identity `$A-DWN -Member Dewy.Seemore
}
Else {
Write-Host "$_.SamAccountName is not in Group"
# $UList
In the above script, There are no errors returned. From what I've tried, If I remove the remark from the near last line (# $UList) I do get a list of the headings from my csv file, and the values for the headings. I have two entries in the csv, one is True and the other is False for matching $_Department. If I place the $UList up in the "IF" section, I get nothing. So this tells me that my expression never returns true that $_Department equals APD.
My CSV file, has two entries. One row have APD as the value for Department, and the other row has a different value, not APD. Can anyone tell me what is wrong with my IF statement?
In my 5th and 6th lines, I would typically not have a Write-Host, and the actual name of an account would be replaced with $_.SamAccountName
I've tried ForeEach ( $UL in UList ) {, and tried $UL_.SamAccountName, as well as $UL_.Department....
thank you, any help would be much appreciated.
DD
The reason you are having the results you are the following reasons:
This ...
$UList = (Import-Csv "\\SERVER\SHARE\Folder\File.csv")
... is just reading a file and storing that content that you are not using in your script for anything.
This...
$_Department
... is not populated from anything in the code you show. I am going to assume, this is in that text file, and if it is that is not the way to call it, but I am not sure since you don't show a sample of what is in the text file.
... is using the PowerShell line continuation / termination character unnecessarily.
($_Department -eq "APD")
... is not the way to call a variable from a file.
The reason, you are getting results when you uncomment this...
... is because you populated it at the top of the script, but again, you are not using. So, please show a sanitized snippet of the text file, for better clarity.
This sounds like you are new to PowerShell, and or that and ADDS scripting. I see you installed the QAD module, though it's a nice module, it is really not needed to do this. Just install the RSAT tools on your workstation...
Remote Server Administration Tools for:Windows 10 - https://www.microsoft.com/en-us/download/details.aspx?id=45520Windows 8 - https://www.microsoft.com/en-us/download/details.aspx?id=28972Windows 7 - https://www.microsoft.com/en-us/download/details.aspx?id=7887
or use PSRemoting (explicit or implicit) and use the cmdlets from the DC.
If that is the case, It's critical that if you are new, that you first spend the time to ramp up to prevent a lot of unnecessary frustration and confusion your are going to encounter.
Do a search on Microsoft Virtual Academy on PowerShell and YouTube for no cost video training. https://mva.microsoft.com/search/SearchResults.aspx#!q=powershell&lang=1033https://www.youtube.com/results?search_query=beginning+powershell
Use all the free eBooks on this site.https://leanpub.com/u/devopscollective
Read the full help file on any cmdlet you are trying to use, practice with the examples, then read the helpfile again, then pick up a few good books, like, 'PowerShell in a Month of Lunches'.https://www.amazon.com/Learn-Windows-PowerShell-Month-Lunches/dp/1617294160/ref=sr_1_1?ie=UTF8&qid=1541490524&sr=8-1&keywords=learn+powershell+in+a+month+of+lunches+3rd+edition
There are lots of free PowerShell eBooks on Microsoft's websites.
See also: The PowerShell Survival Guidehttps://social.technet.microsoft.com/wiki/contents/articles/183.windows-powershell-survival-guide.aspx
As for your script, there is no reason tp do this from scratch. You can let Windows Server write this script for you using the ADAC then you can tweak as needed.
Step-By-Step: Utilizing PowerShell History Viewer in Windows Server 2012 R2https://blogs.technet.microsoft.com/canitpro/2015/03/04/step-by-step-utilizing-powershell-history-viewer-in-windows-server-2012-r2
Learning PowerShell with Active Directory Administrative Center (PowerShell History Viewer)https://sid-500.com/2017/10/10/learning-powershell-with-active-directory-administrative-center-powershell-history-viewer
Or just download on from the Microsoft PowerShell Gallery.
PowerShell: Create Active Directory Users Based On Excel InputThis script will create users in Active Directory based on the settings in the input file (see the Excel / CSV file below this script for an example of the input file used). These settings can, of course, be changed or extended (check this Microsoft TechNet Link to get an overhttps://gallery.technet.microsoft.com/scriptcenter/PowerShell-Create-Active-7e6a3978
Bulk Create Active Directory Users v1.5This utility requires the Active Directory PowerShell module be installed, which is a part of the Remote Server Administration Tools (RSAT) https://gallery.technet.microsoft.com/scriptcenter/Create-AD-Account-from-CSV-09ee9d39
You don't need write host to display to the screen, that is the default. You only need it for text coloring or other specific formatted needs. Use only single quotes, unless you are need to expand variables or dealing with special quoting needs.
In production scripts, never use aliases. They are just hard to remember, maintain, and non-descriptive. They are OK for throw away code, or interactive stuff.
Again, look at the above ADAC would produce for you (that you can tweak) and or the above samples, but all-in-all, roughly, your script should look something like this...
$UList = @'Department,SamAccountName,GroupNameAPD,Dewy.Seemore,A-DWN123,username,group'@ | ConvertFrom-Csv
ForEach($UL in $UList){ If ($_.Department -eq "APD") { Write-Host "This is if Statement for $($_.SamAccountName)" -BackgroundColor White -ForegroundColor Red Add-ADGroupMember -Identity $_.GroupName -Member $_.SamAccountName } Else { "$($_.SamAccountName) is not in List" }}
Thank you, I appreciate all the time you put into helping me understand what I'm doing and doing wrong...
First off.... Here is the output of my csv file. I have two entries in the file. one has the dept. match APD and the other does not. I only list a portion of one entry.
Property Value----------------- -----------GivenName : DewyInitials : ISurname : SeemoreName : Dewy Seemore (xxx)DisplayName : Dewy Seemore (xxx)SamAccountName : Dewy.SeemoreRemoteRoutingAddress : [email protected]Last4SSN : 1234Office : OfficePhone : Title : HelpDeskEmployeeID : 1234567Manager : Rob BobSupervisorPhone : (123) 456-7890CreatedBy : DDEffectiveDate : SubmissionDate : 10/29/18AccountExpirationDate : EmployeeNumber : Division : xxxDepartment : APDEmployeeType : ContractorsInstance : ~Template1 User
The CSV file contains a list of AD User properties and values.
As for Quest, I really do prefer it over the MS AD module. QAD modules are infinitely easier to learn and use. When it comes to adding a single user to multiple groups, I've not been able to figure out how to make it work with MS modules. But the QAD module has been very easy for me. If MS was easier, I'd use it more. Maybe it's just forcing myself to use it more to learn it. It just seems pointless when I have QAD and it so much easier.
I've used the $_Department and or $_.Department to assign a variable. Not in an IF statement. so not sure what the difference is.
On the `$A-DWN, I'm not using it as line continuation. The group name is $A-DWN, and so I'm escaping the $ so that it is not read as a variable, rather its part of the name.
I should probably mention, these few lines of code are just a snipit of the whole script. I just pulled these line out when my script failed. Without these lines, my script runs fine. So I pulled out the lines and am trying to get them to work as is, or isolated.
As for the links and reference, thank you very much. I really appreciate the videos links and the ebooks. I'll may good use of them.
Thanks