-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-ActivationCode.ps1
More file actions
106 lines (95 loc) · 4.85 KB
/
Copy pathNew-ActivationCode.ps1
File metadata and controls
106 lines (95 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# ==============================================================================
# SimpleWiki - マシンバインド・アクティベーションコード発行ツール (管理者用)
# 対応: Windows PowerShell 5.1 / PowerShell 7+
# 文字コード: UTF-8 with BOM
# ==============================================================================
param (
[string]$ApiKey = "",
[string]$MachineId = "",
[string]$Email = "",
[switch]$Legacy
)
$scriptDir = [System.IO.Path]::GetFullPath($PSScriptRoot)
$libDir = Join-Path $scriptDir "lib"
# --- モジュールのロード (lib/WikiSecurity.ps1) ---
. (Join-Path $libDir "WikiSecurity.ps1")
Write-Host "==========================================================" -ForegroundColor Green
Write-Host " SimpleWiki アクティベーションコード生成ツール" -ForegroundColor Green
Write-Host "==========================================================" -ForegroundColor Green
# 1. API キーの入力(未指定時に対話取得)
if ([string]::IsNullOrWhiteSpace($ApiKey)) {
# 既存の config.json から自動検出を試みる
$configPath = Join-Path $scriptDir "config.json"
$defaultKey = ""
if (Test-Path $configPath) {
try {
$cfg = Get-ConfigJson -TargetScriptDir $scriptDir
if ($cfg.rag -and $cfg.rag.apiKey) {
$resolved = Get-ResolvedSecret -SecretValue $cfg.rag.apiKey
if ($resolved) { $defaultKey = $resolved }
}
} catch {
# Suppressed intentionally
}
}
if ($defaultKey) {
$masked = $defaultKey.Substring(0, [Math]::Min(8, $defaultKey.Length)) + "..."
$inputKey = Read-Host "API キーを入力してください (空欄で config.json の既存キー [$masked] を使用)"
$ApiKey = if ([string]::IsNullOrWhiteSpace($inputKey)) { $defaultKey } else { $inputKey }
} else {
while ([string]::IsNullOrWhiteSpace($ApiKey)) {
$ApiKey = Read-Host "API キー (sk-... 等) を入力してください"
}
}
}
# 2. 方式の選択 / マシンIDの入力
$localMachineId = Get-MachineFingerprint
$isLegacy = $Legacy
$MachineId = if ([string]::IsNullOrWhiteSpace($MachineId)) { "" } else { $MachineId.Trim() }
$Email = if ([string]::IsNullOrWhiteSpace($Email)) { "" } else { $Email.Trim() }
if (-not $isLegacy -and [string]::IsNullOrWhiteSpace($MachineId)) {
Write-Host "`n発行タイプを選択してください:" -ForegroundColor Cyan
Write-Host " [1] マシン固有ロック形式 (推奨: 指定PC専用)" -ForegroundColor White
Write-Host " [2] 従来ポータブル形式 (どのPCでも動作する共通コード)" -ForegroundColor White
$typeChoice = Read-Host "選択 (1 または 2 / 既定値: 1)"
if ($typeChoice -eq "2") {
$isLegacy = $true
} else {
$inputMachine = Read-Host "ユーザーのマシンID (例: $localMachineId) を入力してください (空欄で自PCのID)"
$MachineId = if ([string]::IsNullOrWhiteSpace($inputMachine)) { $localMachineId } else { $inputMachine.Trim() }
}
}
if (-not $isLegacy -and [string]::IsNullOrWhiteSpace($MachineId)) {
$MachineId = $localMachineId
}
$activationCode = ""
if ($isLegacy -or $MachineId -eq "PORTABLE" -or $MachineId -eq "ALL") {
$activationCode = Protect-StringAes -PlainText $ApiKey
$null = "従来ポータブル形式 (共通)"
$MachineId = "ALL (どのPCでも動作可能)"
} else {
# 3. メールアドレスの入力(任意)
if ([string]::IsNullOrWhiteSpace($Email)) {
$inputEmail = Read-Host "ユーザーのメールアドレスを入力してください (任意 / なしの場合は Enter)"
$Email = if ([string]::IsNullOrWhiteSpace($inputEmail)) { "" } else { $inputEmail.Trim() }
}
$activationCode = Protect-ActivationCode -ApiKey $ApiKey -MachineId $MachineId -Email $Email
$null = "マシン固有ロック形式"
}
# クリップボードへのコピー試行
try {
Add-Type -AssemblyName System.Windows.Forms -ErrorAction SilentlyContinue
[System.Windows.Forms.Clipboard]::SetText($activationCode)
$clipMsg = " (クリップボードにコピーしました)"
} catch {
$clipMsg = ""
}
Write-Host "`n----------------------------------------------------------" -ForegroundColor Yellow
Write-Host " 対象マシンID : $MachineId" -ForegroundColor Cyan
if ($Email) {
Write-Host " 対象メール : $Email" -ForegroundColor Cyan
}
Write-Host " 発行コード : $activationCode$clipMsg" -ForegroundColor Green
Write-Host "----------------------------------------------------------" -ForegroundColor Yellow
Write-Host "※ ユーザーにこの『発行コード』をお伝えください。" -ForegroundColor White
Write-Host " ユーザー側の設定画面で入力すると自動でアクティベーションされます。`n" -ForegroundColor White