From 3e8def09d668fc55bf990478a451c15f620facb5 Mon Sep 17 00:00:00 2001 From: David Yoder Date: Tue, 5 Dec 2023 14:56:12 -0500 Subject: [PATCH] ren New-LocalUser to New-LocalAccount minor updates to New-LocalAccount --- Tools.ps1 | 56 ++++++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/Tools.ps1 b/Tools.ps1 index 73d07b1..a9762ce 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -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"