0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows Serverで自動ログオン(PowerShell)

Last updated at Posted at 2024-09-13

経緯

  • ParallelsなりHyper-VでWindows Server検証環境を動かすときに毎回Ctrl Alt Delでログインするのがめんどくさい。

image.png

2025年再追記

  • Ctrl + Alt + Delキャンセルのコマンド追記しました。
  • サーバーマネージャー自動起動 + IE保護をオフを追記しました。
AutoLogon-WindowsServer.ps1

# --- 自動ログイン無効化処理を最初に追加 ---
$disable = Read-Host "自動ログインを無効にしますか? (Y/N)"
if ($disable -match "^[Yy]$") {
    $WinlogonPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
    $PoliciesPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
    # 自動ログイン関連レジストリを削除または無効化
    Remove-ItemProperty -Path $WinlogonPath -Name "DefaultUserName" -ErrorAction SilentlyContinue
    Remove-ItemProperty -Path $WinlogonPath -Name "DefaultPassword" -ErrorAction SilentlyContinue
    Remove-ItemProperty -Path $WinlogonPath -Name "DefaultDomainName" -ErrorAction SilentlyContinue
    Set-ItemProperty -Path $WinlogonPath -Name "AutoAdminLogon" -Value "0"
    Set-ItemProperty -Path $PoliciesPath -Name "DisableCAD" -Value 0
    Write-Host "自動ログインを無効化しました。"
    $reboot = Read-Host "今すぐ再起動しますか? (Y/N)"
    if ($reboot -match "^[Yy]$") {
        Restart-Computer
    } else {
        Write-Host "再起動は手動で行ってください。"
    }
    exit
}

# ドメイン情報取得
$domainObj = Get-WmiObject Win32_ComputerSystem
if ($domainObj.PartOfDomain) {
    $netbios = $domainObj.Domain
} else {
    $netbios = ""
}

# ローカルユーザー取得
$localUsers = Get-LocalUser | Where-Object { -not $_.Disabled } | Select-Object -ExpandProperty Name
$localUserObjs = $localUsers | ForEach-Object {
    [PSCustomObject]@{
        Display = "$_(ローカルユーザ)"
        UserName = $_
        Type = "local"
    }
}

# キャッシュされたドメインユーザー取得(順序修正済み)
$profileList = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
$domainUsers = @()
foreach ($profile in $profileList) {
    $profilePath = Get-ItemPropertyValue -Path $profile.PSPath -Name "ProfileImagePath" -ErrorAction SilentlyContinue
    if ($profilePath) {
        $base = Split-Path $profilePath -Leaf
        $lastDot = $base.LastIndexOf('.')
        if ($lastDot -gt 0 -and $lastDot -lt ($base.Length - 1)) {
            $user = $base.Substring(0, $lastDot)
            $domain = $base.Substring($lastDot + 1)
            # ローカルユーザーと重複しない場合のみリスト化
            if ($localUsers -notcontains $base -and $domainUsers.UserName -notcontains "$domain\$user") {
                $domainUsers += [PSCustomObject]@{
                    Display = "$domain\$user$domain ドメインユーザ)"
                    UserName = "$domain\$user"
                    Type = "domain"
                }
            }
        }
    }
}

# 統合リスト作成
$allUsers = @($localUserObjs + $domainUsers)

# 番号付きでリスト表示
Write-Host "自動ログイン対象ユーザー一覧:"
for ($i=0; $i -lt $allUsers.Count; $i++) {
    Write-Host "$($i+1). $($allUsers[$i].Display)"
}

# ユーザー選択
do {
    $idx = Read-Host "自動ログインするユーザー番号を入力してください"
} while (-not ($idx -as [int]) -or $idx -lt 1 -or $idx -gt $allUsers.Count)
$selectedUser = $allUsers[$idx-1]

# パスワード入力
$DefaultPassword = Read-Host "パスワードを入力してください" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($DefaultPassword)
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

# レジストリ設定
$WinlogonPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
$PoliciesPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
Set-ItemProperty -Path $WinlogonPath -Name "DefaultUserName" -Value $selectedUser.UserName
Set-ItemProperty -Path $WinlogonPath -Name "DefaultPassword" -Value $UnsecurePassword
Set-ItemProperty -Path $WinlogonPath -Name "AutoAdminLogon" -Value "1"
Set-ItemProperty -Path $PoliciesPath -Name "DisableCAD" -Value 1

# ドメインユーザーの場合はDefaultDomainNameも設定
if ($selectedUser.Type -eq "domain") {
    $domainName = $selectedUser.UserName.Split('\')[0]
    Set-ItemProperty -Path $WinlogonPath -Name "DefaultDomainName" -Value $domainName
}

# サーバーマネージャー自動起動を無効化
$ServerManagerPath = "HKCU:\Software\Microsoft\ServerManager"
if (-not (Test-Path $ServerManagerPath)) {
    New-Item -Path $ServerManagerPath | Out-Null
}
Set-ItemProperty -Path $ServerManagerPath -Name "DoNotOpenServerManagerAtLogon" -Value 1

# IE ESC(IE保護モード)をオフ
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" -Name "IsInstalled" -Value 0
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}" -Name "IsInstalled" -Value 0
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap" -Name "IEHarden" -Value 0

# 再起動確認
$reboot = Read-Host "設定が完了しました。今すぐ再起動しますか? (Y/N)"
if ($reboot -match "^[Yy]$") {
    Restart-Computer
} else {
    Write-Host "再起動は手動で行ってください。"
}

ドメインユーザーでの実行時

これでローカル管理者権限で実行する必要があります。

.cmd
runas /user:%COMPUTERNAME%\Administrator powershell.exe

ポエム

  • ParallelsのVMは再起動が3秒と爆速だったり、スナップショットが秀逸なので、自動ログインにより気軽に検証ができます。
  • 本番環境ではもちろんパスワードを設定しましょう。
  • Parallels Toolsを入れるとMacのTouchIDとの連携もできるようです。親切だなあ。

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?