Loop user creation until it exists

This commit is contained in:
Manuel Thalmann 2023-06-22 23:36:45 +02:00
parent 686251b0f7
commit 2c1ea94d97

View file

@ -6,17 +6,24 @@ function New-PersonalUser([Context] $context)
if (-not (Get-LocalUser $context.UserName))
{
Write-Host (
[string]::Join(
"`n",
"So... Windows is too dumb to create users which are bound to a Microsoft Account.",
"Thus, you have to do it by yourself.",
"So sorry..."));
while ($true) {
Write-Host (
[string]::Join(
"`n",
"So... Windows is too dumb to create users which are bound to a Microsoft Account.",
"Thus, you have to do it by yourself.",
"So sorry..."));
$users = Get-LocalUser | Select-Object { $_.Name };
Read-Host "Please hit enter once you're done...";
$users = Get-LocalUser | Select-Object { $_.Name };
Read-Host "Please hit enter once you're done...";
$user = Get-LocalUser | Select-Object { -not ($users -contains $_.Name) } | Select-Object -Last -1;
if ($user) {
break;
}
}
$user = Get-LocalUser | Select-Object { -not ($users -contains $_.Name) } | Select-Object -Last;
Rename-LocalUser $user $context.UserName;
Add-LocalGroupMember -Group "Administrators" -Member $user && Set-LocalUser $context.AdminName -Password (ConvertTo-SecureString "Admin");
}