Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions for usage history and compare on next run. #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 139 additions & 8 deletions Invoke-CMApplyDriverPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2077,6 +2077,125 @@ Process {
}
}

function Compare-DriverPackageVersion {
switch ($Script:DeploymentMode) {
"DriverUpdate" {
$NewDriversAvailable = $false
Write-CMLogEntry -Value "[CheckPreviousInstall]: Starting previous install registry check phase." -Severity 1
# Check registry for breadcrumbs left by previous driver update via Modern Driver Management
If (Test-Path -Path HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement) {
$PreviousInstallDate = Get-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" | Select-Object -ExpandProperty InstallDate -ErrorAction SilentlyContinue
$PreviousDeploymentType = Get-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" | Select-Object -ExpandProperty DeploymentType -ErrorAction SilentlyContinue
$PreviousName = Get-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" | Select-Object -ExpandProperty PackageName -ErrorAction SilentlyContinue
$PreviousID = Get-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" | Select-Object -ExpandProperty PackageID -ErrorAction SilentlyContinue
$PreviousVersion = Get-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" | Select-Object -ExpandProperty PackageVersion -ErrorAction SilentlyContinue
}

if ($PreviousInstallDate) { Write-CMLogEntry -Value " - Previous install ran on $($PreviousInstallDate)." -Severity 1 }
if ($PreviousDeploymentType) { Write-CMLogEntry -Value " - Previous install DeploymentType was $($PreviousDeploymentType)." -Severity 1 }
if ($PreviousName) { Write-CMLogEntry -Value " - Previous install used package name $($PreviousName)." -Severity 1 }
if ($PreviousID) {
if ($DriverPackageList[0].PackageID -eq $PreviousID){
Write-CMLogEntry -Value " - Previous install used the same matching available package ID, $($PreviousID)." -Severity 1
Write-CMLogEntry -Value " - Comparing package versions." -Severity 1

if ($PreviousVersion) {
Write-CMLogEntry -Value " - Previous install used package version $($PreviousVersion)." -Severity 1
# Strip version strings of letters and spaces, adding a trailing ".0" to ensure it's a true version string
# Dell uses the letter A and a number
# Lenovo uses an integer of yyyymm from the release date
# Hewlett-Package uses a version with a letter and a number
# Microsoft already uses a version string
$PreviousVersion = (($PreviousVersion -replace ' ','') -replace '[a-z]','') + '.0'
$AvailableVersion = (($DriverPackageList[0].Version -replace ' ','') -replace '[a-z]','') + '.0'

# Compare previously used package to currently available package
if ([System.Version]$AvailableVersion -gt [System.Version]$PreviousVersion) {
Write-CMLogEntry -Value " - The matching available package version, $($DriverPackageList[0].Version), is newer and will be applied" -Severity 1
$NewDriversAvailable = $true
}
else {
Write-CMLogEntry -Value " - Previous Modern Driver Management used the same matching available package version, $($DriverPackageList[0].Version)." -Severity 1
Write-CMLogEntry -Value " - The PC is already up to date, nothing more to do." -Severity 1
}
}
else {
Write-CMLogEntry -Value " - Previous package version data not found." -Severity 1
Write-CMLogEntry -Value " - The matching available package version, $($DriverPackageList[0].Version), will be applied." -Severity 1
$NewDriversAvailable = $true
}
}
else {
Write-CMLogEntry -Value " - The previous install used a different package ID, $($PreviousID)." -Severity 1
Write-CMLogEntry -Value " - The matching available package ID is $($DriverPackageList[0].PackageID) and will be applied." -Severity 1
$NewDriversAvailable = $true
}
}
else {
Write-CMLogEntry -Value " - Previous Modern Driver Management usage data not found." -Severity 1
Write-CMLogEntry -Value " - The matching available package ID is $($DriverPackageList[0].PackageID) and will be applied." -Severity 1
$NewDriversAvailable = $true
}
Write-CMLogEntry -Value "[CheckPreviousInstall]: Completed previous install check phase." -Severity 1
}
default {
$NewDriversAvailable = $true
}
}
$TSEnvironment.Value("NewDriversAvailable") = $NewDriversAvailable
return $NewDriversAvailable
}

