Refactor user creation script
This commit is contained in:
parent
0246633c68
commit
04c6ef125f
1 changed files with 50 additions and 41 deletions
|
@ -9,6 +9,9 @@ $uacDisablerTriggerProperty = "UACDisablerTrigger";
|
||||||
function New-PersonalUser([Context] $context, [string] $userName)
|
function New-PersonalUser([Context] $context, [string] $userName)
|
||||||
{
|
{
|
||||||
$userStageProperty = "UserStage";
|
$userStageProperty = "UserStage";
|
||||||
|
$creationStage = "Create";
|
||||||
|
$disableUACStage = "DisableUAC";
|
||||||
|
$adminRemovalStage = "RemoveAdmin";
|
||||||
|
|
||||||
$null = New-Module {
|
$null = New-Module {
|
||||||
Get-UserStage {
|
Get-UserStage {
|
||||||
|
@ -21,9 +24,12 @@ function New-PersonalUser([Context] $context, [string] $userName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (-not (Get-LocalUser $userName -ErrorAction SilentlyContinue))
|
switch (Get-UserStage) {
|
||||||
{
|
{ $_ -in $null,$creationStage } {
|
||||||
Write-Host "Creating Personal User";
|
Set-UserStage $creationStage;
|
||||||
|
|
||||||
|
if (-not (Get-LocalUser $userName -ErrorAction SilentlyContinue)) {
|
||||||
|
Write-Host "Creating Personal User ``$userName``";
|
||||||
|
|
||||||
while ($true) {
|
while ($true) {
|
||||||
Write-Host (
|
Write-Host (
|
||||||
|
@ -49,7 +55,10 @@ function New-PersonalUser([Context] $context, [string] $userName)
|
||||||
|
|
||||||
Write-Information "Renaming the new User to $userName";
|
Write-Information "Renaming the new User to $userName";
|
||||||
Rename-LocalUser $user $userName;
|
Rename-LocalUser $user $userName;
|
||||||
Add-LocalGroupMember -Group "Administrators" -Member $user && Set-LocalUser $context.AdminName -Password (ConvertTo-SecureString -AsPlainText "Admin") && Disable-LocalUser $context.AdminName;
|
|
||||||
|
Add-LocalGroupMember -Group "Administrators" -Member $user &&
|
||||||
|
Set-LocalUser $context.AdminName -Password (ConvertTo-SecureString -AsPlainText "Admin") &&
|
||||||
|
Disable-LocalUser $context.AdminName;
|
||||||
|
|
||||||
Write-Host "Registering setup script for all new users";
|
Write-Host "Registering setup script for all new users";
|
||||||
$context.RegisterNewUserReboot();
|
$context.RegisterNewUserReboot();
|
||||||
|
@ -59,23 +68,23 @@ function New-PersonalUser([Context] $context, [string] $userName)
|
||||||
|
|
||||||
Write-Information "Disabling Auto login";
|
Write-Information "Disabling Auto login";
|
||||||
$context.RemoveAutologin();
|
$context.RemoveAutologin();
|
||||||
Set-UserStage "DisableUAC";
|
Set-UserStage $disableUACStage;
|
||||||
Restart-Computer -Force;
|
Restart-Computer -Force;
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
switch (Get-UserStage) {
|
$disableUACStage {
|
||||||
"DisableUAC" {
|
|
||||||
Enable-PersonalUserAutologon $context $userName;
|
Enable-PersonalUserAutologon $context $userName;
|
||||||
$context.RegisterReboot();
|
$context.RegisterReboot();
|
||||||
Set-UserStage "RemoveAdmin";
|
Set-UserStage $adminRemovalStage;
|
||||||
Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty);
|
Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
"RemoveAdmin" {
|
$adminRemovalStage {
|
||||||
Write-Information "Removing Admin Account";
|
Write-Information "Removing Admin Account";
|
||||||
Get-CimInstance -ClassName "Win32_UserProfile" -Filter "SID = '$((Get-LocalUser $context.AdminName).SID)'" | Remove-CimInstance;
|
Get-CimInstance -ClassName "Win32_UserProfile" -Filter "SID = '$((Get-LocalUser $context.AdminName).SID)'" | Remove-CimInstance;
|
||||||
$context.Remove($userStageProperty);
|
$context.Remove($userStageProperty);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue