Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell te如何为多个OU设置不同的主目录?_Powershell_Active Directory_Home Directory - Fatal编程技术网

Powershell te如何为多个OU设置不同的主目录?

Powershell te如何为多个OU设置不同的主目录?,powershell,active-directory,home-directory,Powershell,Active Directory,Home Directory,我正在构建一个脚本来检查用户的homeditory是否正确,如果没有设置正确的路径。OU-1的路径与OU-2不同,有些用户是例外。但是脚本不起作用 到目前为止,我得到的是: $folderpath = "\\172.16.32.27\gebruikers\homedir\", "\\172.16.32.27\share\homedirectories\" $homedrive = "H" $SearchBase = "OU=test,DC=Test,DC=org", "OU=users,

我正在构建一个脚本来检查用户的homeditory是否正确,如果没有设置正确的路径。OU-1的路径与OU-2不同,有些用户是例外。但是脚本不起作用

到目前为止,我得到的是:

$folderpath  = "\\172.16.32.27\gebruikers\homedir\", "\\172.16.32.27\share\homedirectories\"
$homedrive   = "H"
$SearchBase  = "OU=test,DC=Test,DC=org", "OU=users,DC=Test,DC=org"
$domain      = "test.org"
$excludes    = @("test", "user22")
$i = 0

$filter3 = "homedirectory -notlike '$("$homepath[$i]")' -and samaccountname -ne '$($excludes -join "' -and samaccountname -ne '")'"

$SearchBase | foreach { 
    Get-ADUser -SearchBase $_ -Filter $filter3 -Properties HomeDirectory, UserPrincipalName, Homedrive, samaccountname | % {
        $homedirectory = "$($folderpath[$i])$($_.SamAccountName)"

        if (!(Test-Path -Path $homedirectory)) {
            New-Item -Type Directory -Path $homedirectory

            $acl = Get-Acl -Path $homedirectory

            $permission = $_.UserPrincipalname, 'FullControl', 'ContainerInherit, ObjectInherit', 'None', 'Allow'
            $rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permission
            $acl.SetAccessRule($rule)

            $permission = "$domain\Domain Admins", 'FullControl', 'ContainerInherit, ObjectInherit', 'None', 'Allow'
            $rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permission
            $acl.SetAccessRule($rule)

            Set-Acl -Path $homedirectory -AclObject $acl
            Set-ADUser $_ -HomeDirectory "$homedirectory" -HomeDrive $homedrive
        } elseif ($_.HomeDirectory -ne "$homedirectory*" -or $_.Homedrive -ne "$homedrive") {
            Set-ADUser $_ -HomeDirectory "$homedirectory" -HomeDrive $homedrive
        }
    }
    $i++
}

如果在OU和主目录之间创建映射:

$homeShares = @{
  'OU=test,DC=Test,DC=org'  = '\\172.16.32.27\gebruikers\homedir'
  'OU=users,DC=Test,DC=org' = '\\172.16.32.27\share\homedirectories'
}
您可以这样处理它们:

foreach ($ou in $SearchBase) {
  Get-ADUser -SearchBase $ou ... | ForEach-Object {
    $homedirectory = Join-Path $homeShares[$ou] $_.SamAccountName
    if (Test-Path ...) {
      ...
    }
  }
}
if not exist \\server\share\%username% mkdir \\server\share\%username%

话虽如此,IMHO更干净的方法是将所有主目录放在一个共享下,调整共享文件夹上的(NTFS)权限,如下所示:

foreach ($ou in $SearchBase) {
  Get-ADUser -SearchBase $ou ... | ForEach-Object {
    $homedirectory = Join-Path $homeShares[$ou] $_.SamAccountName
    if (Test-Path ...) {
      ...
    }
  }
}
if not exist \\server\share\%username% mkdir \\server\share\%username%
  • 管理员:
    • 完全控制(此文件夹、子文件夹和文件)
  • 系统:
    • 完全控制(此文件夹、子文件夹和文件)
  • 已验证用户:
    • 列表文件夹(仅此文件夹)
    • 创建文件夹(仅此文件夹)
    • 写入属性(仅此文件夹)
    • 写入扩展属性(仅此文件夹)
  • 创建者所有者:
    • 完全控制(子文件夹和文件)
并使用简单的登录脚本自动创建缺少的主目录。在批处理中,它看起来有点像这样:

foreach ($ou in $SearchBase) {
  Get-ADUser -SearchBase $ou ... | ForEach-Object {
    $homedirectory = Join-Path $homeShares[$ou] $_.SamAccountName
    if (Test-Path ...) {
      ...
    }
  }
}
if not exist \\server\share\%username% mkdir \\server\share\%username%
但您也可以使用VBScript或PowerShell

除此之外,您的用户不仅可以访问自己的家,甚至看不到其他人的家