Crear múltiples usuarios en AD y asignarles un directorio personal.
Soy relativamente nuevo en el uso de PowerShell y todos los lenguajes de scripting, así que disculpen si esta es una pregunta tonta.
Mi script es el siguiente:
$pw = read-host "Password" -AsSecureString
Import-CSV D:\powershell_create_bulk_users\bulk_users1_Modified.csv | foreach {
New-ADUser -Name $_.Name -SamAccountName $_.SamAccountName -Surname $_.Surname -
DisplayName $_.DisplayName -Path $_.Path -AccountPassword $pw -ChangePasswordAtLogon
$false -Enabled $true
$fullPath = '\\NAS\student\'
$driveLetter = "Z:"
$user = Get-ADUser $_.SamAccountName
Set-ADUser $User -HomeDrive $driveLetter -HomeDirectory $fullPath -ea Stop
$homeShare = New-Item -Path $fullPath -ItemType Directory -force -ea Stop
$acl = Get-Acl $homeShare
$FileSystemRights = [System.Security.AccessControl.FileSystemRights]"Modify"
$AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
$InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
$PropagationFlags = [System.Security.AccessControl.PropagationFlags]"InheritOnly"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($User.SID, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)
$acl.AddAccessRule($AccessRule)
Set-Acl -Path $homeShare -AclObject $acl -ErrorAction Stop
}
Crea al usuario correctamente y también la unidad, pero solo \NAS\student\ y no lo que idealmente quiero, que es por ejemplo \NAS2\student\Tsmith.
También obtengo un error:
New-Item : La ruta no tiene un formato válido.
En D:\powershell_create_bulk_users\bulk_users1_Modified.ps1:8 carácter:14
+ $homeShare = New-Item -Path $fullPath -ItemType Directory -force -ea S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (\\NAS\student\:String) [New-Item], ArgumentException
+ FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.NewItemCommand
¿Esperaba que alguien pudiera orientarme en la dirección correcta?
davy.ai
Parece que el problema podría estar con la variable $fullPath. Actualmente está configurada como ‘\NAS\student\’, que es el directorio raíz. Si desea crear un subdirectorio como ‘\NAS2\student\Tsmith’, necesitará agregar ‘Tsmith’ al camino.
Intente modificar esta línea:
a esto:
O podría reemplazar ‘Tsmith’ con una variable que contenga el nombre del usuario para facilitar la creación de diferentes directorios para diferentes usuarios.