ps1実行時に自動的に昇格
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Administrators")) {
Start-Process powershell.exe "-File `"$PSCommandPath`"" -Verb RunAs; exit
}
コードの解説
| コード | 意味 |
| if ( ! ( ~~~ ) ) | もし(~~~)でなければ |
| [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity] ::GetCurrent() | 現在の実行権限を確認 |
| .IsInRole(“Administrators”) | 管理者権限 |
| Start-Process powershell.exe | PowerShellを新しく始まる |
“-File "$PSCommandPath“ | 私自身のフルパスを指定 |
| -Verb RunAs | 管理者権限 |
| ; exit | 現在のスクリプトを終了 |

コメント