为用户批量创建用户活动目录和主文件夹,但未创建文件夹

为用户批量创建用户活动目录和主文件夹,但未创建文件夹

全部

我正在尝试使用批量文件和 csv 自动创建具有 Active Directory 和主文件夹的用户,但是当用户完成后,我检查用户配置文件,它会重定向每个用户的主文件夹,但我检查目录文件夹不会自动创建,您能解决我的问题吗?

Import-Module activedirectory
  
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:\blablabla.csv

#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
    $Password = $User.Password

    #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 "[email protected]" `
            -Name "$Firstname $Lastname" `
            -GivenName $Firstname `
            -Surname $Lastname `
            -Enabled $True `
            -DisplayName "$Firstname $Lastname" `
            -Path $OU `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $False `
            -HomeDirectory "\\FPEB2019\William\$($Lastname +" "+ $Username)" -HomeDrive "Z:" `   
    }
}
```[result][1]


  [1]: https://i.stack.imgur.com/Vm896.png

答案1

验证参数的值HomeDirectory "\\FPEB2019\William\$($Lastname +" "+ $Username)"是否为正确且预期的值,确保您不需要使用双引号的转义序列来连接字符串以获取文件夹名称。检查您是否在任何变量中使用了无效字符。您是否尝试过在姓氏和用户名之间不加空格?

您可以在 foreach 循环中添加一个命令,以便在所需的 UNC 路径中创建所需的文件夹

相关内容