列表中每个用户的Powershell Set-ADAccountPassword



我有一个生成随机密码的函数。我最终希望导入用户名列表,然后在设置密码时为每个名称运行该函数。

Function New-Password {
$Password = $null
Get-RandomCharacters
Scramble-String
Write-Host "$Password"
}
New-Password
$Password

如何将$Password与

合并
Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$Password" -Force)

如果需要,可以在函数中添加一个开关来转换它。您的完整函数将包含以下内容,并引用您的其他自定义函数:

function Get-RandomCharacters {...}
function Scramble-String {...}
function New-Password {
[CmdletBinding()]
param(
[switch]$converttoSS
)
$Password = $null
$password = Get-RandomCharacters -length 10 -characters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!"§$%&/()=?}][{@#*+'
#Write-Host ('{0}' -f $Password)
if ($converttoSS.IsPresent) {
$password = ConvertTo-SecureString -String $password -AsPlainText -Force
$password
}
else {
$password
}
}

其中New-Password -converttoSS应该返回System.Security.SecureString

但我认为有一个更简单的方法来做到这一点与System.Web组装。

Function New-Password {
[CmdletBinding()]
param(
[switch]$converttoSS
)
$password = $null
Add-Type -AssemblyName 'System.Web'
$password = [System.Web.Security.Membership]::GeneratePassword(20, 5)
if ($converttoSS.IsPresent) {
$newpass = ConvertTo-SecureString $password -AsPlainText -Force
Write-Warning ('The secure string is {0}' -f $password)
$output = $newpass
}
else {
Write-Warning ('The password is {0}' -f $password)
$output = $password
}
return $output
}

使用此测试数据:

id,first_name,last_name,email,manager
1,Kelcy,Dannel,kdannel0@phoca.cz,Kelcy Dannel
2,Vivia,O'Kynsillaghe,vokynsillaghe1@sun.com,Vivia O'Kynsillaghe
3,Valerie,Cartmell,vcartmell2@histats.com,Valerie Cartmell
4,Hilary,Checo,hcheco3@msu.edu,Hilary Checo
5,Sonya,Isacsson,sisacsson4@eepurl.com,Sonya Isacsson

您可以使用类似于下面的脚本。注意-请确保在生产环境中使用它之前进行测试。

Function New-Password {...}
$users = Import-Csv "C:DownloadsMOCK_DATA.csv"
Foreach ($u in $users) {
$newpass = New-Password -converttoSS
Get-ADUser -Identity $user.email | Set-ADAccountPassword -Reset -NewPassword $newpass
Write-Verbose $newpass
}

最新更新