【问题标题】:Set HomeDirectory parameter for users on ADDS 2008 R2 with powershell使用 powershell 在 ADDS 2008 R2 上为用户设置 HomeDirectory 参数
【发布时间】:2016-04-06 18:43:22
【问题描述】:

我正在尝试将“HomeDirectory”参数设置为给定 .csv 文件的用户列表。我正在使用 Trevor Sullivan 创建的修改后的脚本,并在以下帖子中列出: Url

这是我的脚本:

$UserList = Import-Csv -Path c:\scripts\Users2.csv;  
foreach ($User in $UserList)  {

$Account = Get-ADUser -Identity $User.SamAccountName
$Account.HomeDirectory = '\\adminclusterfs\homedir\{0}' -f $Account.SamAccountName;
$Account.homeDrive = "O:"
Set-ADUser -Instance $Account -PassThru
}

脚本几乎可以正常工作... HomeDirectory 和 HomeDrive 参数已为 .csv 文件上的每个用户正确设置,但文件夹未在文件服务器上创建。

当我手动设置这些参数时,文件夹也会被创建

有人解决了这个问题吗?

非常感谢您的帮助!

【问题讨论】:

  • 请不要使用链接。相反,请复制您的代码的最小但足够的提取。

标签: csv powershell scripting active-directory


【解决方案1】:

如果您的帐户有权访问,AD 用户和计算机会为您创建文件夹。这是在您的计算机上运行的用户和计算机程序的一项功能,而不是 AD 服务器本身的一项功能。

您必须在脚本中添加代码才能创建文件夹。

【讨论】:

【解决方案2】:

这里是我设置 HomeDirectory 和 HomeDrive 参数、创建文件夹并为其分配访问权限的完整脚本。感谢 Trevor Sullivan 和 Sean Kearney 的帖子。

# Import the user data from CSV
$UserList = Import-Csv -Path c:\scripts\Users2.csv;

# For each user ...
foreach ($User in $UserList) {
# Get the user's AD account
$Account = Get-ADUser -Identity $User.SamAccountName

# Dynamically declare their home directory path in a String
$Account.HomeDirectory = '\\fileserver\homedir\{0}' -f $Account.SamAccountName;
$Account.homeDrive = "O:"

# Set their home directory and home drive letter in Active Directory
Set-ADUser -Instance $Account

# Create the folder on the root of the Homedirectory Share
NEW-ITEM –path $Account.HomeDirectory -type directory -force 

# Set parameters for Access rule
$IdentityReference = 'Domain\' + $Account.SamAccountName
$FileSystemAccessRights = [System.Security.AccessControl.FileSystemRights]"FullControl"
$InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]::"ContainerInherit","ObjectInherit"
$PropagationFlags = [System.Security.AccessControl.PropagationFlags]"None"
$AccessControl = [System.Security.AccessControl.AccessControlType]"Allow"

# Build Access Rule from parameters
$AccessRule = NEW-OBJECT System.Security.AccessControl.FileSystemAccessRule -argumentlist ($IdentityReference,$FileSystemAccessRights,$InheritanceFlags,$PropagationFlags,$AccessControl)

# Get current Access Rule from Home Folder for User
$HomeFolderACL = GET-ACL $Account.HomeDirectory
$HomeFolderACL.AddAccessRule($AccessRule)

# Set Access rule to the folder
SET-ACL –path $Account.HomeDirectory -AclObject $HomeFolderACL
}

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2018-07-05
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 2016-02-21
    • 2012-04-08
    相关资源
    最近更新 更多