$UpdateXML = "C:\Users\Theone\Downloads\offlinescan\updates.xml"
$toFolder = "C:\Users\Theone\Downloads\offlinescan\"
$installFile = $toFolder +"\_Install.bat"
#Initialize webclient for downloading files
$webclient = New-Object Net.Webclient
$webClient.UseDefaultCredentials = $true
# Get the content of the XML file
$Updates = [xml](Get-Content $UpdateXML)
"@Echo Off" | Out-File $installFile
"REM This will install all patches" | Out-File $installFile -Append
foreach ($Check in $Updates.XMLOut.Check)
{
Write-Host "Checking for", $Check.Name
Write-Host $Check.Advice.ToString()
#Checking for files to download
foreach ($UpdateData in $Check.Detail.UpdateData)
{
if ($UpdateData.IsInstalled -eq $false)
{
Write-Host "Download the file for KB", $UpdateData.KBID
Write-Host "Starting download ", $UpdateData.Title, "."
$url = [URI]$UpdateData.References.DownloadURL
$fileName = $url.Segments[$url.Segments.Count – 1]
$toFile = $toFolder +"\"+ $fileName
#Below line can be commented IF you do not want to download and just create a batch file for Patch installaton, this can be used in non internet connected machines.
$webClient.DownloadFile($url, $toFile)
Write-Host "Done downloading"
"@ECHO Starting installing "+ $fileName | Out-File $installFile -Append
if ($fileName.EndsWith(".msu"))
{
"wusa.exe "+ $fileName + " /quiet /norestart /log:%SystemRoot%\Temp\KB"+$UpdateData.KBID+".log" | Out-File $installFile -Append
}
elseif ($fileName.EndsWith(".cab"))
{
"start /wait pkgmgr.exe /ip /m:"+ $fileName + " /quiet /nostart /l:%SystemRoot%\Temp\KB"+$UpdateData.KBID+".log" | Out-File $installFile -Append
}
else
{
$fileName + " /passive /norestart /log %SystemRoot%\Temp\KB"+$UpdateData.KBID+".log" | Out-File $installFile -Append
}
"@ECHO Installation returned %ERRORLEVEL%" | Out-File $installFile -Append
"@ECHO." | Out-File $installFile -Append
Write-Host
}
}
Write-Host
}