自动安装脚本在文章末尾
自动安装脚本在文章末尾
自动安装脚本在文章末尾
运行环境: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 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
原文地址:https://www.cnblogs.com/GoCircle/p/11225598.html