# Version 1.0 - 2018/02/08
###########################################
# Disclaimer
#
# The sample script is not supported under any standard support program or service. The sample script is provided
# AS IS without warranty of any kind.
#
#=================================================
# Declaring Functions
#=================================================
function GetSiteCode {
PARAM (
[String] $Server = "."
)
$SiteCodes = Get-WmiObject -Namespace "root\sms" -Class SMS_ProviderLocation -ComputerName $Server
ForEach-Object {
If ($_.ProviderForLocalSite) {
$siteCode = $_.SiteCode
}
}
return $siteCode.ToLower()
}
Function Export-BootImageDrivers {
PARAM (
[Parameter(Mandatory=$true)]
[String] $ImageId,
[Parameter(Mandatory=$true)]
[String] $ExportXml
)
$drivers = @{}
(Get-CMBootImage -Id $ImageId).ReferencedDrivers | ForEach-Object {
Write-Verbose "Found driver ID - $($_.Id)"
$drivers.Add($_.Id, $_.SourcePath)
}
$drivers | Export-Clixml -Path $XMLFile
}
Function Import-BootImageDrivers {
PARAM (
[Parameter(Mandatory=$true)]
[String] $ImageId,
[Parameter(Mandatory=$true)]
[String] $ExportXml
)
$BootImage = Get-CMBootImage -Id $ImageId
$drivers = Import-Clixml -Path $ExportXml
$drivers.GetEnumerator() | ForEach-Object {
Write-Verbose "Adding driver ID - $($_.Name)"
Set-CMDriver -Id $_.Name -AddBootImagePackage $BootImage -UpdateDistributionPointsforBootImagePackage $false -Force
}
}
#=================================================
# Main Script
#=================================================
#Get Site Code
$SiteCode = GetSiteCode($SiteServer)
#Import the CM Powershell cmdlets
if (-not (Test-Path -Path $SiteCode)) {
Write-Verbose "$(Get-Date): CM12 module has not been imported yet, will import it now."
Import-Module ($env:SMS_ADMIN_UI_PATH.Substring(0, $env:SMS_ADMIN_UI_PATH.Length - 5) + '\ConfigurationManager.psd1') | Out-Null
}
#CM cmdlets need to be run from the CM drive
Set-Location "$($SiteCode):" | Out-Null
if (-not (Get-PSDrive -Name $SiteCode)) {
Write-Error "There was a problem loading the Configuration Manager powershell module and accessing the sites PSDrive."
exit 1
}
# Boot Images
Write-Host "########### --- GET BOOT IMAGES' DRIVERS --- ###########" -ForegroundColor Yellow
#Get all boot images
$CMObjects=Get-CMBootImage
#Import Drivers when xml file exists
foreach ($CMObject in $CMObjects) {
# Create xml file name for the boot image
$XMLFile = "C:\Temp\ExportBootDrivers_$($CMObject.Name).xml"
# Check if the xml file exists. When exists->import otherwise export the integrated driver infos
switch (Test-Path -Path $XMLFile) {
$true {
$Msg = "Would you like to import drivers from $XMLFile into the $($CMObject.Name) boot image?"
$msgBoxInput = [System.Windows.MessageBox]::Show($Msg, 'Driver Import', 'YesNo', 'Question')
if ($msgBoxInput -eq "Yes") {
Import-BootImageDrivers -ImageId $CMObject.PackageID -ExportXml $XMLFile
}
Write-Host "true"
}
$false {
$BootDrivers = (Get-CMBootImage -Id $CMObject.PackageID).ReferencedDrivers
if($BootDrivers.Count -gt 0) {
New-Item -Path $XMLFile -ItemType File | Out-Null
Export-BootImageDrivers -ImageId $CMObject.PackageID -ExportXml $XMLFile
}
}
}
}