批量Powershell用户csv导入和主文件夹创建

时间:2018-10-29 16:26:06

标签: windows powershell csv azure-active-directory

我正在尝试使用CSV导入来创建批处理用户,但同时还要创建用户的主文件夹和配置文件文件夹,但同时要设置权限。

我在网上找到了很多有用的信息,我只是不知道如何使语法与已经拥有的东西一起使用,这花了我很长的时间。

到目前为止,这是我的脚本,用于在域控制器上创建帐户,然后将其与O365同步。在创建大量用户的同时,我们使用了csv:

Import-Csv "C:\blablabla\filename.csv" | ForEach-Object { 
New-ADUser -Name $_.Name `
 -GivenName $_."GivenName" `
 -Surname $_."Surname" `
 -DisplayName $_."DisplayName" `
 -SamAccountName  $_."samAccountName" `
 -UserPrincipalName  $_."UserPrincipalName" `
 -Path $_."Path" `
 -AccountPassword (ConvertTo-SecureString “Pa$$w0rd” -AsPlainText -force) -Enabled $true `
 -EmailAddress $_."EmailAddress" `
 -ProfilePath $_."ProfilePath" `
 -HomeDrive $_."HomeDrive" `
 -HomeDirectory $_."HomeDirectory" `
 -ScriptPath $_."ScriptPath" `
 -Server $_."Server" `
 -OtherAttributes @{ProxyAddresses= $_."ProxyAddresses"} `
}
Start-ADSyncSyncCycle -PolicyType Initial

所有值都指向excel文件中的列,这些列会根据用户的名字和姓氏自动完成。

例如,我知道我应该按照以下说明创建主目录和个人资料文件夹并设置权限,我只是不知道如何使语法与已经拥有的文件一起使用?

到目前为止,仅在AD中正确设置了值,但未创建文件夹并且未应用权限。

我想我可以添加另一个命令来创建一个新文件夹,但是我不知道如何将其附加到foreach命令上?

New-Item -ItemType Directory -Path \\dc\userdata
$ACL = (Get-ACL -Path $HomeDirectory)
$FullControlAccessRule = (New-Object System.Security.AccessControl.FileSystemAccessRule([System.Security.Principal.NTAccount]"hcc.local\$UserName","FullControl", "ContainerInherit, ObjectInherit", "None", "Allow"))
$ACL.AddAccessRule($FullControlAccessRule)
Set-ACL -Path $HomeDirectory $ACL

任何帮助将不胜感激。

谢谢。

因此,按照NAS所说的,然后这样

?

 -OtherAttributes @{ProxyAddresses= $_."ProxyAddresses"}
 New-Item -ItemType Directory -Path $_.HomeDirectory           
 New-Item -ItemType Directory -Path $_.ProfilePath             
 $ACL = (Get-ACL -Path $_.HomeDirectory)                       
 $FullControlAccessRule = (New-Object System.Security.AccessControl.FileSystemAccessRule(
                                [System.Security.Principal.NTAccount]"hcc.local\$($_.samAccountName)",        
                                "FullControl", "ContainerInherit, ObjectInherit", "None", "Allow"))
    $ACL.AddAccessRule($FullControlAccessRule)
    Set-ACL -Path $_.HomeDirectory $ACL
 $ACL = (Get-ACL -Path $_.ProfilePath)                       
 $FullControlAccessRule = (New-Object System.Security.AccessControl.FileSystemAccessRule(
                                [System.Security.Principal.NTAccount]"hcc.local\$($_.samAccountName)",        
                                "FullControl", "ContainerInherit, ObjectInherit", "None", "Allow"))
    $ACL.AddAccessRule($FullControlAccessRule)
    Set-ACL -Path $_.ProfilePath $ACL

1 个答案:

答案 0 :(得分:1)

FooClass
相关问题