PowerShell.ps1をファイル内で管理者権限で実行するコマンド

PowerShell

コマンド

PowerShellスクリプト内で管理者権限が必要になる場合は、正常に実行されない可能性があり、実行したいコマンド

powershell -NoProfile -ExecutionPolicy unrestricted -Command "Start-Process PowerShell.exe -Verb runas"

ただ、これを実行すると確かに管理者としては起動しますが、別窓であがってきてしまいます。

スクリプト内で記述できるパターン

if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Administrators")) 
{
  Start-Process powershell.exe "-ExecutionPolicy RemoteSigned -File `"$PSCommandPath`"" -Verb RunAs
  exit
}

コメント