Param($PackageDir, $LogDir) if ($PSBoundParameters.Count -ne 2) { Write-host "Error: -PackageDir -LogDir " ;exit 1} If ( -not (Test-Path $packagedir)) { Write-host "Error: -PackageDir does not exist." ;exit 1} If ( -not (Test-Path $LogDir)) { Write-host "Error: -LogDir does not exist." ;exit 1} function Write-Log { param ( [string]$Message, [string]$LogPath = "$env:TEMP\onlinepatch.log" ) $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $entry = "[$timestamp] $Message" Add-Content -Path $LogPath -Value $entry } $LogPath = "$LogDir\$(hostname)_appupdate.log" if (Test-Path $logPath) { $logTime = (get-item $logPath).LastWriteTime } else { $logTime = get-Date("1900/1/1") } Write-Log -Message "Start appupdate.ps1" -LogPath $LogPath $edgever = (Get-ItemProperty "${env:ProgramFiles(x86)}\Microsoft\Edge\Application\msedge.exe").VersionInfo.ProductVersion #$edgever = (Get-AppXPackage Microsoft.MicrosoftEdge.Stable).Version Write-Log -Message "Info: Current Edge Version: $($edgever)" -LogPath $LogPath $packages = (Get-ChildItem -Path $packagedir| where {$_.extension -eq ".msi"} | Sort Name | %{$_.FullName}) $success = $true if ($packages.Count -gt 0){ foreach ($package in $packages){ if ((get-item $package).LastWriteTime -lt $logTime) { $outmsg = "It was already installed." Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath exit 0 } } $outmsg = "Installation of updates (msi) is starting... " Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath foreach ($package in $packages){ Start-Process msiexec.exe -Wait -ArgumentList "/i $package /qn" if (!($LASTEXITCODE -eq 0)) { $success = $false $package = "+ " + (Get-Item $package).Name + ": Failed" } else { $package = "+ " + (Get-Item $package).Name + ": Success" } Write-Output $package Write-Log -Message $package -LogPath $LogPath } $outmsg = "Finished installing updates." Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath if ($success) { $outmsg = "Info: All updates were successfully installed." $edgever = (Get-ItemProperty "${env:ProgramFiles(x86)}\Microsoft\Edge\Application\msedge.exe").VersionInfo.ProductVersion Write-Log -Message "Info: New Edge Version: $($edgever)" -LogPath $LogPath } else { $outmsg = "Info: There is a failed update." } } else { $outmsg = "There are no app updates (msi)." } Write-Output $outmsg Write-Log -Message $outmsg -LogPath $LogPath $packages = (Get-ChildItem -Path $packagedir| where {$_.extension -eq ".exe"} | Sort Name | %{$_.FullName}) $success = $true if ($packages.Count -gt 0){ $outmsg = "Installation of updates (exe) is starting... " Write-Log -Message $outmsg -LogPath $LogPath foreach ($package in $packages){ Copy-Item $package -Destination "$env:TEMP\myinstaller.exe" Start-Process "$env:TEMP\myinstaller.exe" -Wait -ArgumentList "/silent /install" if (!($LASTEXITCODE -eq 0)) { $success = $false $package = "+ " + (Get-Item $package).Name + ": Failed" } else { $package = "+ " + (Get-Item $package).Name + ": Success" } Write-Output $package Write-Log -Message $package -LogPath $LogPath Remove-Item "$env:TEMP\myinstaller.exe" } $outmsg = "Finished installing updates." Write-Log -Message $outmsg -LogPath $LogPath if ($success) { $outmsg = "Info: All updates were successfully installed." } else { $outmsg = "Info: There are failed updates." } } else { $outmsg = "There are no app updates (exe)." } Write-Log -Message $outmsg -LogPath $LogPath exit 0