forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompare-Script.ps1
More file actions
110 lines (91 loc) · 3.36 KB
/
Compare-Script.ps1
File metadata and controls
110 lines (91 loc) · 3.36 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
Function Compare-Script {
[cmdletbinding()]
[OutputType("PSCustomObject")]
[alias("csc")]
Param (
[Parameter(
Position = 0,
ValueFromPipelineByPropertyName
)]
[ValidateNotNullorEmpty()]
[Alias("scriptname")]
[string]$Name,
[ValidateNotNullorEmpty()]
[string]$Gallery = "PSGallery"
)
Begin {
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
$progParam = @{
Activity = $MyInvocation.MyCommand
Status = "Getting installed scripts"
CurrentOperation = "Get-InstalledScript"
PercentComplete = 25
}
Write-Progress @progParam
} #begin
Process {
$gscParams = @{}
if ($Name) {
$gscParams.Add("Name", $Name)
}
$installed = @(Get-InstalledScript @gscParams)
if ($installed) {
$progParam.Status = "Getting online scripts"
$progParam.CurrentOperation = "Find-Script -Repository $Gallery"
$progParam.PercentComplete = 50
Write-Progress @progParam
$fscParams = @{
Repository = $Gallery
ErrorAction = "Stop"
}
if ($Name) {
$fscParams.Add("Name", $Name)
}
Try {
$online = @(Find-Script @fscParams)
}
Catch {
Write-Warning "Failed to find online script(s). $($_.Exception.message)"
}
$progParam.status = "Comparing $($installed.count) installed scripts to $($online.count) online scripts."
$progParam.percentComplete = 80
Write-Progress @progParam
$data = ($online).Where( {$installed.name -contains $_.name}) |
Select-Object -property Name,
@{Name = "OnlineVersion"; Expression = {$_.Version}},
@{Name = "InstalledVersion"; Expression = {
#save the name from the incoming online object
$name = $_.Name
$installed.Where( {$_.name -eq $name}).Version -join ","}
},
PublishedDate,
@{Name = "UpdateNeeded"; Expression = {
$name = $_.Name
#there could be multiple versions installed
#only need to compare the last one
$mostRecentVersion = $installed.Where( {$_.name -eq $name}).Version |
Sort-Object -Descending | Select-Object -first 1
#need to ensure that PowerShell compares version objects and not strings
If ([version]$_.Version -gt [version]$mostRecentVersion) {
$result = $True
}
else {
$result = $False
}
$result
}
} | Sort-Object -Property Name
$progParam.PercentComplete = 100
$progParam.Completed = $True
Write-Progress @progparam
#write the results to the pipeline
$data
}
else {
Write-Warning "No local script or scripts found"
}
} #Progress
End {
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #end
} #close function