function Add-Wsle-Function { $profilePath = $PROFILE if (!(Test-Path -Path $profilePath)) { New-Item -ItemType File -Path $profilePath -Force | Out-Null } $profileContent = Get-Content -Path $profilePath -ErrorAction SilentlyContinue $functionDef = 'function wsle { param([Parameter(ValueFromRemainingArguments = $true)]$Command) wsl bash -c "$($Command -join '' '')" }' $functionExists = $profileContent -match 'function wsle' if (-not $functionExists) { Add-Content -Path $profilePath -Value $functionDef Write-Output "関数 'wsle' をプロファイルに追加しました。" } else { Write-Output "関数 'wsle' は既にプロファイルに存在します。" } } # 管理者権限の確認と昇格 if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Output "管理者権限が必要です。UACプロンプトを表示します。" Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs exit } Write-Output "管理者権限で実行中です。" # 操作内容の表示と確認 $operations = @" 以下の操作を行います: 1. PowerShellプロファイルに 'wsle' 関数を追加します。 2. wsle.exe のApp Paths登録は不要です(関数のみ)。 "@ Write-Output $operations $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "操作を続行します。" $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "操作をキャンセルします。" $print = New-Object System.Management.Automation.Host.ChoiceDescription "&Print", "実行内容のコードを表示します。" $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no, $print) $code = @" # wsle関数をPowerShellプロファイルに追加 `$profilePath = `$PROFILE if (!(Test-Path -Path `$profilePath)) { New-Item -ItemType File -Path `$profilePath -Force | Out-Null } `$functionDef = '``function wsle { param([Parameter(ValueFromRemainingArguments = `$true)]`$Command) wsl bash -c "`$(`$Command -join '' ''`)" }' if ((Get-Content -Path `$profilePath -ErrorAction SilentlyContinue) -notmatch 'function wsle') { Add-Content -Path `$profilePath -Value `$functionDef } "@ while ($true) { $result = $host.ui.PromptForChoice("操作の確認", "続行しますか?", $options, 0) switch ($result) { 0 { Write-Output "操作を実行します..." Add-Wsle-Function Write-Output "すべての操作が完了しました。PowerShellを再起動すると 'wsle' コマンドが利用できます。" return } 1 { Write-Output "操作をキャンセルしました。" return } 2 { Write-Output "`n実行内容のコード:`n" Write-Output $code Write-Output "" } } }