0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShell スクリプトをバッチファイルにのせて、管理者権限で実行する

Last updated at Posted at 2024-12-24
管理者権限の判定、権限昇格を PowerShell を呼び出して行う
Admin.PowerShell_PS.cmd
@echo off && goto PreCheck
<# -----------------コマンドプロンプト-----------------
  :PreCheck
     PowerShell -NoExit -Command "" ^
       $File = "'%~f0'"; ^
       $CD = ${Env:__CD__}.TrimEnd('\'); ^
       function IsNotProcessElevated { ^
         $Name = """WindowsIdentity""", """WindowsPrincipal""", """WindowsBuiltInRole"""; ^
         $Type = """System.Management.Automation.TypeAccelerators"""; ^
         $Pref = """System.Security.Principal."""; ^
         $Name ^| %%{[PSObject].Assembly.GetType($Type)::Add($_,[Type]::GetType($Pref+$_))}; ^
         $Bool = !([WindowsPrincipal][WindowsIdentity]::GetCurrent()).IsInRole([WindowsBuiltInRole]"""Administrator"""); ^
         $Name ^| %%{[void][PSObject].Assembly.GetType($Type)::Remove($_)}; ^
         If ($Bool){return $true}; $false; ^
       }; ^
       if (IsNotProcessElevated) {  ^
         Write-Host "'%~nx0'": elevating self; ^
         $Exe = """PowerShell"""; ^
         $Opt = """-NoProfile -ExecutionPolicy Bypass -Command """ + ^
                """Set-Location -Path '"`""$CD"`""';""" + ^
                """. '"`""$File"`""' """; ^
         $Dir = $Env:__APPDIR__; ^
         $Shell = New-Object -ComObject """Shell.Application"""; ^
         pause; ^
         $Shell::ShellExecute($Exe, $Opt, $Dir, """RunAs""", 1); ^
       }; ^
       if (IsNotProcessElevated) {EXIT 1}; EXIT 0 ^
     ""
     if errorlevel 1 (EXIT)
  :Excute
     set "dir=%~dp0"
     PowerShell -NoP -Ex Unrestricted "$S=[ScriptBlock]::Create((gc '%~f0'|?{$_.ReadCount -gt 1})-join\"`n\");&$S" "'%dir%'" &exit /b
 --------------------------END------------------------- #>
Param([string] $PSScriptRoot)
###################################
####  Powershell Script Start  ####



####       Script FINISH       ####
###################################
Write-Host "---Script Finish---"
PAUSE
EXIT


CMD を使って管理者権限の有無を判定し、VBS を利用して権限昇格
Admin.PowerShell_VBS.cmd
@echo off && goto PreCheck
<# -----------------コマンドプロンプト-----------------
  :PreCheck
     setlocal
       for /f "tokens=3 delims=\ " %%A in ('whoami /groups^|find "Mandatory Label"') do set LEVEL=%%A
       if not "%LEVEL%"=="High"  goto GETadmin
     goto Excute
  :GETadmin
     endlocal
     echo "%~nx0": elevating self
     del "%temp%\getadmin.vbs"                                    2>NUL
       set vbs=%temp%\getadmin.vbs
       echo Set UAC = CreateObject^("Shell.Application"^)          >> "%vbs%"
       echo Dim stCmd                                              >> "%vbs%"
       echo stCmd = "/c """"%~s0"" " ^& """%~dp0""" ^& Chr(34)     >> "%vbs%"
       echo UAC.ShellExecute "cmd.exe", stCmd, "", "runas", 1      >> "%vbs%"
       pause
       "%temp%\getadmin.vbs"
     del "%temp%\getadmin.vbs"
     goto :eof
  :Excute
     endlocal
     set "dir=%~1"
     PowerShell -NoP -Ex Unrestricted "$S=[ScriptBlock]::Create((gc '%~f0'|?{$_.ReadCount -gt 1})-join\"`n\");&$S" "'%dir%'" &exit /b
 --------------------------END------------------------- #>
Param([string] $PSScriptRoot)
###################################
####  Powershell Script Start  ####



####       Script FINISH       ####
###################################
Write-Host "---Script Finish---"
PAUSE
EXIT
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?