Powershell GUI /表单-不创建Active Directory帐户

时间:2019-01-28 09:29:41

标签: powershell active-directory windows-server-2012

我不太擅长为标题添加标题,因此,如果标题有点随机,我会道歉。

我正在构建Powershell GUI脚本,以便于创建Active Directory用户,但是提交时并不能创建帐户。

我尝试重新定义变量并重写实际的用户创建脚本。

我没有包含所有位置,因为这会使代码更长,但是下面是所有相关代码。

希望您的天才能为您提供帮助!

Function Display-UserCreation {
    $PasswordValue = "Annodata" + [system.web.security.membership]::GeneratePassword(9, 2)
    $Password = ConvertTo-SecureString $PasswordValue -AsPlainText -Force

    $DisplayName = $UCFirstNameParam + " " + $UCsurNameParam
    $Mail = $UCFirstNameParam + "." + $UCsurNameParam + "@" + "annodata.co.uk"
    $MailAlias = $UCFirstNameParam + "." + $UCsurNameParam + "@" + $DNSRoot2

    $SInitial = $UCsurNameParam[0]
    $Initial = $UCFirstNameParam[0]
    $SAMAccountName = $UCFirstNameParam + "." + $UCsurNameParam
    $SAMAccountLower = $SAMAccountName.ToLower()
    $UserPrincipalName = $UCFirstNameParam + "." + $UCsurNameParam

    $defaultname = $SAMAccountName
    $email = $UCFirstNameParam + "." + $UCsurNameParam
    $i = 1

    While ((Get-ADUser -Filter "SamAccountName -eq '$SAMAccountName'" -ErrorAction SilentlyContinue) -ne $null) {
        $SamAccountName = $defaultname + [string]$i 
        $Mail = $email + [string]$i + "@" + "annodata.co.uk"
        $i++
    }

    if ( $UCLocationParam -EQ 'Bracknell' ) {
        $NewUserParams = @{
            path                  = "OU=Bracknell,OU=Sites,DC=annodata,DC=co,DC=uk"
            SamAccountName        = $SAMAccountName
            Name                  = $SAMAccountName
            DisplayName           = $DisplayName
            GivenName             = $UCFirstNameParam
            Surname               = $UCsurNameParam
            EmailAddress          = $Mail
            UserPrincipalName     = "$SAMAccountName@annodata.co.uk"
            Description           = $UCRoleParam
            ChangePasswordAtLogon = $true
            PasswordNeverExpires  = $false
            AccountPassword       = $password
            Enabled               = $true
        }
        New-ADUser @NewUserParams -ErrorAction SilentlyContinue

        Set-ADAccountPassword -Identity $SAMAccountName -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $Password -Force)
    }
}

Function UCSubmit {
    if ([System.Windows.Forms.MessageBox]::Show("Please Confirm That The Details Are Correct. If they are, click ok", "Confirmation", [System.Windows.Forms.MessageBoxButtons]::OkCancel) -eq "Ok") {

        $UCFirstNameParamLong = New-Object System.Windows.Forms.Label
        $UCFirstNameParamLong.Text = $UCFirstName.Text
        $UCFirstNameParamLong.Location = New-Object System.Drawing.Point(180, 100)
        $UCFirstNameParamLong.AutoSize = $true
        $UCFirstNameParamLong.Font = 'Georgia,10'

        $UCSurnameParamLong = New-Object System.Windows.Forms.Label
        $UCSurnameParamLong.Text = $UCSurname.Text
        $UCSurnameParamLong.Location = New-Object System.Drawing.Point(180, 150)
        $UCSurnameParamLong.AutoSize = $true
        $UCSurnameParamLong.Font = 'Georgia,10'

        $UCRoleParamLong = New-Object System.Windows.Forms.Label
        $UCRoleParamLong.Text = $UCRole.Text
        $UCRoleParamLong.Location = New-Object System.Drawing.Point(180, 200)
        $UCRoleParamLong.AutoSize = $true
        $UCRoleParamLong.Font = 'Georgia,10'

        $UCLocationParamLong = New-Object System.Windows.Forms.Label
        $UCLocationParamLong.Text = $UCSiteSelection.Text
        $UCLocationParamLong.Location = New-Object System.Drawing.Point(180, 250)
        $UCLocationParamLong.AutoSize = $true
        $UCLocationParamLong.Font = 'Georgia,10'

        $UCFirstNameParam = $UCFirstNameParamLong.Text
        $UCSurnameParam = $UCSurnameParamLong.Text
        $UCRoleParam = $UCRoleParamLong.Text
        $UCLocationParam = $UCLocationParamLong.Text

        Display-UserCreation
    }
}

1 个答案:

答案 0 :(得分:0)

仅供以后参考。

似乎一开始并没有导航到创建功能。

一旦解决,它就不会运行IF。 我必须更改IF的参数。

此后,由于某种原因未生成密码,密码复杂性还不够。

我现在已经更改了它,一切都解决了。