major logging overhaul, plus other fixes

This commit is contained in:
2020-10-22 14:01:35 -04:00
parent 77cf4d6276
commit 978373ebe2
27 changed files with 430 additions and 611 deletions
+12 -17
View File
@@ -1,46 +1,41 @@
## 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
LogMsg "Create-LocalAdmin.ps1"
LogMsg "Securing supplied password"
Try { $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force ; $Password = $null }
Catch { $_.Exception.Message | Log -Error -Name $LogName ; Exit }
Catch { LogErr $_.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) )
{
Log "Updating user: ${Username}" -Name $LogName
LogMsg "Updating user: ${Username}"
Try { Set-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires $true }
Catch { $_.Exception.Message | Log -Error -Name $LogName ; Exit }
Catch { LogErr $_.Exception.Message ; Exit }
}
Else
{
Log "Creating user: ${Username}" -Name $LogName
LogMsg "Creating user: ${Username}"
Try { New-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires $true }
Catch { $_.Exception.Message | Log -Error -Name $LogName ; Exit }
Catch { LogErr $_.Exception.Message ; Exit }
}
If ( !($(Get-LocalGroupMember -Group "Administrators" -Member $Username -ErrorAction SilentlyContinue)) )
{
Log "Adding ""${Username}"" to local Administrators group" -Name $LogName
LogMsg "Adding ""${Username}"" to local Administrators group"
Try { Add-LocalGroupMember -Group "Administrators" -Member $Username }
Catch { $_.Exception.Message | Log -Error -Name $LogName ; Exit }
Catch { LogErr $_.Exception.Message ; Exit }
}
}
Else { Log "The local user account ""${Username}"" cannot be created on a domain controller" -Name $LogName }
Else { LogMsg "The local user account ""${Username}"" cannot be created on a domain controller" }
}
Else { Log "Cannot create a local administrator account without administrative privileges" -Name $LogName }
Else { LogMsg "Cannot create a local administrator account without administrative privileges" }
## TODO: Hide account from logon screen
#Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"
}