在 PowerShell 脚本末尾添加命令

Add a command at the end of the PowerShell script

我有一个脚本可以将用户添加到 Active Directory

Import-Module activedirectory

$ADUsers = Import-csv E:\src\userlist.csv

foreach ($User in $ADUsers)

    {
        $Username   = $User.username
        $Password   = $User.password
        $Firstname  = $User.firstname
        $Lastname   = $User.lastname
        $OU         = $User.ou #This field refers to the OU the user account is to be created in
        $email      = $User.email
        $streetaddress = $User.streetaddress
        $city       = $User.city
        $postalcode    = $User.postalcode
        $state      = $User.state
        $country    = $User.country
        $telephone  = $User.telephone
        $jobtitle   = $User.jobtitle
        $company    = $User.company
        $department = $User.department
        $Password = $User.Password

        # check if user already existe
        if (Get-ADUser -F {SamAccountName -eq $Username})
        {
            Write-Warning "The $Username already exist."   
        }
        else
        {
            #create user account in the good $OU from the csv
            New-ADUser -SamAccountName $Username -UserPrincipalName "$Username@" -Name "$Firstname $Lastname" -GivenName $Firstname -Surname $Lastname -Enabled $True -DisplayName "$Lastname, $Firstname" -Path $OU -City $city -Company $company -State $state -StreetAddress $streetaddress -OfficePhone $telephone -EmailAddress $email -Title $jobtitle -Department $department -postalcode $postalcode -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $false
        }
    }

在同一个脚本中,我还希望有 New-ADUser 命令来添加代理地址邮件,如下所示:

Set-ADUser -Identity $Username -EmailAddress $email -add  {ProxyAddresses="smtp:$email"}

如何将 New-ADUser 添加到我的脚本中?

为了避免必须首先从 CSV 创建这么多变量,主要是为了不必在代码中使用反引号,我建议您改用 Splatting 以使代码更具可读性和可维护性.

您可以使用 OtherAttributes 参数通过 New-ADUSer cmdlet 添加 proxyAddresses 属性,或者在创建用户后使用 Set-ADUser 添加此属性。

proxyAddresses 属性是一个 强类型 字符串数组。这意味着您不能使用 'normal' 数组 (Object[]),因为在那里您可以拥有所有类型的值,而不仅仅是字符串。这就是为什么下面的代码将 SMTP 电子邮件地址转换为 [string[]].

我假设要添加的电子邮件地址应该是主电子邮件地址,所以这就是我使用 SMTP:(全部大写)的原因。

Import-Csv -Path 'E:\src\userlist.csv' | ForEach-Object {
    # the '$_' automatic variable holds one record from the CSV

    # for convenience create these two variables
    $accountName = $_.username
    # the proxyAddresses attribute is an array of STRONGLY typed strings
    $proxyAddresses = [string[]]"SMTP:$($_.email)"

    # check if user already exists
    if (Get-ADUser -Filter "SamAccountName -eq '$accountName'") {
            Write-Warning "The $accountName already exist." 
    }
    else {
        # lire les variables de chaque champs et les assigner en variables de commandes 
        # use Splatting 
        $userParams = @{
            SamAccountName        = $accountName
            UserPrincipalName     = "$accountName@yourdomain.com"
            Name                  = '{0} {1}' -f $_.firstname, $_.lastname
            DisplayName           = '{0}, {1}' -f $_.lastname, $_.firstname
            GivenName             = $_.firstname
            Surname               = $_.lastname
            Enabled               = $true
            Path                  = $_.ou
            City                  = $_.city
            Company               = $_.company
            State                 = $_.state
            StreetAddress         = $_.streetaddress
            OfficePhone           = $_.telephone
            EmailAddress          = $_.email
            Title                 = $_.jobtitle
            Department            = $_.department
            PostalCode            = $_.postalcode
            AccountPassword       = (ConvertTo-SecureString $_.Password -AsPlainText -Force)
            ChangePasswordAtLogon = $false

            # either add the proxyAddresses here, or do it AFTER creating the new user with
            # Set-ADUser -Identity $accountName -Add @{'proxyAddresses' = $proxyAddresses}
            OtherAttributes       = @{'proxyAddresses' = $proxyAddresses}
        }

        #create user account in the good $OU from the csv
        New-ADUser @userParams
    }
}

希望对您有所帮助