forked from aaronparker/packagefactory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-Win32Package.psm1
More file actions
152 lines (138 loc) · 7.25 KB
/
New-Win32Package.psm1
File metadata and controls
152 lines (138 loc) · 7.25 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
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
using namespace System.Management.Automation
<#
Functions for use in New-Win32Package.ps1
#>
function Test-IntuneWin32App {
<#
Return true of false if there's a matching Win32 package in the Intune tenant
#>
param (
[Parameter(Mandatory = $true)]
[System.Object] $Manifest
)
Write-Information -MessageData "$($PSStyle.Foreground.Cyan)Retrieve existing Win32 applications in Intune"
$ExistingApp = Get-IntuneWin32App | `
Select-Object -Property * -ExcludeProperty "largeIcon" | `
Where-Object { $_.notes -match "PSPackageFactory" } | `
Where-Object { ($_.notes | ConvertFrom-Json -ErrorAction "SilentlyContinue").Guid -eq $Manifest.Information.PSPackageFactoryGuid } | `
Sort-Object -Property @{ Expression = { [System.Version]$_.displayVersion }; Descending = $true } -ErrorAction "SilentlyContinue" | `
Select-Object -First 1
# Determine whether the new package should be imported
if ($null -eq $ExistingApp) {
Write-Information -MessageData "$($PSStyle.Foreground.Cyan)Import new application: '$($Manifest.Information.DisplayName)'"
Write-Output -InputObject $true
}
elseif ([System.String]::IsNullOrEmpty($ExistingApp.displayVersion)) {
Write-Information -MessageData "$($PSStyle.Foreground.Cyan)Found matching app but 'displayVersion' is null: '$($ExistingApp.displayName)'"
Write-Output -InputObject $false
}
elseif ([version]$Manifest.PackageInformation.Version -le [version]$ExistingApp.displayVersion) {
Write-Information -MessageData "$($PSStyle.Foreground.Cyan)Existing Intune app version is current: '$($ExistingApp.displayName)'"
Write-Output -InputObject $false
}
elseif ([version]$Manifest.PackageInformation.Version -gt [version]$ExistingApp.displayVersion) {
Write-Information -MessageData "$($PSStyle.Foreground.Cyan)Import application version: '$($Manifest.Information.DisplayName)'"
Write-Output -InputObject $true
}
else {
Write-Output -InputObject $false
}
}
function Get-MsiProductCode {
<#
Modified Version of https://www.powershellgallery.com/packages/Get-MsiProductCode/1.0/Content/Get-MsiProductCode.ps1
from Thomas J. Malkewitz @dotsp1
mod. by @constey
#>
param (
[Parameter(Mandatory = $true, ValueFromPipeLine = $true)]
[ValidateScript({
if ($_.EndsWith('.msi')) { $true } else { throw "$_ must be an '*.msi' file." }
if (Test-Path $_) { $true } else { throw "$_ does not exist." }
})]
[System.String[]] $Path
)
process {
foreach ($Item in $Path) {
try {
$WindowsInstaller = New-Object -ComObject "WindowsInstaller.Installer"
$Database = $WindowsInstaller.GetType().InvokeMember('OpenDatabase', 'InvokeMethod', $null, $WindowsInstaller, @((Get-Item -Path $Item).FullName, 0))
$View = $Database.GetType().InvokeMember('OpenView', 'InvokeMethod', $null, $Database, ("SELECT Value FROM Property WHERE Property = 'ProductCode'"))
$View.GetType().InvokeMember('Execute', 'InvokeMethod', $null, $View, $null)
$Record = $View.GetType().InvokeMember('Fetch', 'InvokeMethod', $null, $View, $null)
Write-Output -InputObject $($record.GetType().InvokeMember('StringData', 'GetProperty', $null, $Record, 1))
}
catch {
Write-Error -Message $_.Exception.Message
break
}
finally {
$view.GetType().InvokeMember('Close', 'InvokeMethod', $null, $view, $null)
[Void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($WindowsInstaller)
}
}
}
}
function Set-ScriptSignature {
<#
Use Set-AuthenticodeSignature to sign all scripts in a defined path
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $true, ParameterSetName = "Certificate")]
[Parameter(Mandatory = $true, ParameterSetName = "Subject")]
[Parameter(Mandatory = $true, ParameterSetName = "Thumbprint")]
[ValidateScript({ if (Test-Path -Path $_) { $true } else { throw [System.IO.DirectoryNotFoundException]::New("Directory not found: $_") } })]
[System.String[]] $Path,
[Parameter(Mandatory = $true, ParameterSetName = "Certificate")]
[System.Security.Cryptography.X509Certificates.X509Certificate2] $Certificate,
[Parameter(Mandatory = $true, ParameterSetName = "Subject")]
[System.String] $CertificateSubject,
[Parameter(Mandatory = $true, ParameterSetName = "Thumbprint")]
[System.String] $CertificateThumbprint,
[Parameter(Mandatory = $false, ParameterSetName = "Certificate")]
[Parameter(Mandatory = $false, ParameterSetName = "Subject")]
[Parameter(Mandatory = $false, ParameterSetName = "Thumbprint")]
[System.String] $TimestampServer,
[Parameter(Mandatory = $false, ParameterSetName = "Certificate")]
[Parameter(Mandatory = $false, ParameterSetName = "Subject")]
[Parameter(Mandatory = $false, ParameterSetName = "Thumbprint")]
[System.String] $IncludeChain = "NotRoot"
)
begin {
$CertPaths = @("Cert:\CurrentUser\My", "Cert:\CurrentUser\Root", "Cert:\CurrentUser\TrustedPublisher",
"Cert:\LocalMachine\My", "Cert:\LocalMachine\Root", "Cert:\LocalMachine\TrustedPublisher")
if ($PSBoundParameters.ContainsKey("CertificateSubject")) {
$Certificate = Get-ChildItem -Path $CertPaths -CodeSigningCert | Where-Object { $_.Subject -eq $CertificateSubject }
if ($null -eq $Certificate) { throw [Microsoft.PowerShell.Commands.CertificateNotFoundException]::New("Certificate matching subject name '$CertificateSubject' not found.") }
}
elseif ($PSBoundParameters.ContainsKey("CertificateThumbprint")) {
$Certificate = Get-ChildItem -Path $CertPaths -CodeSigningCert | Where-Object { $_.Subject -eq $CertificateThumbprint }
if ($null -eq $Certificate) { throw [Microsoft.PowerShell.Commands.CertificateNotFoundException]::New("Certificate matching thumbprint '$CertificateThumbprint' not found.") }
}
}
process {
foreach ($Directory in $Path) {
Get-ChildItem -Path $Directory -Recurse -Include "*.ps1", "*.psm1", "*.psd1" | ForEach-Object {
try {
$params = @{
FilePath = $_.FullName
Certificate = $Certificate
HashAlgorithm = "SHA256"
IncludeChain = $IncludeChain
ErrorAction = "Stop"
}
if ($PSBoundParameters.ContainsKey("TimestampServer")) {
$params.TimestampServer = $TimestampServer
}
if ($PSCmdlet.ShouldProcess("Set-AuthenticodeSignature", $_.FullName)) {
Set-AuthenticodeSignature @params
}
}
catch {
Write-Error -Message "Failed to sign script '$($_.FullName)', with '$($_.Exception.Message)'"
}
}
}
}
}