【问题标题】:Trying to Generate a Random password尝试生成随机密码
【发布时间】:2013-06-19 16:03:33
【问题描述】:

我有下面的 powershell 脚本,我正在其中创建一个新用户并尝试设置其所需的属性。我面临的问题是,我无法通过创建“获取密码”功能来设置随机密码。请看一下。

Import-Module ActiveDirectory
[xml]$dataSource = Get-Content C:\Names1.xml

$name = Read-Host 'Please enter the table name : '

$user_logon = $dataSource.names.$name | ? { $_.Rule_Label -eq 'Regular service account (user logon)'}

$display_name = $dataSource.names.$name | ? { $_.Rule_Label -eq 'Regular service account (display name)'}

$pre_windows = $dataSource.names.$name | ? { $_.Rule_Label -eq 'Regular service account (pre-Windows 2000)'}

Function GET-Temppassword() { 
Param(
[int]$length=10, 
[string[]]$sourcedata 
)

For ($loop=1; $loop –le $length; $loop++) { 
        $TempPassword+=($sourcedata | GET-RANDOM)
        }

return $TempPassword
}

switch ($name) 
{ 
    DevTable{foreach($dataRecord in $dataSource) 
    {
    try     
    {
    $cn=$user_logon.Output_Value
    $sAMAccountName=$user_logon.Output_Value
    $givenName=$user_logon.Output_Value
    $sn=$user_logon.Output_Value 
    $displayName=$display_name.Output_Value 
    $userPrincipalName=$sAMAccountName + “@test.com”;

    $alphabet=$NULL;For ($a=65;$a –le 90;$a++) {$alphabet+=,[char][byte]$a }
    GET-Temppassword –length 10 –sourcedata $alphabet

    New-ADUser $cn -SamAccountName $sAMAccountName -GivenName $givenName -Surname $sn -DisplayName $displayName -UserPrincipalName $userPrincipalName -accountPassword (ConvertTo-SecureString -AsPlainText $alphabet -Force) -PasswordNeverExpires $true -Path "OU=Service,OU=Accounts,DC=xyz,DC=com"  

    set-aduser $cn -replace @{comment="xxyyzz"}
    set-aduser $cn -replace @{"account"=1}      

    Add-ADGroupMember -Identity xyz -Member $cn
    Add-ADGroupMember -Identity abc -Member $cn

    write-host "New DevTable ADUser has been created!!!";
    }

    catch [Exception]
    {

        write-host "Error - Requested AD Service Account is already present...Please check & confirm " -foreground "red"
    }
    }   
    break;
    }

    default {"The table could not be determined!!!"}    

}   

[System.GC]::Collect()

另外,我想知道这是否是设置随机密码最合适的方式。

谢谢。

【问题讨论】:

  • “无法设置随机密码”是什么意思?您是否没有获得密码,或者您生成的密码是否不符合您的密码限制(长度、字符类型等)?您收到了哪些错误消息(如果有)?
  • 当我尝试添加 AccountPassword 属性时,在这种情况下没有创建我的用户。
  • 如果您在 $dataSource 的内容中发布一两行内容可能会有所帮助。

标签: powershell powershell-2.0 powershell-ise


【解决方案1】:
$alphabet=$NULL;For ($a=65;$a –le 90;$a++) {$alphabet+=,[char][byte]$a }
GET-Temppassword –length 10 –sourcedata $alphabet

New-ADUser $cn -SamAccountName $sAMAccountName -GivenName $givenName -Surname $sn -DisplayName $displayName -UserPrincipalName $userPrincipalName -accountPassword (ConvertTo-SecureString -AsPlainText $alphabet -Force) -PasswordNeverExpires $true -Path "OU=Service,OU=Accounts,DC=rjfdev,DC=com"   

您没有捕获从Get-Temppassword 返回的值。您将 $alphabet 作为用户帐户的新密码传递。试试这个(未经测试):

$alphabet=$NULL;For ($a=65;$a –le 90;$a++) {$alphabet+=,[char][byte]$a }
$TempPassword = GET-Temppassword –length 10 –sourcedata $alphabet

New-ADUser $cn -SamAccountName $sAMAccountName -GivenName $givenName -Surname $sn -DisplayName $displayName -UserPrincipalName $userPrincipalName -accountPassword (ConvertTo-SecureString -AsPlainText $TempPassword -Force) -PasswordNeverExpires $true -Path "OU=Service,OU=Accounts,DC=rjfdev,DC=com"   

【讨论】:

  • 我很害怕,但是即使进行了上述更改,随机密码也没有设置。
  • @alroc 是正确的,但他在修复中添加了一个错字。在他的修复中,将$TempPassword-Force 更改为$TempPassword -Force,然后再试一次。我已经编辑了答案,但它需要被接受。
  • @charleswj81,您的编辑被四分之三的审稿人拒绝,理由是“此编辑太小;建议的编辑应该是解决帖子中多个问题的实质性改进。”。这真的很遗憾,因为您的编辑完全有效,实际上是“解决多个(嗯,唯一的)问题的实质性改进” - 如果没有那个空间,代码错误的。我希望有办法审查评论者。
猜你喜欢
  • 2022-10-18
  • 2010-09-08
  • 2016-01-10
  • 2012-11-06
  • 2015-04-13
  • 2012-07-28
  • 2011-09-05
相关资源
最近更新 更多