VHD_Update_diskpart

时间:2023-12-20 17:20:02

###################功能说明#####################
###该脚本用来对离线VHD文件更新,导入系统补丁####
################################################

#####################################################实现过程#####################################################
#1.使用Diskpart命令挂载VHD文件,该VHD必须包含系统保留分区,OS默认所在第二个分区 #
#2.使用DISM命令将补丁更新(.cab、.msu)导入到VHD文件中,并将日志输出到当前目录下 #
#3.卸载VHD文件 #
##################################################################################################################

#指定VHD文件存放位置
$VHD_File = "e:\vhd\test\win2008r2.vhdx"
#指定补丁文件存放位置
$Updates_Folder = "E:\vhd\Kb"
#指定VHD挂载后的OS分区盘符
$DriveLetter = "U"

#DISM命令日志存放于当前脚本目录下
$CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf('\')+1)
#$today = Get-Date -UFormat "%Y%m%d"
#如果将dism过程输出到Log,则当前屏幕无显示
#$DISM_Log = $CurrentPath+"Dism_$today.txt"

#使用Diskpart命令挂载VHD ,并指定盘符(注:该VHD必须包含系统保留分区,否则所选择的分区会不正确,需要使用 partition 1)
Function Diskpart_Mount ($DriveLetter,$VHD_File)
{
$now = get-date -format 'yyyyMMddHHmm'
$dpmountcmd_ScriptName = "DPMount_script_" + $now + ".txt"
$dpmountcmd_ScriptPath = Join-Path -Path $CurrentPath -ChildPath $dpmountcmd_ScriptName
$dpmountcmd = @()
$dpmountcmd += ("Select Vdisk file" + " = """ + $VHD_File + """")
$dpmountcmd += ("Attach Vdisk")
#系统保留分区默认为Partition 1,OS所在分区默认为Partition 2
$dpmountcmd += ("Select Partition 2")
$dpmountcmd += ("Assign letter=" + $DriveLetter)
$dpmountcmd += ("Exit")
Set-Content -Path $dpmountcmd_ScriptPath -Value $dpmountcmd
Diskpart -s $dpmountcmd_ScriptPath
}

#使用Diskpart命令卸载VHD
Function Diskpart_Unmount ($VHD_File)
{
$now = get-date -format 'yyyyMMddHHmm'
$dpunmountcmd_ScriptName = "DPUnmount_script_" + $now + ".txt"
$dpunmountcmd_ScriptPath = Join-Path -Path $CurrentPath -ChildPath $dpunmountcmd_ScriptName
$dpunmountcmd = @()
$dpunmountcmd += ("Select Vdisk file" + " = """ + $VHD_File + """")
$dpunmountcmd += ("Detach Vdisk")
$dpunmountcmd += ("Exit")
Set-Content -Path $dpunmountcmd_ScriptPath -Value $dpunmountcmd
Diskpart -s $dpunmountcmd_ScriptPath
}

#生成添加补丁更新的DISM命令
Function Add_Updates ($Updates_Folder)
{
$Updates = gci $Updates_Folder |? {$_.Extension -eq ".msu" -or $_.Extension -eq ".cab"} |Select Fullname
Foreach ($Update in $Updates)
{
$DISMcmd += "dism /Image:" + $letter + " /Add-Package /PackagePath:" + $Update.Fullname + " /IgnoreCheck" + "`n"
}
Return $DISMcmd
}

#挂载VHD文件,导入补丁更新
Diskpart_Mount $DriveLetter $VHD_File
#判断所指定的分区是否为OS所在分区
$letter = $DriveLetter + ":\"
$Path_Win = Join-Path -Path $letter -ChildPath "Windows"
$Path_ProgramFiles = Join-Path -Path $letter -ChildPath "Program Files"
If ( (Test-Path -Path $Path_Win) -and ( Test-Path -Path $Path_ProgramFiles) )
{
$DISMcmds = Add_Updates $Updates_Folder
$DISMcmds
Invoke-Expression $DISMcmds #|Out-File $DISM_Log -Append
Diskpart_Unmount $VHD_File
}
Else
{
$ws = New-Object -ComObject WScript.Shell
$ws.popup("VHD盘符不正确,请重新指定!")
}

################################

补丁更新完成后,如果某些补丁并未提示“不适用”,显示已成功,也有可能是真的不适用该系统,无法安装成功,如下图中的KB975560和KB2425227两个补丁在更新完成后系统中并未找到,直接安装这两个补丁,提示不适用。

VHD_Update_diskpart