自動安裝腳本在文章末尾
自動安裝腳本在文章末尾
自動安裝腳本在文章末尾
運行環境:Windows Server 2012 R2
開發環境:Win10
Microsoft .NET Framework 4.6.1離線安裝包:下載地址
需要注意的是win10能運行的腳本在2012上不一定能運行,所以要把寫好的腳本在2012上確保運行。
為了確保安裝,需要提前做一些判斷
一、獲取已安裝軟件列表
這些判斷借鑒了 示例(傳送門) ,可以下載運行試試,但在我的2012不能直接使用
這里 Get-WmiObject -Class "win32_product" 在2012上運行無效
然后百度到使用注冊表獲取安裝的軟件列表 參考鏈接
‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall’ ‘HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall’ ‘HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall’
代碼在這里,但win10運行速度超快😮(比Get-WmiObject快很多),在2012上找不到注冊表目錄

<# .Synopsis Get installed software list by retrieving registry. .DESCRIPTION The function return a installed software list by retrieving registry from below path; 1.'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall' 2.'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall' 3.'HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall' Author: Mosser Lee (http://www.pstips.net/author/mosser/) .EXAMPLE Get-InstalledSoftwares .EXAMPLE Get-InstalledSoftwares | Group-Object Publisher #> function Get-InstalledSoftwares { # # Read registry key as product entity. # function ConvertTo-ProductEntity { param([Microsoft.Win32.RegistryKey]$RegKey) $product = '' | select Name,Publisher,Version $product.Name = $_.GetValue("DisplayName") $product.Publisher = $_.GetValue("Publisher") $product.Version = $_.GetValue("DisplayVersion") if( -not [string]::IsNullOrEmpty($product.Name)){ $product } } $UninstallPaths = @(, # For local machine. 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', # For current user. 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall') # For 32bit softwares that were installed on 64bit operating system. if([Environment]::Is64BitOperatingSystem) { $UninstallPaths += 'HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall' } $UninstallPaths | foreach { Get-ChildItem $_ | foreach { ConvertTo-ProductEntity -RegKey $_ } } }
由於安裝framework之前必須要確認是否存在,無奈偷個懶,也是讀取注冊表🙃
這次直接讀取下邊的注冊表,獲取所有已安裝的framework版本號😃,完美結果
HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full
# 用法
Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' -Name Version
二、安裝進度條顯示
下一個問題就是按軟件安裝時,由於安裝時間很長,容易認為腳本假死,所以加個進度條,但是進度條需要獲取安裝進度,這里未百度到如何獲取安裝程序的進度,所以又偷懶了😂
安裝時無限循環進度條來響應
安裝完成后隱藏進度條

function Show-Progress ($file, $arguments){ $process = Start-Process $file $arguments -Verb RunAs -PassThru for($i = 0; $i -le 100; $i = ($i + 1) % 100) { Write-Progress -Activity "正在安裝" -PercentComplete $i Start-Sleep -Milliseconds 100 if ($process.HasExited) { Write-Progress -Activity "Installer" -Completed if ($process.HasExited) { if ($process.ExitCode -eq 3010) { Write-Host "成功安裝。" } break } } } }
三、安裝離線包錯誤5100
這個問題好解決點,就是系統未安裝更新包(Windows Server 2012 R2 Update (KB2919355)),所以不能安裝高級的framework版本
安裝順序如下
2、clearcompressionflag.exe 下載鏈接
至此,腳本寫好
自動安裝Microsoft .NET Framework 4.6.1的PowerShell腳本代碼
# 自動安裝.net4.6.1 # author:lttr <www.cnblogs.com/GoCircle> # date:2019-08-09 Set-ExecutionPolicy -Force remotesigned # 獲取當前腳本絕對路徑 $path = Split-Path -Parent $MyInvocation.MyCommand.Definition; $Net = "\NDP461-KB3102436-x86-x64-AllOS-ENU.exe"; Set-Location $path; function Show-Progress ($file, $arguments){ $process = Start-Process $file $arguments -Verb RunAs -PassThru for($i = 0; $i -le 100; $i = ($i + 1) % 100) { Write-Progress -Activity "正在安裝 Microsoft .NET Framework 4.6.1" -PercentComplete $i Start-Sleep -Milliseconds 100 if ($process.HasExited) { Write-Progress -Activity "Installer" -Completed if ($process.HasExited) { # 提示安裝狀態 if ($process.ExitCode -eq 3010) { Write-Host "成功安裝Microsoft.NET Framework 4.6.1。" shutdown -r -t 20 }elseif ($process.ExitCode -ne 0) { Write-Warning "安裝過程返回錯誤代碼: $($process.ExitCode)"; }elseIf(((Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' -Name Version).Version | Where-Object {$_ -ge "4.6"}) -eq $null) { Write-Host "成功安裝Microsoft.NET Framework 4.6.1。" shutdown -r -t 20 } else{ Write-Warning "安裝Microsoft.NET Framework 4.6.1失敗,可以在$$file中找到它並手動安裝。"; } break } } } } function InstallNet ($filepath,$filename){ # 檢查當前系統是否已安裝.net4.6.1或更高版本 If(((Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' -Name Version).Version | Where-Object {$_ -ge "4.6"}) -eq $null) { # If($true) { $NetFxPath = $filepath + $filename; #驗證安裝文件是否存在 If(Test-Path -Path $NetFxPath) { Write-Host "正在安裝 Microsoft .NET Framework 4.6.1 ..." Show-Progress $NetFxPath "/q /norestart" } Else { Write-Warning "找不到Microsoft.NET Framework 4.6.1安裝包。" } } Else { Write-Host "這台計算機中已經安裝了 .NET Framework 4.6.1 或版本更高的更新。" } } InstallNet $path $Net