自动创建用户主目录 Powershell
Automate user homedirectory creation Powershell
我正在自动执行在 Windows 系统上创建 LocalUsers 的过程。到目前为止,我在 New-LocalUser 上使用了 Microsoft 文档,它可以很好地创建帐户,这是我目前的代码:
function New-AdminUser {
param(
[Parameter(Position=0)]
[string] $UNameLocal,
[Parameter(Position=1)]
[string] $UDescription,
[Parameter(Position=2)]
[System.Security.SecureString] $Password
)
New-LocalUser -Name $UNameLocal -Description $UDescription -Password $Password -AccountNeverExpires -Confirm
Add-LocalGroupMember -Group "Administrators" -Member $UNameLocal
}
但是这个命令实际上并没有在C:\Users\username
中生成主目录。
我可以通过手动登录创建的用户来创建它,但我想在 Powershell 中自动执行它。我在 LocalAccounts module.
中找不到任何内容
有什么方法可以使用 Powershell 在 Windows10 中自动设置本地帐户,而无需手动登录新帐户?
代码如下:
param([Parameter(Mandatory=$true)][String]$samAccountName)
$fullPath = "\srv2012r2\Users\{0}" -f $samAccountName
$driveLetter = "Z:"
$User = Get-ADUser -Identity $samAccountName
if($User -ne $Null) {
Set-ADUser $User -HomeDrive $driveLetter -HomeDirectory $fullPath -ea Stop
$homeShare = New-Item -path $fullPath -ItemType Directory -force -ea Stop
$acl = Get-Acl $homeShare
$FileSystemRights = [System.Security.AccessControl.FileSystemRights]"Modify"
$AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
$InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
$PropagationFlags = [System.Security.AccessControl.PropagationFlags]"InheritOnly"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($User.SID, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)
$acl.AddAccessRule($AccessRule)
Set-Acl -Path $homeShare -AclObject $acl -ea Stop
Write-Host ("HomeDirectory created at {0}" -f $fullPath)
}
这里是参考:
https://activedirectoryfaq.com/2017/09/powershell-create-home-directory-grant-permissions/
如果您以创建的用户身份启动进程 (cmd /c),它将创建他的配置文件。将此添加到您的函数中:
$Cred = New-Object System.Management.Automation.PSCredential ("$UNameLocal", $Password)
Start-Process "cmd.exe" -Credential $Cred -ArgumentList "/C" -LoadUserProfile
我正在自动执行在 Windows 系统上创建 LocalUsers 的过程。到目前为止,我在 New-LocalUser 上使用了 Microsoft 文档,它可以很好地创建帐户,这是我目前的代码:
function New-AdminUser {
param(
[Parameter(Position=0)]
[string] $UNameLocal,
[Parameter(Position=1)]
[string] $UDescription,
[Parameter(Position=2)]
[System.Security.SecureString] $Password
)
New-LocalUser -Name $UNameLocal -Description $UDescription -Password $Password -AccountNeverExpires -Confirm
Add-LocalGroupMember -Group "Administrators" -Member $UNameLocal
}
但是这个命令实际上并没有在C:\Users\username
中生成主目录。
我可以通过手动登录创建的用户来创建它,但我想在 Powershell 中自动执行它。我在 LocalAccounts module.
有什么方法可以使用 Powershell 在 Windows10 中自动设置本地帐户,而无需手动登录新帐户?
代码如下:
param([Parameter(Mandatory=$true)][String]$samAccountName)
$fullPath = "\srv2012r2\Users\{0}" -f $samAccountName
$driveLetter = "Z:"
$User = Get-ADUser -Identity $samAccountName
if($User -ne $Null) {
Set-ADUser $User -HomeDrive $driveLetter -HomeDirectory $fullPath -ea Stop
$homeShare = New-Item -path $fullPath -ItemType Directory -force -ea Stop
$acl = Get-Acl $homeShare
$FileSystemRights = [System.Security.AccessControl.FileSystemRights]"Modify"
$AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
$InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
$PropagationFlags = [System.Security.AccessControl.PropagationFlags]"InheritOnly"
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($User.SID, $FileSystemRights, $InheritanceFlags, $PropagationFlags, $AccessControlType)
$acl.AddAccessRule($AccessRule)
Set-Acl -Path $homeShare -AclObject $acl -ea Stop
Write-Host ("HomeDirectory created at {0}" -f $fullPath)
}
这里是参考: https://activedirectoryfaq.com/2017/09/powershell-create-home-directory-grant-permissions/
如果您以创建的用户身份启动进程 (cmd /c),它将创建他的配置文件。将此添加到您的函数中:
$Cred = New-Object System.Management.Automation.PSCredential ("$UNameLocal", $Password)
Start-Process "cmd.exe" -Credential $Cred -ArgumentList "/C" -LoadUserProfile