46 lines
2.4 KiB
PowerShell
46 lines
2.4 KiB
PowerShell
## Source the tools script if it isn't already available
|
|
If ( !($MSPName) ) { . $([Scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1'))) }
|
|
|
|
#Requires -Version 5.1
|
|
Function Create-LocalAdmin
|
|
{
|
|
param(
|
|
[Parameter(Mandatory=$true)][string]$Username,
|
|
[Parameter(Mandatory=$true)][string]$Password,
|
|
[Parameter(Mandatory=$true)][string]$FullName,
|
|
[Parameter(Mandatory=$true)][string]$Description
|
|
)
|
|
$LogName = $MyInvocation.MyCommand
|
|
Log "Securing supplied password" -Name $LogName
|
|
Try { $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force ; $Password = $null }
|
|
Catch { $_.Exception.Message | Log -Error -Name $LogName ; Exit }
|
|
If ( IsAdmin )
|
|
{
|
|
If ( $(Get-CimInstance -ClassName Win32_OperatingSystem -Property ProductType | Select-Object -ExpandProperty ProductType) -ne "2" )
|
|
{
|
|
If ( $(Get-LocalUser -Name $Username -ErrorAction SilentlyContinue) )
|
|
{
|
|
Log "Updating user: ${Username}" -Name $LogName
|
|
Try { Set-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires $true }
|
|
Catch { $_.Exception.Message | Log -Error -Name $LogName ; Exit }
|
|
}
|
|
Else
|
|
{
|
|
Log "Creating user: ${Username}" -Name $LogName
|
|
Try { New-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires $true }
|
|
Catch { $_.Exception.Message | Log -Error -Name $LogName ; Exit }
|
|
}
|
|
If ( !($(Get-LocalGroupMember -Group "Administrators" -Member $Username -ErrorAction SilentlyContinue)) )
|
|
{
|
|
Log "Adding ""${Username}"" to local Administrators group" -Name $LogName
|
|
Try { Add-LocalGroupMember -Group "Administrators" -Member $Username }
|
|
Catch { $_.Exception.Message | Log -Error -Name $LogName ; Exit }
|
|
}
|
|
}
|
|
Else { Log "The local user account ""${Username}"" cannot be created on a domain controller" -Name $LogName }
|
|
}
|
|
Else { Log "Cannot create a local administrator account without administrative privileges" -Name $LogName }
|
|
|
|
## TODO: Hide account from logon screen
|
|
#Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"
|
|
} |