Files
management-scripts/Create-LocalAdmin.ps1
T

42 lines
1.9 KiB
PowerShell
Raw Normal View History

2020-09-28 15:14:18 -04:00
#Requires -Version 5.1
2020-10-22 14:01:35 -04:00
param(
[Parameter(Mandatory=$true)][string]$Username,
[Parameter(Mandatory=$true)][string]$Password,
[Parameter(Mandatory=$true)][string]$FullName,
[Parameter(Mandatory=$true)][string]$Description
)
2021-09-20 07:39:32 -04:00
## Secure supplied password
2020-09-28 15:14:18 -04:00
Try { $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force ; $Password = $null }
2020-10-22 14:01:35 -04:00
Catch { LogErr $_.Exception.Message ; Exit }
If ( IsAdmin )
{
If ( $(Get-CimInstance -ClassName Win32_OperatingSystem -Property ProductType | Select-Object -ExpandProperty ProductType) -ne "2" )
{
2020-09-28 15:14:18 -04:00
If ( $(Get-LocalUser -Name $Username -ErrorAction SilentlyContinue) )
{
2020-10-22 14:01:35 -04:00
LogMsg "Updating user: ${Username}"
2020-09-28 15:14:18 -04:00
Try { Set-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires $true }
2020-10-22 14:01:35 -04:00
Catch { LogErr $_.Exception.Message ; Exit }
}
Else
{
2020-10-22 14:01:35 -04:00
LogMsg "Creating user: ${Username}"
2020-09-28 15:14:18 -04:00
Try { New-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires $true }
2020-10-22 14:01:35 -04:00
Catch { LogErr $_.Exception.Message ; Exit }
}
If ( !($(Get-LocalGroupMember -Group "Administrators" -Member $Username -ErrorAction SilentlyContinue)) )
{
2020-10-22 14:01:35 -04:00
LogMsg "Adding ""${Username}"" to local Administrators group"
Try { Add-LocalGroupMember -Group "Administrators" -Member $Username }
2020-10-22 14:01:35 -04:00
Catch { LogErr $_.Exception.Message ; Exit }
}
}
2020-10-22 14:01:35 -04:00
Else { LogMsg "The local user account ""${Username}"" cannot be created on a domain controller" }
}
2020-10-22 14:01:35 -04:00
Else { LogMsg "Cannot create a local administrator account without administrative privileges" }
2020-09-28 15:14:18 -04:00
## TODO: Hide account from logon screen
#Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"