コード署名埋め込みメニューの追加と関連付け
PowerShellのバッチファイルに相当する、*.ps1ですが、
これってインストール時に関連づけされません。
なので、関連付けを行うプログラムとコンテキストメニューに
スクリプトファイルを変更した時に署名を埋め込むコンテキストメニューを追加しました。
コンテキストメニューの方は、まずコード署名を作っておいて下さい。
- .ps1ファイルを作成したら、右クリックして、PS1変換を選び署名を埋め込みます。
- そしたら.ps1をダブルクリックして実行したいところですが、vistaではUACの関係で制限がかかります。
- vistaでやるときは、私の場合、あふやDYNAなどUACにて起動したアプリから関連付け実行してます。
- もしくは、powershellを起動し、そこ上でxxxx.ps1
って感じで起動して下さい。
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace ShellExtensionAddPS 6 { 7 class Program 8 { 9 private static readonly string powershell = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"; 10 11 static void Main(string[] args) 12 { 13 RegistContextMenu(); 14 RegistFileType(); 15 } 16 17 /// <summary> 18 /// コンテキストメニューに追加 19 /// </summary> 20 static void RegistContextMenu() 21 { 22 //実行するコマンドライン 23 string commandline = 24 powershell + @" -command ""& {Set-AuthenticodeSignature ""%1"" @(Get-ChildItem cert:\CurrentUser\My -codesigning)[0]}"""; 25 //説明(エクスプローラのコンテキストメニューに表示される) 26 string description = "PS1変換"; 27 28 //フォルダへの関連付けを行う 29 Microsoft.Win32.RegistryKey regkey = 30 Microsoft.Win32.Registry.ClassesRoot.CreateSubKey( "*\\shell\\" + description + "\\command" ); 31 regkey.SetValue("", commandline); 32 regkey.Close(); 33 } 34 35 /// <summary> 36 /// 関連付け 37 /// </summary> 38 static void RegistFileType() 39 { 40 //関連付ける拡張子 41 string extension = ".ps1"; 42 //実行するコマンドライン 43 string commandline = powershell + @" ""%1"""; 44 //ファイルタイプ名 45 string fileType = "PowerShell"; 46 //説明(必要なし) 47 string description = "PowerShellスクリプトファイル"; 48 //動詞 49 string verb = "open"; 50 //動詞の説明(エクスプローラのコンテキストメニューに表示される) 51 //(必要なし) 52 string verb_description = "PowerShellで実行(&O)"; 53 //アイコンのパスとインデックス 54 string iconPath = powershell; 55 int iconIndex = 0; 56 57 //ファイルタイプを登録 58 Microsoft.Win32.RegistryKey regkey = 59 Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(extension); 60 regkey.SetValue("", fileType); 61 regkey.Close(); 62 63 //ファイルタイプとその説明を登録 64 Microsoft.Win32.RegistryKey shellkey = 65 Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(fileType); 66 shellkey.SetValue("", description); 67 68 //動詞とその説明を登録 69 shellkey = shellkey.CreateSubKey("shell\\" + verb); 70 shellkey.SetValue("", verb_description); 71 72 //コマンドラインを登録 73 shellkey = shellkey.CreateSubKey("command"); 74 shellkey.SetValue("", commandline); 75 shellkey.Close(); 76 77 //アイコンの登録 78 Microsoft.Win32.RegistryKey iconkey = 79 Microsoft.Win32.Registry.ClassesRoot.CreateSubKey( 80 fileType + "\\DefaultIcon"); 81 iconkey.SetValue("", iconPath + "," + iconIndex.ToString()); 82 iconkey.Close(); 83 } 84 } 85 }