update
This commit is contained in:
+13
-9
@@ -1,5 +1,9 @@
|
||||
#Requires -Version 5.1
|
||||
## 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,
|
||||
@@ -8,22 +12,22 @@ param(
|
||||
)
|
||||
$LogName = $MyInvocation.MyCommand
|
||||
Log "Securing supplied password" -Name $LogName
|
||||
Try { $Password = ConvertTo-SecureString $Password -AsPlainText -Force }
|
||||
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 $User -ErrorAction SilentlyContinue) )
|
||||
If ( $(Get-LocalUser -Name $Username -ErrorAction SilentlyContinue) )
|
||||
{
|
||||
Log "Updating user: ${Username}" -Name $LogName
|
||||
Try { Get-LocalUser -Name $Username | Set-LocalUser -Password $Password -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires -UserMayNotChangePassword }
|
||||
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 $Password -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires -UserMayNotChangePassword }
|
||||
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)) )
|
||||
@@ -33,10 +37,10 @@ If ( IsAdmin )
|
||||
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 "The local user account ""${Username}"" cannot be created on a domain controller" -Name $LogName }
|
||||
}
|
||||
Else { Log "Cannot create a local user account without administrative privileges" -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"
|
||||
|
||||
#[Environment]::Is64BitProcess
|
||||
}
|
||||
Reference in New Issue
Block a user