首页 > 解决方案 > 创建新 AD 用户的脚本失败

问题描述

我想用 CSV 文件自动创建用户帐户。

我做了一些测试,但仍然失败。

# Import active directory module for running AD cmdlets
Import-Module activedirectory
$log = "log.txt"  
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:\Scripts\newusers.csv -Delimiter ";" 

#Loop through each row containing user details in the CSV file 
foreach ($User in $ADUsers)
{
    #Read user data from each field in each row and assign the data to a variable as below

    $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
 #  $zipcode    = $User.zipcode
    $state      = $User.state
 #  $country    = $User.country
 #  $telephone  = $User.telephone
    $jobtitle   = $User.jobtitle
    $company    = $User.company
    $department = $User.department
    $Password = $User.password

echo = $Firstname

    #Check to see if the user already exists in AD
    if (Get-ADUser -F {SamAccountName -eq '$Username'})
    {
         #If user does exist, give a warning
         Write-Warning "A user account with username $Username already exist in Active Directory."
    }
    else
    {
        #User does not exist then proceed to create the new user account

        #Account will be created in the OU provided by the $OU variable read from the CSV file
        New-ADUser `
            -SamAccountName $Username `
            -UserPrincipalName "$Username@axpenet.local" `
            -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 `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True

    }
}

如果我然后运行脚本,我会收到以下错误

    New-ADUser : No se puede validar el argumento del parámetro 'Path'. El argumento es null o está vacío. Proporcione un argumento que no sea null o que no 
esté vacío e intente ejecutar el comando de nuevo.
En C:\Scripts\newpipol.ps1: 50 Carácter: 19
+             -Path $OU `
+                   ~~~
    + CategoryInfo          : InvalidData: (:) [New-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.NewADUser

ConvertTo-SecureString : No se puede enlazar el argumento al parámetro 'String' porque es nulo.
En C:\Scripts\newpipol.ps1: 59 Carácter: 54
+ ...            -AccountPassword (convertto-securestring $Password -AsPlai ...
+                                                         ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

所以我认为 New-ADUser 命令没有从上面获取变量值。

例如,如何在某处设置断点以查看脚本特定点上的变量值?

标签: powershell

解决方案


推荐阅读