-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPackages.ps1
More file actions
88 lines (79 loc) · 4.2 KB
/
getPackages.ps1
File metadata and controls
88 lines (79 loc) · 4.2 KB
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
# Replace with the full path of the folder MSI-Deploy-AutoUpdate : "c:\programs\MSI-Deploy-AutoUpdate"
$FullPath = "."
# importing the CSV file that contains the links to the MSI packages
$LinksFile = Import-CSV -Path "$FullPath\packagesLinks.csv" -Delimiter ";"
# run through the CSV file to download the MSI packages
foreach($LINE in $LinksFile)
{
$packageName = $LINE.NAME
$packageLink = $LINE.LINK
# Download the MSI package to the buffer zone
(new-object Net.WebClient).DownloadFile($packageLink, "$FullPath\bufferZone\$packageName.msi")
if($?) {
CreateANewLogEntry -State "Successful" -InputContent "MSI package $packageName downloaded successfully to buffer zone."
# Verification of the presence of the package in the updatedPackages folder
if(Test-Path "$FullPath\updatedPackages\$packageName.msi"){
# We retrieve the version number of the old package
$oldVersion = Get-AppLockerFileInformation -Path "$FullPath\updatedPackages\$packageName.msi" | Select-Object -ExpandProperty Publisher | Select-Object BinaryVersion
} else {
$oldVersion = "none"
}
# We retrieve the version number of the new package
$newVersion = Get-AppLockerFileInformation -Path "$FullPath\bufferZone\$packageName.msi" | Select-Object -ExpandProperty Publisher | Select-Object BinaryVersion
# If the version number has not changed, we do nothing
if ($oldVersion.ToString() -eq $newVersion.ToString()) {
CreateANewLogEntry -State "Checking" -InputContent "$packageName package is already the newest version."
} else {
CreateANewLogEntry -State "Checking" -InputContent "New version available for $packageName."
# If the version number has changed, we delete the old package and move the new package to the updatedPackages folder
if(Test-Path "$FullPath\updatedPackages\$packageName.msi"){
Remove-Item -Path $FullPath\updatedPackages\$packageName.msi
if($?) {
CreateANewLogEntry -State "Successful" -InputContent "Successfully removed the $packageName old package."
Move-Item -Path $FullPath\bufferZone\$packageName.msi -Destination .\updatedPackages\$packageName.msi
if($?) {
CreateANewLogEntry -State "Successful" -InputContent "$packageName package successfully updated."
} else {
CreateANewLogEntry -State "Fail" -InputContent "failed to updating $packageName package."
}
} else {
CreateANewLogEntry -State "Fail" -InputContent "failed to remove $packageName old package."
}
} else {
Move-Item -Path "$FullPath\bufferZone\$packageName.msi -Destination .\updatedPackages\$packageName.msi"
if($?) {
CreateANewLogEntry -State "Successful" -InputContent "$packageName package successfully updated."
} else {
CreateANewLogEntry -State "Fail" -InputContent "failed to updating $packageName package."
}
}
}
}
else
{
CreateANewLogEntry -State "Fail" -InputContent "Failed to download MSI package $packageName to buffer zone."
}
# Erase the package from the buffer zone
Remove-Item -Path "$FullPath\bufferZone\$packageName.msi"
if($?){
CreateANewLogEntry -State "Successful" -InputContent "$packageName package successfully erased from buffer zone."
} else {
CreateANewLogEntry -State "Fail" -InputContent "Failed to erase MSI package $packageName from buffer zone."
}
}
# Function to create a new log entry
function CreateANewLogEntry{
Param
(
[Parameter(Mandatory=$true, Position=0)]
[string] $State,
[Parameter(Mandatory=$true, Position=1)]
[string] $InputContent
)
$currentDate = Get-Date -Format "dd-MM-yyyy_HH:mm"
$currentContent = Get-Content -Path $FullPath\logs.txt
$newContent = @()
$newContent += "$currentDate - $State - $InputContent"
$newContent += $currentContent
$newContent | Out-File "$FullPath\logs.txt"
}