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