目錄

Install .NET Framework 3.5 quickly

目錄

快速安裝 .NET Framework 3.5 的方法

首先,您還是必須要有一個 Windows 的安裝光碟或是 ISO 檔,若是 ISO 檔需要先掛載起來。 下面的 script 會假設 Windows 會掛載在某一個 driver letter (D ~ Z),找到後會自動開始安裝。

小記
必須使用管理者權限來執行 command prompt 或是 powershell

batch file 的作法為

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@echo off
prompt $P$G
chcp 437 1>nul
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

cls
echo Administrative permission required. Detecting permissions ...
net session >nul 2>&1
if %errorlevel% == 0 (
    echo Administrative permissions confirmed.
    echo.
) else (
    echo Failure: Current permissions inadequate.
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    exit /B
)

echo Install .NET FrameWork 3.5 for Windows 8.x/10.x
set WindowsDiskDrive=
for %%i in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist %%i:\sources\sxs set WindowsDiskDrive=%%i
if /I NOT [!WindowsDiskDrive!]==[] (
    echo Detected Windows Install disk is !WindowsDiskDrive!
    DISM /Online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:!WindowsDiskDrive!:\sources\sxs
) else (
    echo ERROR. Can't find any Windows install disk.
)

ENDLOCAL
ENDLOCAL

若是習慣使用 Powershell 的可以用下面的 script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# https://serverfault.com/questions/95431/in-a-powershell-script-how-can-i-check-if-im-running-with-administrator-privil
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    write-output "Administrative permissions confirmed."
} else {
    throw "Failure: Current permissions inadequate."
}

# https://stackoverflow.com/questions/53779425/check-if-netfx3-is-enabled-if-not-enable-netfx3
If ((Get-WindowsCapability -Online -Name NetFX3~~~~).State -ne 'Installed') {
    $drvletter = 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    foreach ($drv in $drvletter) {
        if (Test-Path ${drv}:\sources\sxs -PathType Container) {
            Add-WindowsCapability -Online -Name NetFx3~~~~ -Source "${drv}:\sources\sxs"
        }
    }
}

If ((Get-WindowsCapability -Online -Name NetFX3~~~~).State -ne 'Installed') {
    throw "ERROR. Can't find any Windows install disk."
} else {
    write-output "Windows .NET Framework 3.5 installed successfully"
}