-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-ps1-shortcut.ps1
282 lines (236 loc) · 10.1 KB
/
create-ps1-shortcut.ps1
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<#
.SYNOPSIS
PowerShellスクリプト(.ps1)用のショートカット作成GUIツール
.DESCRIPTION
このスクリプトはPowerShellスクリプト(.ps1)のショートカットを作成するためのGUIツールです。
スクリプト実行はPowerShell Core (pwsh)を優先的に使用し、未インストールの場合はWindows PowerShell (powershell.exe)にフォールバックします。
機能:
- GUIによるps1ファイルの選択
- 選択したスクリプトと同じディレクトリにショートカットを作成
- ショートカット作成後の設定ガイド表示
- 作成したショートカットの保存先を直接開く機能
.PARAMETER Debug
デバッグモードを有効にします。
操作時の詳細なログが表示されます。
.NOTES
ファイル名: create-ps1-shortcut.ps1
作成者: 7rikazhexde
作成日: 2024/12/26
バージョン: 0.1.1
このスクリプトはUTF-8 with BOM エンコーディングで保存してください。
PowerShell実行環境に関する注意事項:
1. PowerShell Core (pwsh)が利用可能な場合、優先的に使用されます
2. PowerShell Coreが未インストールの場合、自動的にWindows PowerShell (powershell.exe)が使用されます
3. PowerShell Coreのインストールを推奨します(https://github.com/PowerShell/PowerShell)
実行時の注意事項:
1. ショートカットをタスクバーにピン留めする場合:
その他のオプションを確認 > タスクバーにピン留めする(K)
2. 実行ポリシーや引数を個別に指定する場合:
プロパティ > ショートカット > リンク先
.EXAMPLE
.\create-ps1-shortcut.ps1
通常モードでGUIを起動し、ショートカットを作成します。
.EXAMPLE
.\create-ps1-shortcut.ps1 -Debug
デバッグモードでGUIを起動し、詳細な操作ログを表示します。
#>
# .NET Frameworkのクラスをロード
[CmdletBinding()]
param()
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
# CmdletBindingによって自動的に提供されるDebugパラメータを使用
if ($PSBoundParameters['Debug']) {
$DebugPreference = 'Continue'
Write-Debug "デバッグモードが有効化されました"
}
function Show-FileDialog {
[CmdletBinding()]
param (
[string]$Filter = "PowerShell Scripts (*.ps1)|*.ps1",
[string]$Title = "Create Shortcut - Select PowerShell Script"
)
Write-Debug "ファイル選択ダイアログを表示: Title=$Title"
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.Filter = $Filter
$dialog.Title = $Title
Write-Debug "ダイアログを表示します"
if ($dialog.ShowDialog() -eq 'OK') {
Write-Debug "選択されたファイル: $($dialog.FileName)"
return $dialog.FileName
}
Write-Debug "ファイル選択がキャンセルされました"
return $null
}
function Get-PowerShellPath {
[CmdletBinding()]
param()
Write-Debug "PowerShellパスを取得"
try {
# pwshが利用可能かチェック
$pwshPath = Get-Command pwsh -ErrorAction SilentlyContinue
if ($pwshPath) {
Write-Debug "PowerShell Core (pwsh)が見つかりました: $($pwshPath.Source)"
return $pwshPath.Source
}
# pwshが見つからない場合はpowershell.exeを使用
Write-Debug "PowerShell Core (pwsh)が見つからないため、Windows PowerShellを使用します"
return "powershell.exe"
}
catch {
Write-Debug "PowerShellパスの取得中にエラーが発生: $($_.Exception.Message)"
return "powershell.exe"
}
}
function New-ScriptShortcut {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$ScriptPath
)
Write-Debug "ショートカット作成を開始: ScriptPath=$ScriptPath"
try {
Write-Debug "WScript.Shellオブジェクトを作成"
$WshShell = New-Object -ComObject WScript.Shell
# ショートカットファイルのパスを設定
$ShortcutPath = [System.IO.Path]::Combine(
[System.IO.Path]::GetDirectoryName($ScriptPath),
[System.IO.Path]::GetFileNameWithoutExtension($ScriptPath) + "_shortcut.lnk"
)
Write-Debug "ショートカットパス: $ShortcutPath"
# ショートカットオブジェクトを作成
Write-Debug "ショートカットオブジェクトを作成"
$Shortcut = $WshShell.CreateShortcut($ShortcutPath)
# PowerShellパスを取得して設定
$psPath = Get-PowerShellPath
Write-Debug "使用するPowerShellパス: $psPath"
# プロパティを設定
Write-Debug "ショートカットのプロパティを設定"
$Shortcut.TargetPath = $psPath
$Shortcut.Arguments = "-File `"$ScriptPath`""
$Shortcut.WorkingDirectory = [System.IO.Path]::GetDirectoryName($ScriptPath)
Write-Debug "ショートカットを保存"
$Shortcut.Save()
Write-Debug "ショートカット作成完了"
return $ShortcutPath
}
catch {
Write-Debug "エラーが発生: $($_.Exception.Message)"
throw "ショートカットの作成に失敗しました: $_"
}
}
function Show-CustomDialog {
[CmdletBinding()]
param (
[string]$ShortcutPath
)
Write-Debug "カスタムダイアログを表示: ShortcutPath=$ShortcutPath"
# WPFのXAML定義
$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ショートカット作成完了" Height="250" Width="600"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="ショートカットを作成しました:" Margin="0,0,0,5"/>
<TextBox Grid.Row="1"
Text="$ShortcutPath"
IsReadOnly="True"
FontFamily="Consolas"
Margin="0,0,0,10"
Padding="5"
Background="#F0F0F0"
TextWrapping="Wrap"/>
<TextBlock Grid.Row="2" Margin="0,0,0,10">
<Run Text="ショートカットをタスクバーから実行する場合は下記を設定してください。"/>
<LineBreak/>
<Run Text="その他のオプションを確認 > タスクバーにピン留めする(K)"/>
<LineBreak/>
</TextBlock>
<TextBlock Grid.Row="3" Margin="0,0,0,10">
<Run Text="実行ポリシーや引数を個別に指定する場合は下記で設定を変更してください。"/>
<LineBreak/>
<Run Text="プロパティ > ショートカット > リンク先"/>
<LineBreak/>
</TextBlock>
<StackPanel Grid.Row="4"
Orientation="Horizontal"
HorizontalAlignment="Center">
<Button Name="OpenFolderButton"
Content="保存先を開く"
Width="100"
Margin="5"/>
<Button Name="CancelButton"
Content="キャンセル"
Width="100"
Margin="5"/>
</StackPanel>
</Grid>
</Window>
"@
Write-Debug "XAMLをロード"
$reader = [System.Xml.XmlNodeReader]::new([xml]$xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
Write-Debug "ボタンコントロールを取得"
$openFolderButton = $window.FindName("OpenFolderButton")
$cancelButton = $window.FindName("CancelButton")
Write-Debug "イベントハンドラを設定"
$openFolderButton.Add_Click({
Write-Debug "保存先を開くボタンがクリックされました"
Start-Process "explorer.exe" -ArgumentList "/select,`"$ShortcutPath`""
$window.Close()
})
$cancelButton.Add_Click({
Write-Debug "キャンセルボタンがクリックされました"
$window.Close()
})
Write-Debug "ダイアログを表示"
$window.ShowDialog() | Out-Null
Write-Debug "ダイアログが閉じられました"
}
# メイン処理
function Main {
[CmdletBinding()]
param()
Write-Debug "メイン処理を開始"
# ファイル選択
$scriptPath = Show-FileDialog
Write-Debug "ファイル選択結果: $scriptPath"
if ($scriptPath) {
try {
# ショートカット作成
Write-Debug "ショートカット作成処理を開始"
$shortcutPath = New-ScriptShortcut -ScriptPath $scriptPath
Write-Debug "ショートカット作成完了: $shortcutPath"
# カスタムダイアログを表示
Write-Debug "完了ダイアログを表示"
Show-CustomDialog -ShortcutPath $shortcutPath
}
catch {
Write-Debug "エラーが発生: $($_.Exception.Message)"
[System.Windows.MessageBox]::Show(
$_.Exception.Message,
"エラー",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Error
)
}
}
Write-Debug "メイン処理を終了"
}
# スクリプト実行
if ($PSBoundParameters['Debug']) {
Write-Debug "スクリプトを開始"
Main
Write-Debug "スクリプトを終了"
} else {
Main
}