function Set-DriverPackageHistory {
# Saving driver package information to registry for future driver update comparison to ensure not running the same driver package version again.
switch ($Script:DeploymentMode) {
"BareMetal" {
# Mount Offline Image registry, save driver package info, unmount image registry
Write-CMLogEntry -Value "[SavePackageInfo]: - Attempting to mount offline image SYSTEM registry." -Severity 1
reg load HKLM\OfflineSOFTWARE C:\Windows\System32\config\SOFTWARE | Out-Null
New-Item -Path HKLM:\OfflineSOFTWARE -Name MSEndpointMgr
New-Item -Path HKLM:\OfflineSOFTWARE\MSEndpointMgr -Name ModernDriverManagement
New-ItemProperty -Path "HKLM:\OfflineSOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "InstallDate" -Value (Get-Date -UFormat %c) -Force -PropertyType String
New-ItemProperty -Path "HKLM:\OfflineSOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "DeploymentType" -Value $Script:DeploymentMode -Force -PropertyType String
New-ItemProperty -Path "HKLM:\OfflineSOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "PackageName" -Value $DriverPackageList[0].Name -Force -PropertyType String
New-ItemProperty -Path "HKLM:\OfflineSOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "PackageID" -Value $DriverPackageList[0].PackageID -Force -PropertyType String
New-ItemProperty -Path "HKLM:\OfflineSOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "PackageVersion" -Value $DriverPackageList[0].Version -Force -PropertyType String
[gc]::Collect()
reg unload HKLM\OfflineSOFTWARE | Out-Null
Write-CMLogEntry -Value "[SavePackageInfo]: - Saved driver package info to registry for future reference." -Severity 1
}
"OSUpgrade" {
# Save driver package info
If (!(Test-Path -Path HKLM:\SOFTWARE\MSEndpointMgr)) { New-Item -Path HKLM:\SOFTWARE -Name MSEndpointMgr }
If (!(Test-Path -Path HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement)) { New-Item -Path HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement }
New-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "InstallDate" -Value (Get-Date -UFormat %c) -Force -PropertyType String
New-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "DeploymentType" -Value $Script:DeploymentMode -Force -PropertyType String
New-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "PackageName" -Value $DriverPackageList[0].Name -Force -PropertyType String
New-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "PackageID" -Value $DriverPackageList[0].PackageID -Force -PropertyType String
New-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "PackageVersion" -Value $DriverPackageList[0].Version -Force -PropertyType String
Write-CMLogEntry -Value "[SavePackageInfo]: - Saved driver package info to registry for future reference." -Severity 1
}
"DriverUpdate" {
# Save driver package info
If (!(Test-Path -Path HKLM:\SOFTWARE\MSEndpointMgr)) { New-Item -Path HKLM:\SOFTWARE -Name MSEndpointMgr }
If (!(Test-Path -Path HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement)) { New-Item -Path HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement }
New-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "InstallDate" -Value (Get-Date -UFormat %c) -Force -PropertyType String
New-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "DeploymentType" -Value $Script:DeploymentMode -Force -PropertyType String
New-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "PackageName" -Value $DriverPackageList[0].Name -Force -PropertyType String
New-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "PackageID" -Value $DriverPackageList[0].PackageID -Force -PropertyType String
New-ItemProperty -Path "HKLM:\SOFTWARE\MSEndpointMgr\ModernDriverManagement" -Name "PackageVersion" -Value $DriverPackageList[0].Version -Force -PropertyType String
Write-CMLogEntry -Value "[SavePackageInfo]: - Saved driver package info to registry for future reference." -Severity 1
}
"PreCache" {
# Do not save driver package info because it has not been installed yet.
}
"PreStage" {
# Do not save driver package info because it has not been installed yet.
}
}
}


Write-CMLogEntry -Value "[ApplyDriverPackage]: Apply Driver Package process initiated" -Severity 1
if ($PSCmdLet.ParameterSetName -like "Debug") {
Write-CMLogEntry -Value " - Apply driver package process initiated in debug mode" -Severity 1
Expand Down Expand Up @@ -2181,18 +2300,30 @@ Process {

# At this point, the code below here is not allowed to be executed in debug mode, as it requires access to the Microsoft.SMS.TSEnvironment COM object
if ($PSCmdLet.ParameterSetName -notlike "Debug") {
Write-CMLogEntry -Value "[DriverPackageDownload]: Starting driver package download phase" -Severity 1

# Checking for previous driver package usage to ensure not trying to upgrade with the same package version again and again.
$NewDriversAvailable = Compare-DriverPackageVersion

if ($NewDriversAvailable -eq $true) {

# Attempt to download the matched driver package content files from distribution point
$DriverPackageContentLocation = Invoke-DownloadDriverPackageContent
Write-CMLogEntry -Value "[DriverPackageDownload]: Starting driver package download phase" -Severity 1

Write-CMLogEntry -Value "[DriverPackageDownload]: Completed driver package download phase" -Severity 1
Write-CMLogEntry -Value "[DriverPackageInstall]: Starting driver package install phase" -Severity 1
# Attempt to download the matched driver package content files from distribution point
$DriverPackageContentLocation = Invoke-DownloadDriverPackageContent

# Depending on deployment type, take action accordingly when applying the driver package files
Install-DriverPackageContent -ContentLocation $DriverPackageContentLocation
Write-CMLogEntry -Value "[DriverPackageDownload]: Completed driver package download phase" -Severity 1
Write-CMLogEntry -Value "[DriverPackageInstall]: Starting driver package install phase" -Severity 1

Write-CMLogEntry -Value "[DriverPackageInstall]: Completed driver package install phase" -Severity 1
# Depending on deployment type, take action accordingly when applying the driver package files
Install-DriverPackageContent -ContentLocation $DriverPackageContentLocation

Write-CMLogEntry -Value "[DriverPackageInstall]: Completed driver package install phase" -Severity 1

Set-DriverPackageHistory
}
else {
Write-CMLogEntry -Value "[CheckPreviousInstall]: Skipping driver upgrade because the PC has already applied the latest driver package currently available." -Severity 1
}
}
else {
Write-CMLogEntry -Value " - Script has successfully completed debug mode" -Severity 1
Expand Down
Loading