-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate-SCCM3rdPartySoftware.ps1
387 lines (314 loc) · 15.5 KB
/
Update-SCCM3rdPartySoftware.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<#
.SYNOPSIS
Update 3rd party update files in MDT Apllication section
.DESCRIPTION
Parses third party updates CliXML list generated from Get-3rdPartySoftware.ps1. Then check MDT for applications similiar to the list using filters.
.PARAMETER
NONE
.EXAMPLE
powershell.exe -ExecutionPolicy Bypass -file "Update-SCCM3rdPartySoftware.ps1"
.NOTES
Script name: Update-SCCM3rdPartySoftware
Version: 1.0
Author: Richard Tracy
DateCreated: 2019-04-22
#>
#==================================================
# FUNCTIONS
#==================================================
Function Test-IsISE {
# try...catch accounts for:
# Set-StrictMode -Version latest
try {
return $psISE -ne $null;
}
catch {
return $false;
}
}
Function Get-ScriptPath {
If (Test-Path -LiteralPath 'variable:HostInvocation') { $InvocationInfo = $HostInvocation } Else { $InvocationInfo = $MyInvocation }
# Makes debugging from ISE easier.
if ($PSScriptRoot -eq "")
{
if (Test-IsISE)
{
$psISE.CurrentFile.FullPath
#$root = Split-Path -Parent $psISE.CurrentFile.FullPath
}
else
{
$context = $psEditor.GetEditorContext()
$context.CurrentFile.Path
#$root = Split-Path -Parent $context.CurrentFile.Path
}
}
else
{
#$PSScriptRoot
$PSCommandPath
#$MyInvocation.MyCommand.Path
}
}
Function Format-ElapsedTime($ts) {
$elapsedTime = ""
if ( $ts.Minutes -gt 0 ){$elapsedTime = [string]::Format( "{0:00} min. {1:00}.{2:00} sec.", $ts.Minutes, $ts.Seconds, $ts.Milliseconds / 10 );}
else{$elapsedTime = [string]::Format( "{0:00}.{1:00} sec.", $ts.Seconds, $ts.Milliseconds / 10 );}
if ($ts.Hours -eq 0 -and $ts.Minutes -eq 0 -and $ts.Seconds -eq 0){$elapsedTime = [string]::Format("{0:00} ms.", $ts.Milliseconds);}
if ($ts.Milliseconds -eq 0){$elapsedTime = [string]::Format("{0} ms", $ts.TotalMilliseconds);}
return $elapsedTime
}
Function Format-DatePrefix{
[string]$LogTime = (Get-Date -Format 'HH:mm:ss.fff').ToString()
[string]$LogDate = (Get-Date -Format 'MM-dd-yyyy').ToString()
$CombinedDateTime = "$LogDate $LogTime"
return ($LogDate + " " + $LogTime)
}
Function Write-LogEntry{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string]$Message,
[Parameter(Mandatory=$false,Position=2)]
[string]$Source = '',
[parameter(Mandatory=$false)]
[ValidateSet(0,1,2,3,4)]
[int16]$Severity,
[parameter(Mandatory=$false, HelpMessage="Name of the log file that the entry will written to.")]
[ValidateNotNullOrEmpty()]
[string]$OutputLogFile = $Global:LogFilePath,
[parameter(Mandatory=$false)]
[switch]$Outhost = $Global:OutToHost
)
## Get the name of this function
[string]${CmdletName} = $PSCmdlet.MyInvocation.MyCommand.Name
[string]$LogTime = (Get-Date -Format 'HH:mm:ss.fff').ToString()
[string]$LogDate = (Get-Date -Format 'MM-dd-yyyy').ToString()
[int32]$script:LogTimeZoneBias = [timezone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes
[string]$LogTimePlusBias = $LogTime + $script:LogTimeZoneBias
# Get the file name of the source script
Try {
If ($script:MyInvocation.Value.ScriptName) {
[string]$ScriptSource = Split-Path -Path $script:MyInvocation.Value.ScriptName -Leaf -ErrorAction 'Stop'
}
Else {
[string]$ScriptSource = Split-Path -Path $script:MyInvocation.MyCommand.Definition -Leaf -ErrorAction 'Stop'
}
}
Catch {
$ScriptSource = ''
}
If(!$Severity){$Severity = 1}
$LogFormat = "<![LOG[$Message]LOG]!>" + "<time=`"$LogTimePlusBias`" " + "date=`"$LogDate`" " + "component=`"$ScriptSource`" " + "context=`"$([Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " + "type=`"$Severity`" " + "thread=`"$PID`" " + "file=`"$ScriptSource`">"
# Add value to log file
try {
Out-File -InputObject $LogFormat -Append -NoClobber -Encoding Default -FilePath $OutputLogFile -ErrorAction Stop
}
catch {
Write-Host ("[{0}] [{1}] :: Unable to append log entry to [{1}], error: {2}" -f $LogTimePlusBias,$ScriptSource,$OutputLogFile,$_.Exception.ErrorMessage) -ForegroundColor Red
}
If($Outhost){
If($Source){
$OutputMsg = ("[{0}] [{1}] :: {2}" -f $LogTimePlusBias,$Source,$Message)
}
Else{
$OutputMsg = ("[{0}] [{1}] :: {2}" -f $LogTimePlusBias,$ScriptSource,$Message)
}
Switch($Severity){
0 {Write-Host $OutputMsg -ForegroundColor Green}
1 {Write-Host $OutputMsg -ForegroundColor Gray}
2 {Write-Warning $OutputMsg}
3 {Write-Host $OutputMsg -ForegroundColor Red}
4 {If($Global:Verbose){Write-Verbose $OutputMsg}}
default {Write-Host $OutputMsg}
}
}
}
# Function to get properties from an MSI package
function Get-MsiProperty
{
param(
[ValidateNotNullOrEmpty()]
[string]$Path,
[ValidateNotNullOrEmpty()]
[string]$Property
)
function Get-Property($Object, $PropertyName, [object[]]$ArgumentList)
{
return $Object.GetType().InvokeMember($PropertyName, 'Public, Instance, GetProperty', $null, $Object, $ArgumentList)
}
function Invoke-Method($Object, $MethodName, $ArgumentList)
{
return $Object.GetType().InvokeMember($MethodName, 'Public, Instance, InvokeMethod', $null, $Object, $ArgumentList)
}
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$msiOpenDatabaseModeReadOnly = 0
$Installer = New-Object -ComObject WindowsInstaller.Installer
$Database = Invoke-Method $Installer OpenDatabase @($Path, $msiOpenDatabaseModeReadOnly)
$View = Invoke-Method $Database OpenView @("SELECT Value FROM Property WHERE Property=$Property")
Invoke-Method $View Execute
$Record = Invoke-Method $View Fetch
if ($Record)
{
Write-Output(Get-Property $Record StringData 1)
}
Invoke-Method $View Close @( )
Remove-Variable -Name Record, View, Database, Installer
}
##*===========================================================================
##* VARIABLES
##*===========================================================================
# Use function to get paths because Powershell ISE and other editors have differnt results
$scriptPath = Get-ScriptPath
[string]$scriptDirectory = Split-Path $scriptPath -Parent
[string]$scriptName = Split-Path $scriptPath -Leaf
[string]$scriptBaseName = [System.IO.Path]::GetFileNameWithoutExtension($scriptName)
#Get required folder and File paths
[string]$ConfigPath = Join-Path -Path $scriptDirectory -ChildPath 'Configs'
[string]$RelativeLogPath = Join-Path -Path $scriptDirectory -ChildPath 'Logs'
#Set this to somehting different for testing
$ConfigFile = "sccm_configs.s3i.xml"
#build log name
[string]$FileName = $scriptBaseName +'.log'
#build global log fullpath
$Global:LogFilePath = Join-Path $RelativeLogPath -ChildPath $FileName
#clean old log
if(Test-Path $Global:LogFilePath){remove-item -Path $Global:LogFilePath -ErrorAction SilentlyContinue | Out-Null}
Write-Host "Logging to file: $LogFilePath" -ForegroundColor Cyan
# BUILD PATHS FROM XML
#=======================================================
[string]$SCCMXMLFile = (Get-Content "$ConfigPath\$ConfigFile" -ReadCount 0) -replace '&','&'
[xml]$SCCMConfigs = $SCCMXMLFile
#get the list of aoftware
[string]$SCCMServer = $SCCMConfigs.SCCMConfigs.server.host
[string]$SCCMRepo = $SCCMConfigs.SCCMConfigs.server.repository
[string]$SCCMLocalRepo = $SCCMConfigs.SCCMConfigs.server.localrepository
[string]$SCCMSiteCode = $SCCMConfigs.SCCMConfigs.server.sitecode
#build sccm source path
$SCCMSharePath = "\\" + $SCCMServer + "\" + $SCCMRepo
#Test directories before continuing
If(-not(Test-Path $SCCMSharePath) ){
write-host ("Unable to connect to SCCM Source Repository: {0}" -f $SCCMSharePath) -ForegroundColor Red
Break
}
Else{
New-PSDrive -Name SCCMREPO -PSProvider FileSystem -Root $SCCMSharePath
write-host ("Connected to SCCM Source Repository: {0}" -f $SCCMSharePath)
}
#get the list of software
[string]$AppendToName = $SCCMConfigs.SCCMConfigs.softwareBuild.appendName
[string]$3rdSoftwareRootPath = $SCCMConfigs.SCCMConfigs.softwareBuild.software.rootPath -replace '&','&'
[string]$3rdSoftwareListPath = $SCCMConfigs.SCCMConfigs.softwareBuild.software.listPath -replace '&','&'
[string]$ExcludedSoftware = $SCCMConfigs.SCCMConfigs.softwareBuild.ExcludeSoftware -replace ',','|'
If((Test-Path $3rdSoftwareRootPath) -or (Test-Path $3rdSoftwareListPath)){
$SoftwarePath = New-PSDrive -Name SOFTREPO -PSProvider FileSystem -Root $3rdSoftwareRootPath
}
Else{
write-host ("Unable to find software repository path or xml file: {0}" -f $3rdSoftwareRootPath) -ForegroundColor Red
Exit
}
#Import ConfigurationManager powershell Module
If($Env:SMS_ADMIN_UI_PATH){
Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1) -Verbose
#Connect to the site drive if not already mapped
If(-not(Get-PSDrive -Name $SCCMSiteCode -PSProvider CMSite)){
New-PSDrive -Name $SCCMSiteCode -PSProvider CMSite -Root $SCCMServer
}
}
ElseIf($RemoteProvider){
[System.Management.Automation.PSCredential]$SCCMCreds = Import-Clixml ($scriptDirectory + "\" + $SCCMConfigs.SCCMConfigs.server.remoteAuthFile)
#remote into MDT server to import MDT module
Try{
Enter-PSSession $SCCMServer -Credential $SCCMCreds -EnableNetworkAccess -ErrorAction Stop
Start-sleep 5
Import-Module -Name "$(split-path $Env:SMS_ADMIN_UI_PATH)\ConfigurationManager.psd1"
if(Get-module *ConfigurationManager*){
Set-Location -path "$(Get-PSDrive -PSProvider CMSite):\"
}
$session = New-PSSession $SCCMServer -Credential $SCCMCreds -EnableNetworkAccess -ErrorAction Stop
Invoke-Command -Session $session -ScriptBlock {
Import-Module -Name "$(split-path $Env:SMS_ADMIN_UI_PATH)\ConfigurationManager.psd1"
if(Get-module *ConfigurationManager*){
Set-Location -path "$(Get-PSDrive -PSProvider CMSite):\"
}
$CMsite = (Get-CMSite).SiteCode
$CMApplications = Get-CMApplication
$CMPackages = Get-CMPackage
$CCMTaskSequences = Get-CMTaskSequence
} -ArgumentList
}
Catch{
Write-Host "Failed to remote into: $($_.Exception.Message)" -ForegroundColor Red
Exit
}
}
Else{
Write-Host "Unable to load Configuration Managers powershell module" -ForegroundColor Red
Exit
}
##* ==============================
##* MAIN - DO ACTION
##* ==============================
If(Get-PSDrive -Name $SCCMSiteCode -PSProvider CMSite){
#set the working location to sccm
Set-Location "$($SCCMSiteCode):\"
#once Connected, lets grab some information
$CMsite = (Get-CMSite).SiteCode
$CMApplications = Get-CMApplication
$CMPackages = Get-CMPackage
$CCMTaskSequences = Get-CMTaskSequence
#since mapping ps drive; must grab software list xml from share
$NewSoftwareList = Import-Clixml "$($SoftwarePath):\softwarelist.xml"
#$NewSoftwareList = Import-Clixml "$3rdSoftwareListPath"
#filter list using excludded list
$FilteredNewSoftList = $NewSoftwareList | Where-Object {$_.Publisher -notmatch $ExcludedSoftware}
#loop through new software (filtered)
foreach($NewSoftware in $FilteredNewSoftList)
{
#append name to applications to indetify what the script built
If($AppendToName){$append = " (" + $AppendToName + ")"}Else{$append = ""}
$GeneratedName = $NewSoftware.Publisher + " " + $NewSoftware.Product + $append
#Check there is already an application found
If(-not(Compare-object $GeneratedName $CMApplications.LocalizedDisplayName -IncludeEqual | Where{$_.SideIndicator -eq "=="})){
}
} # end loop
}
#set schedule
New-CMSchedule -Start(Random-StartTime) –RecurInterval Days –RecurCount 1
$AppCollection = Get-CMCollection -Name $CollectionName
$AppCollection = New-CMDeviceCollection -Name $CollectionName -LimitingCollectionName $DeviceLimitingCollection -RefreshType Both -RefreshSchedule $Schedule
# If an AD group was specified, add a query membership rule based on that group
Add-CMDeviceCollectionQueryMembershipRule -Collection $AppCollection -QueryExpression "select * from SMS_R_System where SMS_R_System.SystemGroupName = ""$DomainNetbiosName\\$ADGroupName""" -RuleName "Members of AD group $ADGroupName"
#Created user collection
$AppCollection = New-CMUserCollection -Name $CollectionName -LimitingCollectionName $UserLimitingCollection -RefreshType Both -RefreshSchedule $Schedule
# Create application
New-CMApplication -Name $ApplicationName -Publisher $Publisher -AutoInstall $true -SoftwareVersion $ApplicationVersion -LocalizedName $TextBoxAppName.Text
# Move the application to folder
$ApplicationObject = Get-CMApplication -Name $ApplicationName
Move-CMObject -FolderPath $ApplicationFolderPath -InputObject $ApplicationObject
# CREATE MSI DEPLOYMENT TYPE
Add-CMMsiDeploymentType -ApplicationName $ApplicationName -DeploymentTypeName "Install $ApplicationName" -ContentLocation $MSIFile -LogonRequirementType WhereOrNotUserLoggedOn -Force
# Update the deployment type
$NewDeploymentType = Get-CMDeploymentType -ApplicationName $ApplicationName
# Set the installation program
Set-CMMsiDeploymentType -ApplicationName $ApplicationName -DeploymentTypeName $NewDeploymentType.LocalizedDisplayName -InstallCommand $InstallationProgram
# Set the uninstallation program
Set-CMMsiDeploymentType -ApplicationName $ApplicationName -DeploymentTypeName $NewDeploymentType.LocalizedDisplayName -UninstallCommand $UninstallationProgram
# Set behavior for running installation as 32-bit process on 64-bit systems
Set-CMMsiDeploymentType -ApplicationName $ApplicationName -DeploymentTypeName $NewDeploymentType.LocalizedDisplayName -Force32Bit $true
# Set the content source path
Set-CMDeploymentType -MsiOrScriptInstaller -ApplicationName $ApplicationName -DeploymentTypeName $NewDeploymentType.LocalizedDisplayName -ContentLocation $ContentSourcePath -WarningAction SilentlyContinue
# Set the option for fallback source location
Set-CMMsiDeploymentType -ApplicationName $ApplicationName -DeploymentTypeName $NewDeploymentType.LocalizedDisplayName -ContentFallback $true
# Set the behavior for clients on slow networks
Set-CMMsiDeploymentType -ApplicationName $ApplicationName -DeploymentTypeName $NewDeploymentType.LocalizedDisplayName -SlowNetworkDeploymentMode Download
# Distribute content to DP group
Start-CMContentDistribution -ApplicationName $ApplicationName -DistributionPointGroupName $DPGroup
# Deploy the application
Start-CMApplicationDeployment -CollectionName $AppCollection.Name -Name $ApplicationName -DeployPurpose $DeployPurpose
# CREATE MANUAL DEPLOYMENT TYPE
Add-CMScriptDeploymentType -ApplicationName $ApplicationName -DeploymentTypeName "Install $ApplicationName" -ContentLocation $ContentSourcePath -InstallCommand $InstallationProgram -ScriptLanguage PowerShell -ScriptText 'if (Test-Path C:\DummyDetectionMethod) {Write-Host "IMPORTANT! This detection method does not work. You must manually change it."}' -InstallationBehaviorType InstallForSystem -UserInteractionMode Normal -LogonRequirementType WhereOrNotUserLoggedOn
# Update the deployment type
$NewDeploymentType = Get-CMDeploymentType -ApplicationName $ApplicationName