导出随机密码-导入和重置用户密码

时间:2018-10-30 12:10:47

标签: powershell import passwords export

是否可以导入随机生成的密码并将其设置为用户帐户?

以下仅带回以下内容:

  

无法将类型为“ System.String”的“密码”值转换为类型为“ System.Security.SecureString”。


$testing1 = read-host "Enter name"
$usertesting = Get-Aduser -Filter "DisplayName -eq '$testing1'" 

$fileNameDate = Get-Date
$fileNameDate = $fileNameDate.ToString('MM-dd-yyyy hh-mm-ss')

$Password = New-Object -TypeName PSObject
$Password | Add-Member -MemberType ScriptProperty -Name "Password" -Value { ("!@#$%^&*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".tochararray() | sort {Get-Random})[0..12] -join '' }
$Password | Export-csv -Path "C:\Output\$fileNameDate.txt" -notypeinformation

$passwordImport = import-csv "C:\Output\$fileNameDate.txt" | ForEach-Object { Set-ADAccountPassword -identity $testing1 -NewPassword $_.Password | ConvertTo-SecureString -AsPlainText -Force }

1 个答案:

答案 0 :(得分:0)

我很好奇为什么您将密码(以csv格式)保存并读回,然后再将其转换为安全字符串?

尝试一下(未试用)

## Q:\Test\2018\10\30\SO_53064023.ps1

$testing1 = read-host "Enter name"
$usertesting = Get-Aduser -Filter "DisplayName -eq '$testing1'" 

$filenamedate = "C:\Output\{0}.txt" -f (get-date -f 'MM-dd-yyyy hh-mm-ss')

$PassWord = -join ([char[]]"!@#$%^&*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |Get-Random -Count 13)
$Password | Export-csv -Path $fileNameDate -NoTypeInformation

$SecPW = ConvertTo-SecureString -String $PassWord -AsPlainText -Force

Set-ADAccountPassword -identity $testing1 -NewPassword $SecPW