【问题标题】:Batch Powershell User csv import and home folder creation批量 Powershell 用户 csv 导入和主文件夹创建
【发布时间】:2019-04-02 15:25:04
【问题描述】:

我正在尝试使用 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 文件中的列,这些列会根据用户的名字和姓氏自动完成。

我知道我应该创建 home 和 profile 文件夹并设置权限,例如,我只是不知道如何使语法与我已有的语法一起工作?

到目前为止,仅在 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

【问题讨论】:

    标签: windows powershell csv azure-active-directory


    【解决方案1】:
    Import-Csv "C:\blablabla\filename.csv" | ForEach-Object { 
        New-ADUser -Name $_.Name `
         ...
         -OtherAttributes @{ProxyAddresses= $_."ProxyAddresses"}      # Remove backtick
        New-Item -ItemType Directory -Path $_.HomeDirectory           # create home path if needed
        New-Item -ItemType Directory -Path $_.ProfilePath             # create profile path if needed
        $ACL = (Get-ACL -Path $_.HomeDirectory)                       # No need for quotes around properties if they do not contain spaces or other special characters
        $FullControlAccessRule = (New-Object System.Security.AccessControl.FileSystemAccessRule(
                                    [System.Security.Principal.NTAccount]"hcc.local\$($_.samAccountName)",        # replace $UserName with correct variable
                                    "FullControl", "ContainerInherit, ObjectInherit", "None", "Allow"))
        $ACL.AddAccessRule($FullControlAccessRule)
        Set-ACL -Path $_.HomeDirectory $ACL
        # ->                                                                     # repeat for profile path if needed
    }
    

    【讨论】:

    • 您好,非常感谢您,hcc.local\$($_.samAccountName)" 位会自动从上面获取用户名还是我必须手动输入它?有效我想要实现的是让它根据它从 csv 获取的值自行完成所有这些操作。另外,如果需要,我该如何为配置文件路径重复此操作?我只是有点困惑。所有来自 csv 的用户将需要创建和分配他们的配置文件路径和主驱动器,并为主目录分配驱动器号等等。非常感谢您的宝贵时间!
    • 我还想在脚本之后添加以下命令(我已经有自动连接到 Office365 的方式)但不知道如何使电子邮件地址从上面拉入,或者,甚至如何设置这部分为每个用户递归发生。有什么想法吗?我将在下面发布我试图附加的命令
    • Set-MsolUserLicense -UserPrincipalName "@domain.com" -AddLicenses "domain:STANDARDPACK" Add-MailboxPermission -Identity newuser@domain.com -User 'admin@domain.com' -AccessRight FullAccess -InheritanceType全部 -Automapping $false Add-MailboxFolderPermission -Identity newuser@domain.com:\calendar -user admin@domain.com -AccessRights Editor 有什么想法吗?
    • 要重复配置文件路径,复制代码的acl部分(从$ACL=...Set-ACL -Path...)并将变量$_.HomeDirectory替换为$_.ProfilePath
    • 对于您的第二条评论,最好创建一个新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多