42 lines
2.1 KiB
PowerShell
42 lines
2.1 KiB
PowerShell
#Requires -Version 5.1
|
|
|
|
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 { $Password = ConvertTo-SecureString $Password -AsPlainText -Force }
|
|
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 $User -ErrorAction SilentlyContinue) )
|
|
{
|
|
Log "Updating user: ${Username}" -Name $LogName
|
|
Try { Get-LocalUser -Name $Username | Set-LocalUser -Password $Password -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires -UserMayNotChangePassword }
|
|
Catch { $_.Exception.Message | Log -Error -Name $LogName ; Exit }
|
|
}
|
|
Else
|
|
{
|
|
Log "Creating user: ${Username}" -Name $LogName
|
|
Try { New-LocalUser -Name $Username -Password $Password -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires -UserMayNotChangePassword }
|
|
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 ""${User}"" cannot be created on a domain controller" -Name $LogName }
|
|
}
|
|
Else { Log "Cannot create a local user account without administrative privileges" -Name $LogName }
|
|
|
|
#Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"
|
|
|
|
#[Environment]::Is64BitProcess |