ren New-LocalUser to New-LocalAccount
minor updates to New-LocalAccount
This commit is contained in:
@@ -693,46 +693,38 @@ Function IsRunning ([Parameter(ValueFromPipeline=$true)][string]$Name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Function to create a local user account
|
# Function to create a local user account
|
||||||
Function New-LocalUser {
|
Function New-LocalAccount {
|
||||||
param(
|
param(
|
||||||
[Parameter(Mandatory=$true)][string]$Username,
|
[Parameter(Mandatory=$true)][string]$Username,
|
||||||
[Parameter(Mandatory=$true)][string]$Password,
|
[Parameter(Mandatory=$true)][SecureString]$Password,
|
||||||
[Parameter(Mandatory=$true)][string]$FullName,
|
[Parameter(Mandatory=$true)][string]$FullName,
|
||||||
[Parameter(Mandatory=$true)][string]$Description,
|
[Parameter(Mandatory=$true)][string]$Description,
|
||||||
[Parameter(Mandatory=$false)][switch]$MakeAdmin=$false,
|
[Parameter(Mandatory=$false)][switch]$MakeAdmin=$false,
|
||||||
[Parameter(Mandatory=$false)][switch]$Hide=$false
|
[Parameter(Mandatory=$false)][switch]$Hide=$false
|
||||||
)
|
)
|
||||||
|
If ( !IsAdmin ) {
|
||||||
# Secure supplied password
|
Write-Output "Cannot create or modify a local account without administrative privileges"
|
||||||
Try { $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force ; $Password = $null }
|
Return
|
||||||
Catch { Write-Error $_.Exception.Message ; Exit }
|
}
|
||||||
If ( IsAdmin )
|
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"
|
||||||
If ( $(Get-CimInstance -ClassName Win32_OperatingSystem -Property ProductType | Select-Object -ExpandProperty ProductType) -ne "2" )
|
Return
|
||||||
{
|
}
|
||||||
If ( $(Get-LocalUser -Name $Username -ErrorAction SilentlyContinue) )
|
If ( $(Get-LocalUser -Name $Username -ErrorAction SilentlyContinue) ) {
|
||||||
{
|
Write-Output "Updating user: ${Username}"
|
||||||
Write-Output "Updating user: ${Username}"
|
Try { Set-LocalUser -Name "${Username}" -Password $SecurePassword -FullName "${FullName}" -Description "${Description}" -AccountNeverExpires -PasswordNeverExpires }
|
||||||
Try { Set-LocalUser -Name $Username -Password $SecurePassword -FullName $FullName -Description $Description -AccountNeverExpires -PasswordNeverExpires }
|
Catch { Write-Error $_.Exception.Message ; Exit }
|
||||||
Catch { Write-Error $_.Exception.Message ; Exit }
|
}
|
||||||
}
|
Else {
|
||||||
Else
|
Write-Output "Creating user: ${Username}"
|
||||||
{
|
Try { New-LocalUser -Name "${Username}" -Password $SecurePassword -FullName "${FullName}" -Description "${Description}" -AccountNeverExpires -PasswordNeverExpires }
|
||||||
Write-Output "Creating user: ${Username}"
|
Catch { Write-Error $_.Exception.Message ; Exit }
|
||||||
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"
|
||||||
If ( (-not($(Get-LocalGroupMember -Group "Administrators" -Member $Username -ErrorAction SilentlyContinue))) -and $MakeAdmin )
|
Try { Add-LocalGroupMember -Name "Administrators" -Member $(Get-LocalUser -Name "${Username}") }
|
||||||
{
|
Catch { Write-Error $_.Exception.Message ; Exit }
|
||||||
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" }
|
|
||||||
}
|
}
|
||||||
Else { Write-Output "Cannot create or modify a local account without administrative privileges" }
|
|
||||||
|
|
||||||
If ( $Hide ) {
|
If ( $Hide ) {
|
||||||
# TODO: Hide account from logon screen
|
# TODO: Hide account from logon screen
|
||||||
#Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"
|
#Get-ChildItem "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"
|
||||||
|
|||||||
Reference in New Issue
Block a user