diff --git a/Tools.ps1 b/Tools.ps1 index c3cfde6..59eeb2a 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -609,5 +609,50 @@ function IsRunning ([Parameter(ValueFromPipeline=$true)][string]$Name) { If ( $RunningInstances ) { Return $true } Else { Return $false } } +# Function to create a local user account +function Create-LocalUser { + param( + [Parameter(Mandatory=$true)][string]$Username, + [Parameter(Mandatory=$true)][string]$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 { 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) ) + { + LogMsg "Updating user: ${Username}" + Try { Set-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires } + Catch { LogErr $_.Exception.Message ; Exit } + } + Else + { + LogMsg "Creating user: ${Username}" + Try { New-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires } + Catch { LogErr $_.Exception.Message ; Exit } + } + If ( (-not($(Get-LocalGroupMember -Group "Administrators" -Member $Username -ErrorAction SilentlyContinue))) -and ($MakeAdmin) ) + { + LogMsg "Adding ""${Username}"" to local Administrators group" + Try { Add-LocalGroupMember -Group "Administrators" -Member $Username } + Catch { LogErr $_.Exception.Message ; Exit } + } + } + Else { LogMsg "Cannot create or modify local accounts on a domain controller" } + } + Else { LogMsg "Cannot create or modify a local account without administrative privileges" } + + ## TODO: Hide account from logon screen + #Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" +} + Setup-MSPDirectories Setup-LogFile \ No newline at end of file