ken_nogi/PAD/UserCreate/GENKI-SANO-PC.ps1
Kenichiro NOGI 88a402ce0f up
2026-07-10 18:13:30 +09:00

44 lines
1.9 KiB
PowerShell

$ErrorActionPreference = 'Stop'
$targetComputerName = 'GENKI-SANO-PC'
$loginId = 'genki.sano'
$plainPassword = 'Next0710#'
$displayName = '佐野 元希'
$groups = 'Users,Administrators'.Split(',') | ForEach-Object { $_.Trim() } | Where-Object { $_ }
Write-Host "設定開始: $targetComputerName / $loginId"
if ($env:COMPUTERNAME -ne $targetComputerName) {
Rename-Computer -NewName $targetComputerName -Force
Write-Host "コンピュータ名を '$targetComputerName' に変更しました。反映には再起動が必要です。"
} else {
Write-Host "コンピュータ名は既に '$targetComputerName' です。"
}
$securePassword = ConvertTo-SecureString $plainPassword -AsPlainText -Force
$existingUser = Get-LocalUser -Name $loginId -ErrorAction SilentlyContinue
if ($null -eq $existingUser) {
New-LocalUser -Name $loginId -Password $securePassword -FullName $displayName -Description $displayName -ErrorAction Stop | Out-Null
Write-Host "ローカルユーザー '$loginId' を作成しました。"
} else {
Set-LocalUser -Name $loginId -FullName $displayName -Description $displayName -ErrorAction Stop
& net.exe user $loginId $plainPassword /fullname:"$displayName" | Out-Null
Write-Host "ローカルユーザー '$loginId' を更新しました。"
}
foreach ($group in $groups) {
try {
Add-LocalGroupMember -Group $group -Member $loginId -ErrorAction Stop
Write-Host "'$loginId' を '$group' に追加しました。"
} catch {
if ($_.Exception.Message -match 'The specified account name is already a member of the group|既にメンバー') {
Write-Host "'$loginId' は既に '$group' に所属しています。"
} else {
throw
}
}
}
Write-Host '設定が完了しました。'