-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriver-Extract.ps1
202 lines (174 loc) · 7.49 KB
/
Driver-Extract.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#Import Reg Key to set cleanMgr options to clean unused drivers
# TODO: -Add switch to disable sending email notification
# -Build XML file for configuration
# Import XML Configuration Settings
[xml]$ConfigFile = Get-Content "$PSScriptRoot\config.xml"
$RootPath = $ConfigFile.Settings.RootPath
if ($ConfigFile.Settings.CompletedNotification -eq "Email")
{
$SendCompletionEmail = "True"
}
if (!(Test-Path -Path $RootPath))
{
Write-Verbose -Message "[INFO] 1/2: Unable to connect to driver staging directory at: $RootPath"
Write-Verbose -Message "[INFO] 2/2: Proceeding using $PSScriptRoot as staging directory, please move driver folder when complete"
$RootPath = $PSScriptRoot
}
Write-Verbose -Message "[INFO] Running cleanmgr.exe and cleaning up unused driver packages"
New-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Device Driver Packages" -Name "StateFlags0003" -PropertyType Dword -Value 2 -Force
Start-Process cleanmgr -ArgumentList "/sagerun:3" -NoNewWindow -Wait
$CurrentSystemOS = Get-CIMInstance -ClassName Win32_OperatingSystem -NameSpace root\CIMV2 | select -Property OSArchitecture, Version, Caption
$CurrentModel = Get-CIMInstance -ClassName Win32_ComputerSystem -NameSpace root\CIMV2 | select -Property Manufacturer, Model, SystemSKUNumber
$BaseBoardProduct = (Get-CIMInstance -ClassName MS_SystemInformation -NameSpace root\WMI).BaseBoardProduct
$Platform = 'ConfigMgr'
switch -wildcard ($CurrentModel.Manufacturer)
{
"*HP*" {
$ExtractMake = "Hewlett-Packard"
$ExtractSKU = (Get-CIMInstance -ClassName MS_SystemInformation -NameSpace root\WMI).BaseBoardProduct
}
"*Hewlett-Packard*" {
$ExtractMake = "Hewlett-Packard"
$ExtractSKU = (Get-CIMInstance -ClassName MS_SystemInformation -NameSpace root\WMI).BaseBoardProduct
}
"*Dell*" {
$ExtractMake = "Dell"
$ExtractSKU = $CurrentModel.SystemSKUNumber
$CurrentModel.Model = (Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Model).Trim()
$ExtractSku = (Get-CIMInstance -ClassName MS_SystemInformation -NameSpace root\WMI).SystemSku
if ($ExtractSku -eq "")
{
$ExtractSku = (Get-CIMInstance -ClassName MS_SystemInformation -NameSpace root\WMI).BaseBoardProduct
}
}
"*Lenovo*" {
$ExtractMake = "Lenovo"
$ExtractSKU = ((Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Model).SubString(0, 4)).Trim()
}
"*Panasonic*" {
$ComputerManufacturer = "Panasonic Corporation"
$ComputerModel = (Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Model).Trim()
$SystemSKU = (Get-CIMInstance -ClassName MS_SystemInformation -NameSpace root\WMI).BaseBoardProduct.Trim()
}
"*Viglen*" {
$ComputerManufacturer = "Viglen"
$ComputerModel = (Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Model).Trim()
$SystemSKU = (Get-WmiObject -Class Win32_BaseBoard | Select-Object -ExpandProperty SKU).Trim()
}
"*AZW*" {
$ComputerManufacturer = "AZW"
$ComputerModel = (Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Model).Trim()
$SystemSKU = (Get-CIMInstance -ClassName MS_SystemInformation -NameSpace root\WMI).BaseBoardProduct.Trim()
}
default
{
$ExtractMake = $CurrentModel.Manufacturer
$ExtractSKU = (Get-CIMInstance -ClassName MS_SystemInformation -NameSpace root\WMI).BaseBoardProduct
}
}
switch -wildcard ($CurrentSystemOS.Caption)
{
"*Windows 10*" {
$OSRelease = [version]"10.0"
$OSName = "Windows 10"
}
"*Windows 8.1" {
$OSRelease = [version]"6.3"
$OSName = "Windows 8.1"
}
"*Windows 8" {
$OSRelease = [version]"6.2"
$OSName = "Windows 8"
}
"*Windows 7" {
$OSRelease = [version]"6.1"
$OSName = "Windows 7"
}
}
switch -wildcard ($CurrentSystemOS.OSArchitecture)
{
"64*" {
$OSArchitecture = "x64"
}
"32*" {
$OSArchitecture = "x86"
}
}
if ($OSName -eq "Windows 10")
{
$ReleaseID = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name ReleaseID).ReleaseID
$OSName = $OSName + " $ReleaseID"
}
# Format: Make Model - SKU - OSNameOsArchitecture
$ExtractDriverRootDir = Join-Path -Path "$RootPath" -ChildPath "$ExtractMake $($CurrentModel.Model) - $ExtractSKU - $OSName$OSArchitecture"
$ExtractDriverDir = Join-Path -Path "$ExtractDriverRootDir" -ChildPath "DriverPackage"
$ExtractDriverInfoXML = Join-Path -Path $ExtractDriverRootDir -ChildPath "ModelDetail.xml"
if (!(Test-Path -Path $ExtractDriverDir))
{
# Folder does not exist, create it
New-Item -Path $ExtractDriverRootDir -ItemType directory
New-Item -Path $ExtractDriverDir -ItemType directory
}
$ExtractDriverDir = "`"$ExtractDriverDir`""
# Extract drivers and place in ExtractDriverDir
try
{
Write-Verbose "[TRY] Attempting to extract drivers with dism..." -Verbose
Start-Process dism -ArgumentList "/online /export-driver /destination:$($ExtractDriverDir)" -Wait
}
catch [System.Exception] {
Write-Warning -Message "Failed to run dism"
}
if ($ConfigFile.Settings.Compression -eq "Zip")
{
# Compress contents of ExtractDriverDir
Write-Verbose -Message "[INFO] Creating DriverPackage.zip"
$CompressedZipPath = Join-Path -Path $ExtractDriverRootDir -ChildPath "DriverPackage"
$DestinationZipPath = Join-Path -path $ExtractDriverRootDir -ChildPath "DriverPackage.zip"
Compress-Archive -Path $CompressedZipPath -DestinationPath $DestinationZipPath -CompressionLevel Fastest -Force
if ($DestinationZipPath)
{
Write-Verbose -Message "[INFO] Zip file created. Removing source.."
Remove-Item -Path $CompressedZipPath -Recurse -Force
}
}
Write-Verbose -Message "[INFO] Writing ModelDetails.xml file..."
# Set XML Structure
$XmlWriter = New-Object System.XML.XmlTextWriter($ExtractDriverInfoXML, $Null)
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='style.xsl'")
# Write Initial Header Comments
$XmlWriter.WriteComment('Created with the SCConfigMgr Driver Automation Tool')
$xmlWriter.WriteStartElement('Details')
$XmlWriter.WriteAttributeString('current', $true)
# Export Model Details
$xmlWriter.WriteStartElement('ModelDetails')
$xmlWriter.WriteElementString('Make', $ExtractMake)
$xmlWriter.WriteElementString('Model', $CurrentModel.Model)
$xmlWriter.WriteElementString('SystemSKU', $ExtractSKU)
$xmlWriter.WriteElementString('OperatingSystem', $OSName)
$xmlWriter.WriteElementString('Architecture', $OSArchitecture)
$xmlWriter.WriteElementString('Platform', $Platform)
$xmlWriter.WriteEndElement()
# Save XML Document
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
if ($SendCompletionEmail = "True")
{
Write-Verbose -Message "[INFO] Sending email report..."
$SendEmailFrom = "$($ConfigFile.Settings.EmailFromText) <$($ConfigFile.Settings.EmailFromAddress)>"
$SendEmailTo = "$($ConfigFile.Settings.EmailToText) <$($ConfigFile.Settings.EmailToAddress)>"
$SendEmailSubject = "$($ConfigFile.Settings.EmailSubject)"
$SendEmailBody = "Driver capture completed. The latest model captured is: $($CurrentModel.Manufacturer) $($CurrentModel.Model) for $OSName $OSArchitecture - Please import the driver package using the XML file in this folder: $ExtractDriverInfoXML"
$MailServer = "$($ConfigFile.Settings.EmailServer)"
Write-Verbose -Message "[INFO] Sending email report to $SendEmailTo"
Send-MailMessage -From $SendEmailFrom -To $SendEmailTo -Subject $SendEmailFrom -Body $SendEmailBody -Priority High -dno onSuccess, onFailure -SmtpServer $MailServer
}
if ($ConfigFile.Settings.VerboseLogging -eq "True")
{
Pause
}