我對使用 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-ADUserand 的呼叫New-Item現在將創建主目錄并將其設定為\\NAS\student\username正確
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405510.html
標籤:
