2016-11-07 199 views
2

我在PowerShell中有此脚本。我想为每个用户从文件导入设置随机密码,我希望在弹出式cmd中的所有注释我想保存在其他txt文件中。我怎样才能做到这一点?生成随机密码

# 
# Description: Enable accounts, reset passwords and set change password option at first logon. 
# 
Import-Module ActiveDirectory 
# Set default password "XXX" 
$password = ConvertTo-SecureString -AsPlainText “XXX” -Force 
# get account list from UserList.txt 
# 1 user per line 
$users = Get-Content -Path 'G:\Shares\XXX\ResetPassword\UserList.txt' 
ForEach ($user in $users) 
{ 
# Set default password for account 
Get-ADUser $user | Set-ADAccountPassword -NewPassword $password -Reset 
# Set option to change password at first logon 
Get-ADUser $user | Set-AdUser -ChangePasswordAtLogon $true 
# Enable account 
Enable-ADAccount -Identity $user 
Write-Host “Password change for: $user” 
} 
Write-Host “New password for all users: XXX” 
# ————- End ———– 
Read-Host -Prompt "Click enter to quit" 
+0

你给所有用户提供相同的密码? – Nkosi

+0

是的,但现在我想给每个用户的随机密码 –

回答

6

您可以使用静态GeneratePassword方法来生成密码:

Add-Type -AssemblyName System.Web 
[System.Web.Security.Membership]::GeneratePassword(10, 3) 

编辑:

你有你的脚本改为:

# 
# Description: Enable accounts, reset passwords and set change password option at first logon. 
# 
Import-Module ActiveDirectory 
Add-Type -AssemblyName System.Web 
$unsecuredPwd = [System.Web.Security.Membership]::GeneratePassword(10, 3) 
# Set default password "XXX" 
$password = ConvertTo-SecureString -AsPlainText $unsecuredPwd -Force 
# get account list from UserList.txt 
# 1 user per line 
$users = Get-Content -Path 'G:\Shares\XXX\ResetPassword\UserList.txt' 
ForEach ($user in $users) 
{ 
# Set default password for account 
Get-ADUser $user | Set-ADAccountPassword -NewPassword $password -Reset 
# Set option to change password at first logon 
Get-ADUser $user | Set-AdUser -ChangePasswordAtLogon $true 
# Enable account 
Enable-ADAccount -Identity $user 
Write-Host "Password change for: $user" 
} 
Write-Host "New password for all users: $unsecuredPwd" 
# ————- End ———– 
Read-Host -Prompt "Click enter to quit" 
+0

,我需要设置此命令? –

+0

'$ password = ConvertTo-SecureString -AsPlainText([System.Web.Security.Membership] :: GeneratePassword(10,3))-Force' –

+0

请参阅我编辑的答案。 –