-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
New-Administrator.ps1
60 lines (50 loc) · 1.45 KB
/
New-Administrator.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<#
.SYNOPSIS
Create a new local admin user.
.PARAMETER Password
The required password of the new local admin account to create, as a SecureString.
.PARAMETER Username
The required username of the new local admin account to create.
.EXAMPLE
New-Administrator -Username foo -Password (ConvertTo-SecureString 'bar' -AsPlainText -Force)
#>
# CmdletBinding adds -Verbose functionality, SupportsShouldProcess adds -WhatIf
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter(Mandatory=$true, Position=1)] [string] $Username,
[Parameter(Mandatory=$true, Position=2)] [securestring] $Password
)
Begin
{
$root = $PSScriptRoot | Split-Path -Parent | Split-Path -Parent
. $root\common.ps1
function CreateAdministrator
{
#$Password = ConvertTo-SecureString $Password -AsPlainText -Force
New-LocalUser $Username -Password $Password -PasswordNeverExpires -Description "Local admin"
Add-LocalGroupMember -Group Administrators -Member $Username
}
}
Process
{
if (!(IsElevated))
{
return
}
if ($Username -eq $env:USERNAME)
{
WriteWarn '... username must be unique'
return
}
$go = Read-Host 'Create local administrator? (y/n) [y]'
if (($go -eq '') -or ($go -eq 'y'))
{
CreateAdministrator
Write-Host
$go = Read-Host "Logout to log back in as $Username`? (y/n) [y]"
if (($go -eq '') -or ($go -eq 'y'))
{
logoff; exit
}
}
}