我正在自動化在 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 模塊中找不到任何內容。
有什么方法可以使用 Powershell 在 Windows 10 中自動設定本地帳戶,而無需手動登錄新帳戶?
uj5u.com熱心網友回復:
如果您以創建的用戶身份啟動一個行程 (cmd /c),它將創建他的組態檔。將此添加到您的函式中:
$Cred = New-Object System.Management.Automation.PSCredential ("$UNameLocal", $Password)
Start-Process "cmd.exe" -Credential $Cred -ArgumentList "/C" -LoadUserProfile
uj5u.com熱心網友回復:
這是代碼:
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/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/460741.html
