ren New-LocalUser to New-LocalAccount
minor updates to New-LocalAccount
This commit is contained in:
@@ -693,46 +693,38 @@ Function IsRunning ([Parameter(ValueFromPipeline=$true)][string]$Name) {
|
||||
}
|
||||
|
||||
# Function to create a local user account
|
||||
Function New-LocalUser {
|
||||
Function New-LocalAccount {
|
||||
param(
|
||||
[Parameter(Mandatory=$true)][string]$Username,
|
||||
[Parameter(Mandatory=$true)][string]$Password,
|
||||
[Parameter(Mandatory=$true)][SecureString]$Password,
|
||||
[Parameter(Mandatory=$true)][string]$FullName,
|
||||
[Parameter(Mandatory=$true)][string]$Description,
|
||||
[Parameter(Mandatory=$false)][switch]$MakeAdmin=$false,
|
||||
[Parameter(Mandatory=$false)][switch]$Hide=$false
|
||||
)
|
||||
|
||||
# Secure supplied password
|
||||
Try { $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force ; $Password = $null }
|
||||
Catch { Write-Error $_.Exception.Message ; Exit }
|
||||
If ( IsAdmin )
|
||||
{
|
||||
If ( $(Get-CimInstance -ClassName Win32_OperatingSystem -Property ProductType | Select-Object -ExpandProperty ProductType) -ne "2" )
|
||||
{
|
||||
If ( $(Get-LocalUser -Name $Username -ErrorAction SilentlyContinue) )
|
||||
{
|
||||
Write-Output "Updating user: ${Username}"
|
||||
Try { Set-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires }
|
||||
Catch { Write-Error $_.Exception.Message ; Exit }
|
||||
}
|
||||
Else
|
||||
{
|
||||
Write-Output "Creating user: ${Username}"
|
||||
Try { New-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires }
|
||||
Catch { Write-Error $_.Exception.Message ; Exit }
|
||||
}
|
||||
If ( (-not($(Get-LocalGroupMember -Group "Administrators" -Member $Username -ErrorAction SilentlyContinue))) -and $MakeAdmin )
|
||||
{
|
||||
Write-Output "Adding ""${Username}"" to local Administrators group"
|
||||
Try { Add-LocalGroupMember -Name "Administrators" -Member $(Get-LocalUser -Name $Username) }
|
||||
Catch { Write-Error $_.Exception.Message ; Exit }
|
||||
}
|
||||
}
|
||||
Else { Write-Output "Cannot create or modify local accounts on a domain controller" }
|
||||
If ( !IsAdmin ) {
|
||||
Write-Output "Cannot create or modify a local account without administrative privileges"
|
||||
Return
|
||||
}
|
||||
If ( $(Get-CimInstance -ClassName Win32_OperatingSystem -Property ProductType | Select-Object -ExpandProperty ProductType) -eq "2" ) {
|
||||
Write-Output "Cannot create or modify local accounts on a domain controller"
|
||||
Return
|
||||
}
|
||||
If ( $(Get-LocalUser -Name $Username -ErrorAction SilentlyContinue) ) {
|
||||
Write-Output "Updating user: ${Username}"
|
||||
Try { Set-LocalUser -Name "${Username}" -Password $SecurePassword -FullName "${FullName}" -Description "${Description}" -AccountNeverExpires -PasswordNeverExpires }
|
||||
Catch { Write-Error $_.Exception.Message ; Exit }
|
||||
}
|
||||
Else {
|
||||
Write-Output "Creating user: ${Username}"
|
||||
Try { New-LocalUser -Name "${Username}" -Password $SecurePassword -FullName "${FullName}" -Description "${Description}" -AccountNeverExpires -PasswordNeverExpires }
|
||||
Catch { Write-Error $_.Exception.Message ; Exit }
|
||||
}
|
||||
If ( (-not($(Get-LocalGroupMember -Group "Administrators" -Member $Username -ErrorAction SilentlyContinue))) -and $MakeAdmin ) {
|
||||
Write-Output "Adding ""${Username}"" to local Administrators group"
|
||||
Try { Add-LocalGroupMember -Name "Administrators" -Member $(Get-LocalUser -Name "${Username}") }
|
||||
Catch { Write-Error $_.Exception.Message ; Exit }
|
||||
}
|
||||
Else { Write-Output "Cannot create or modify a local account without administrative privileges" }
|
||||
|
||||
If ( $Hide ) {
|
||||
# TODO: Hide account from logon screen
|
||||
#Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"
|
||||
|
||||
Reference in New Issue
Block a user