I have a pretty standard AD account creation script that I created, but need to customize it to fit my needs and that's where I'm having trouble... I added a ability to create a txt or csv file based on searching a OU and made the search filter for accounts created within the last day that includes the username and password of the account(need password for other services that will be created, non-ldap), the passwords are randomly generate through a PowerShell Function.
But when ran, it only ever includes 1 username and no password, Or it will loop through and make 3-4 log files when I only want one. Any Suggestions on how to make 1 log file with all accounts created? I'm pretty sure it has to do with the location of the csv or txt file location within the script, still learning PowerShell :)
#EDIT 3/14/2018**gabriel-luci - The way you suggested worked. However, after talking with several people they asked if it could be a CSV. I have the CSV function working outside of this script. But I need it do do the same thing the other one was doing just in a csv format with headers of "Username,Password" Any Idea's? Thanks for your help! # Below is the part that should be writing to the CSV.. When I separate this out into another PS script, it works. It should loop through the CSV and for every username write it with its password. But when I put it in with the account creation script, it doesn't write the username or password and I check my powershell script and it's not commented out, I think the Stackoverflow thing is formatting what i posted weirdly. Or is there a better way to do what I'm attempting?
###
$CSV | ForEach-Object {
$FIRST = $_.FIRST_NAME
$LAST = $_.LAST_NAME
$USER = "$FIRST" + "$LAST"
$csvadd = $USER + "," + $Student_Password
$csvadd | Out-File $filename -Append -Encoding ASCII
###
Import-Module ActiveDirectory
#Imports CSV File and Starts Loop
$CSV = Import-CSV "C:\ActiveDirectory\NewStudent.csv" | % {
#Create Variables
#
$First_Name = $_.FIRST_NAME
$Last_Name = $_.LAST_NAME
$Username = "$First_Name" + "$Last_Name"
$Student_Number = $_.Student_Number
#Shouldn'tBeNeeded-$Lunch_ID = $_.Lunch_ID
$Grade_Level = $_.Grade_Level
$School_Name = $_.School_Name
$School_Abbr = $_.School_Abbr
$Graduation_Year = $_.graduation_year
$EmployeeID = $Student_Number
$StudentPassword = New-Password
$SecurePassword = (ConvertTo-SecureString -AsPlainText $StudentPassword -
Force)
$FullName = "$First_Name" + " "+ "$Last_Name"
$UserPN = $Username + "@home.virtual.local"
$Email = $Username + "@home.virtual.local"
#NotNeeded-$LegacyEmail = $_.WEB_ID + "@home.virtual.local"
$Description = "Grade " + $Grade_Level
$Path = "OU=ScriptTesting,OU=Test Accounts,OU=People,DC=Home,DC=Local"
#$Path = "OU=" + $Grade_Level + ",OU=" + $School_Abbr +
",OU=ScriptTesting,OU=Test Accounts,OU=People,DC=Home,DC=Local"
#NotNeeded-$GradeLevelGroup = "Stu_" + $Grade_Level + "_Grade"
#$HomeFolder = "\\staff-files\" + "$Graduation_Year\" + $Username
#
$curdate = Get-Date -Format o | foreach {$_ -replace ":", "."}
#curdate = Get-Date -format s | foreach {$_ -replace ":", "."}
$filename = "C:\Student_Accounts\logs\Usernames_Passwords-" + $curdate +
".csv"
$CSVHeader = "Username,Password"
$CSVHeader | Out-File $filename -Encoding ASCII
##
$ADUser = Get-ADUser -LDAPFilter "(sAMAccountName=$Username)"
#
If ($ADUser -eq $Null)
{
New-ADUser $FullName -GivenName $First_Name -Surname $Last_Name `
-SamAccountName $Username -UserPrincipalName $UserPN `
-AccountPassword (ConvertTo-SecureString $StudentPassword -AsPlainText -
force) `
-Office $School_Name `
-Title $Graduation_Year `
-EmployeeID $Student_Number `
-DisplayName $FullName `
-Department $Grade_Level `
-Description $Description `
-EmailAddress $Email `
-Path $Path `
-PassThru | Enable-ADAccount
#
Sets Home Directory
New-Item -Name $Username -ItemType -Path $HomeFolder | Out-Null
Set-ADUser $Username -HomeDirectory $HomeFolder -HomeDrive S:
Sets ACL List
$ACL= Get-ACL $HomeFolder
$ACL.SetAccessRuleProtection($true, $True)
#Add User to AD Grade Level Group
#Add-ADGroupMember -Identity $GradeLevelGroup -Members $Username
#Small Pause to give time for AD Account to be created before resetting
password
Start-Sleep -s 30
#Reset Password
Set-ADAccountPassword -Identity $Username -NewPassword $SecurePassword -
Reset | Set-ADuser -ChangePasswordAtLogon $False -PasswordNeverExpires
$True -CannotChangePassword $True
Write-Host $StudentPassword
#Write-Host "Account Created" $Username
#Write-Host "Password for" $Username "Is" $StudentPassword
#Return $Username
#Return $StudentPassword
$CSV | ForEach-Object {
$Username = "$First_Name" + "$Last_Name"
$csvadd = $Username + "," + $Student_Password
$csvadd | Out-File $filename -Append -Encoding ASCII
}
}
}
$Path = "OU=$Grade_Level,OU=$School_Abbr,OU=User,DC=home,DC=virtual,DC=local"or$UserPN = "[email protected]"Doing this saves using excessive+everywhere. The only gotcha is$_.Xwhere you would need to wrap it in$()eg:$LegacyEmail = "$($_.WEB_ID)@home.virtual.local"