3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PS1形式が実行できない環境で管理者権限でPSスクリプトを実行する方法

Posted at

これは?

グループポリシーなどによって制御されている環境下だけどPS1ファイルを管理者権限で実行したい! という方向け(ごく少数)の技術共有となります。

実際のコード

test.bat
powershell -NoProfile -ExecutionPolicy unrestricted -Command "Start-Process PowerShell.exe -Verb runas 'Get-Content -Raw -Encoding UTF8 ((Split-Path ''%0'' -Parent) + ''\test.txt'') | iex'"

使い方

①任意の名前のbatファイルを作成し上記コードを記述します。
②任意の名前のPS1ファイルをtxt拡張子に変更し、bat内test.txtの部分をTXTファイル名に置き換えます。
③batファイルと同じ階層にTXTファイルを置きます。

注意点

batと同じ階層にTXTファイルを置かない場合

test.bat
powershell -NoProfile -ExecutionPolicy unrestricted -Command "Start-Process PowerShell.exe -Verb runas 'Get-Content -Raw -Encoding UTF8 ""FileFullPath"" | iex'"

カレントディレクトリについて

立ち上がったPowerShellはSystem32をカレントディレクトリとして立ち上がるため、スクリプト内でファイルの読み込み等する際はフルパス記述推奨

最後に

システム管理者向けの共有となります。
これはスクリプトの内容をよく理解している場合にのみ使用すべきであり、不明なスクリプトを実行する際にはスクリプトの内容を検証し、信頼できるものだけを実行するようにしてください。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?