预定义通过Powershell脚本创建的用户密码

时间:2017-12-01 08:05:51

标签: powershell powershell-v4.0

我目前正在制作一个Powershell脚本,要求我输入" FirstName"," LastName"和部门"在Active Directory中添加用户,将其添加到某些组,创建自己的文件夹等等。

唯一不起作用的是预先定义一个默认密码,每个用户在通过脚本创建时都应该获得该密码。我设法通过另一个要求您输入密码的字段使其工作。

但由于用户第一次登录后必须更改密码,我希望不要一直输入相同的密码。

提前致谢!

Clear Host Write-Host "---------------------------------------------------------------" $Vorname = Read-Host "FirstName"
Write-Host "---------------------------------------------------------------" $Nachname = Read-Host "LastName"
Write-Host "---------------------------------------------------------------" $Department = Read-Host "Department"

    #Variables
    $OU = "ou=Sales,dc=contoso,dc=local"
    $Domain = "contoso.local"
    $group = "Sales-Alle"
    $verteiler = "Sales-Mail"



    $DisplayName = $Vorname + " " + $Nachname
    $userLogonName = $Vorname + "." + $Nachname
    $SecurePass = ConvertTo-SecureString -String "Pa$$w0rd" -AsPlainText -Force

    New-ADUser -Name $Vorname `
    -SamAccountName $userLogonName `
    -GivenName $Vorname `
    -Surname $Nachname `
    -DisplayName $DisplayName `
    -Department $Department `
    -Path $OU `
    -AccountPassword $SecurePass `
    -Enabled $true `
    -UserPrincipalName ($userLogonName + "@" + $Domain) `
    -ChangePasswordAtLogon $true

#Variables $OU = "ou=Sales,dc=contoso,dc=local" $Domain = "contoso.local" $group = "Sales-Alle" $verteiler = "Sales-Mail" $DisplayName = $Vorname + " " + $Nachname $userLogonName = $Vorname + "." + $Nachname $SecurePass = ConvertTo-SecureString -String "Pa$$w0rd" -AsPlainText -Force New-ADUser -Name $Vorname ` -SamAccountName $userLogonName ` -GivenName $Vorname ` -Surname $Nachname ` -DisplayName $DisplayName ` -Department $Department ` -Path $OU ` -AccountPassword $SecurePass ` -Enabled $true ` -UserPrincipalName ($userLogonName + "@" + $Domain) ` -ChangePasswordAtLogon $true

1 个答案:

答案 0 :(得分:0)

您可以使用$securepass = Read-Host -AsSecureString "Enter Password:"来询问密码。如果您只想生成密码,可以使用System.Web.Security.Membership class' GeneratePassword方法为您选择一个密码。

#Add the assembly
Add-Type -AssemblyName System.Web
#18 characters with a minimum of 4 non alphanumeric
$unsecurepass = [System.Web.Security.Membership]::GeneratePassword(18,4)
#convert to a securestring
$SecurePass = ConvertTo-SecureString -String $unsecurepass -AsPlainText -Force
#give the initial credential to the person running the script
Write-Output "User's One Time Password: $unsecurepass"
#Clear the unsecure password
Remove-Variable unsecurepass
#continue with New-ADUser