我对使用 PowerShell 和所有脚本语言相对较新,如果这是一个愚蠢的问题,我深表歉意。
我的脚本如下:
$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
}
它可以很好地创建用户和驱动器,但只有 \NAS\student\ 而不是我理想中想要的,例如 \NAS2\student\Tsmith。
我也收到一个错误:
`New-Item : The path is not of a legal form.
At D:\powershell_create_bulk_users\bulk_users1_Modified.ps1:8 char: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`
希望有人能指出我正确的方向吗?
uj5u.com热心网友回复:
发生这种情况是因为您忘记指定新主目录的名称 -New-Item
试图提供帮助,然后“呼叫者可能希望我创建一个以student
路径命名的档案夹\\NAS
” - 但\\NAS
不是目录,并尝试打开它因此会导致您看到的错误。
更改此行:
$fullPath = '\\NAS\student\'
到:
$basePath = '\\NAS\student\'
# construct full home directory path for user to basepath username
$fullPath = Join-Path $basePath -ChildPath $_.SamAccountName
随后对Set-ADUser
and 的呼叫New-Item
现在将创建主目录并将其设定为\\NAS\student\username
正确
0 评论