PowerShell帐户创建脚本

时间:2018-03-13 11:56:54

标签: powershell automation active-directory user-accounts

我创建了一个非常标准的AD帐户创建脚本,但需要对其进行自定义以满足我的需求,这就是我遇到问题的地方......我添加了基于搜索创建txt或csv文件的功能一个OU并对在最后一天创建的帐户进行了搜索过滤,其中包括帐户的用户名和密码(需要创建其他服务的密码,非ldap),密码是通过PowerShell函数随机生成的。

但是当它运行时,它只包含1个用户名而没有密码,或者当我只想要一个时,它会循环并生成3-4个日志文件。有关如何创建包含所有帐户的1个日志文件的任何建议?我很确定它与脚本中csv或txt文件位置的位置有关,仍在学习PowerShell:)

#EDIT 3/14/2018

** gabriel-luci - 你的建议方式。然而,在与几个人交谈之后,他们询问它是否可以是CSV。我在这个脚本之外使用CSV功能。但我需要它做同样的事情另一个人只是用csv格式做标题为“用户名,密码”任何想法? 谢谢你的帮助! # 下面是应该写入CSV的部分。当我将其分成另一个PS脚本时,它可以工作。它应该遍历CSV并且每个用户名都用密码写入。但是,当我使用帐户创建脚本时,它不会写入用户名或密码,我检查我的powershell脚本并且它没有被注释掉,我认为Stackoverflow的事情正在格式化我发布的奇怪内容。或者有更好的方法来做我正在尝试的事情吗?

###
$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
}
}
}

1 个答案:

答案 0 :(得分:1)

您的日志文件名包括时间,包括秒和毫秒,因此,因为您在循环的每次迭代中重新生成文件名,所以每次都会更改日志文件名,并且您将获得多个日志文件。

您可以通过在进入循环之前确定日志文件的名称来解决此问题,以便循环的每次迭代都使用相同的文件。

将这些行移到循环外的脚本顶部:

$curdate = Get-Date -Format o | foreach {$_ -replace ":", "."}
$filename = "C:\ActiveDirectory\Logs\AccountCreation\AccountsCreated-" + $curdate + ".txt"

输出到文件的行也会覆盖文件(默认情况下Out-File会覆盖):

Write-Host $filename
Write-Output $log | Out-File $filename

实际上有三种方法可以附加到文件中。随便挑选。所有3个完全相同:

$log | Out-File $filename -Append
$log | Add-Content $filename
$log >> $filename

您不需要使用Write-HostWrite-Output

编辑:这是我在谈论删除循环的地方的部分:

#$CSV | ForEach-Object { ### You don't need this ForEach
#Just use the next 3 lines
$Username = "$First_Name" + "$Last_Name"
$csvadd = $Username + "," + $Student_Password
$csvadd | Out-File $filename -Append -Encoding ASCII
#}