使用PoweShell脚本映射主目录时出错

时间:2018-06-26 14:25:19

标签: powershell active-directory sysadmin

我正在使用脚本来创建新用户,并且一切正常,但是,当我尝试使用该脚本创建的用户登录时,会出现以下错误:

A Problem has occurred and your network home directory is not available.

This may be because a network file server is offline.

You have been logged on with a temporary home drive (H:) which may be shared
with other users.

该脚本会创建用户并根据我的判断应用正确的权限。

这是脚本:

# Import active directory module for running AD cmdlets
Import-Module activedirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv .\Create_Staff_Accounts.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
$group      = $User.group
$title      = $User.title
$start      = $User.start
$Homedrive  = "H:"
$UserRoot = "\\servername\st$\"
$HomeDirectory = $UserRoot + $Username

#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 exists 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 `
        -Name "$Firstname $Lastname" `
        -UserPrincipalName "$($username)@domain.local" `
        -GivenName $Firstname `
        -Surname $Lastname `
        -Enabled $True `
        -DisplayName "$Firstname $Lastname" `
        -Path $OU `
        -AccountPassword (convertto-securestring $Password -AsPlainText -Force) `
        -Homedrive $Homedrive `
        -HomeDirectory $HomeDirectory `
        -ScriptPath "logonscript.bat" `
        -Description "Staff Account:  $($title) from: $($start)"
         #-ChangePasswordAtLogon $True `



    #Now create the home folder and set modify permissions
    Add-ADGroupMember -Identity $group -Members $Username
    Add-ADGroupMember -Identity groupname -Members $Username
    Add-ADGroupMember -Identity groupname -Members $Username
    New-Item -ItemType Directory -Path "\\servername\st$\$($User.username)"
    $path = Get-Item -Path "\\servername\st$\$($User.username)"
    $acl = (Get-Item $path).GetAccessControl('Access')
    $AR = New-Object System.Security.AccessControl.FileSystemAccessRule($Username, 'Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
    $acl.SetAccessRule($AR)
    Set-Acl -Path $Path -AclObject $acl





}
} 

感谢您的帮助和支持。

0 个答案:

没有答案