$ErrorActionPreference = 'Stop' $targetComputerName = 'YUKINA-EBINA-PC' $loginId = 'yukina.ebina' $plainPassword = 'Next0706#' $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 '設定が完了しました。'