バッチファイルを管理者権限で実行する(ネットワークドライブ対応版)


Windows 8.1, Windows 10 で動作確認しました。

文字コードは ANSI で保存して下さい。

AcroRdrDC_Install.bat
@echo off
for /f "usebackq tokens=2 delims=:" %%A in (`chcp`) do (set "ACTIVE_CP=%%A")
chcp 932 >nul
setlocal

set /a Arg_R=0
set /a Arg_S=0
for %%a in (%*) do (
    if /i %%a==/R (set /a Arg_R=1)
    if /i %%a==/S (set /a Arg_S=1)
)

:checkMandatoryLevel
    for /f "usebackq" %%A in (`powershell -NoProfile -Command "([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)"`) do set "ELEVATED=%%A"
    if /i "%ELEVATED%"=="False" (
        if %Arg_R% neq 1 goto runas
        goto exit1
    )

:admins
    echo Adobe Acrobat Reader DC - Japanese 21.005.20048 のインストール...
    "%~dp0AcroRdrDC2100520048_ja_JP.exe" /sPB /rs

REM echo Adobe Acrobat Reader DC - Japanese 21.001.20150 へのアップデート...
REM msiexec /update "%~dp0AcroRdrDCUpd2100120150.msp" /passive /norestart

    echo onlineservices_Disabled...
    reg import "%~dp0AdobeAcrobatReaderDC_onlineservices_Disabled.reg"

    set TARGET_F="%PUBLIC%\Desktop\Acrobat Reader DC.lnk"
    if exist %TARGET_F% (
        echo パブリック デスクトップのショートカットを削除...
        del %TARGET_F%
    )

    if %Arg_S% equ 0 pause

    goto exit1

:runas
    set "GetCredentialMessage=次にアクセスするための資格情報を入力してください:"
    chcp 65001 >nul
    echo Administrator privileges are required to run this file. [Process requires privileges]
    set "FP=%~f0"
    set PS_NWDriveToUNC=^
        $FP = $env:FP;^
        $FP_Qualifier = Split-Path -Path $FP -Qualifier 2^> $null;^
        if ($FP_Qualifier -ne $null) {^
            $FP_ProviderName = (Get-WMIObject -Class Win32_MappedLogicalDisk -Filter \"DeviceID ^= '$FP_Qualifier'\").ProviderName;^
            if ($FP_ProviderName -ne $null) {^
                $FP_NoQualifier = Split-Path -Path $FP -NoQualifier;^
                $FP_UNCPath = Join-Path -Path $FP_ProviderName -ChildPath $FP_NoQualifier;^
            }^
        }^
        if ($FP_UNCPath -eq $null) {^
            $ReturnValue = $FP;^
        } else {^
            $ReturnValue = $FP_UNCPath;^
        }^
        $ECharacters = '`^^ ' + [char]0x3000 + '^&(),;!%%=';^
        $EQuotationMarks = '''' + [char]0x2018 + [char]0x2019 + [char]0x201A + [char]0x201B;^
        $ReturnValue = $ReturnValue -replace \"^([$ECharacters]^)\", '^^$1';^
        $ReturnValue = $ReturnValue -replace \"^([$EQuotationMarks]^)\", '$1$1$1$1';^
        return $ReturnValue;

    for /f "usebackq tokens=*" %%A in (`powershell -NoProfile -Command "%PS_NWDriveToUNC%"`) do (set "EFP=%%A")

    set PS_RunAs=^
        $Arg_S = %Arg_S%;^
        $FP = ''\"%EFP%\"'';^
        if (($Arg_S -eq 0) -and [bool]([System.Uri]$FP).IsUnc -and !(Test-Path -LiteralPath $FP -PathType Leaf)) {^
            $cred = Get-Credential -Message \"%GetCredentialMessage%` $FP\";^
            New-PSDrive -Name TargetUNCPath -PSProvider FileSystem -Root (Split-Path -Path $FP -Parent) -Credential $cred;^
        }^
        $ArguList = ''\"/s /c \"\"\"\"\"\"'' + $FP + ''\"\"\"\"\"\" %* /R\"'';^
        Start-Process -FilePath ''%SystemRoot%\System32\cmd.exe'' -ArgumentList $ArguList;

    powershell -NoProfile -Command Start-Process -WindowStyle Minimized -Verb RunAs -FilePath powershell -ArgumentList '-NoProfile -Command "%PS_RunAs%"'

    goto exit1

:exit1
    chcp %ACTIVE_CP% >nul

参考