-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-NotepadppBinary.ps1
137 lines (117 loc) · 4.42 KB
/
Get-NotepadppBinary.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
<#
.Synopsis
Short description
This script will be used for downloading the latest Notepad++ version from its official site.
.DESCRIPTION
Long description
2021-02-06 Sukri Created.
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
.INPUTS
Inputs to this cmdlet (if any)
.OUTPUTS
Output from this cmdlet (if any)
.NOTES
General notes
Author : Sukri Kadir
Email : [email protected]
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
function Get-NotepadppBinary
{
Param
(
#parameter for Notepad++ installer source url.
[ValidateNotNullOrEmpty()]
[String]
$InstallerSourceUrl
)
Begin
{
#create the request.
$HttpRequest = [System.Net.WebRequest]::Create($InstallerSourceUrl)
#get a response from the site.
$HttpResponse = $HttpRequest.GetResponse()
#get the HTTP code as an integer.
$HttpStatusCode = [int]$HttpResponse.StatusCode
#throw exception if status code is not 200 (OK).
if ($HttpStatusCode -ne 200)
{
Write-Error -Message "[$InstallerSourceUrl] unable to reach out with status code [$HttpStatusCode]." -ErrorAction Stop
}
#get OS architecture - 32 or 64-bit?.
$OSArchitecture = Get-OSArchitecture
}
Process
{
#get site contents.
$SiteContents = Invoke-WebRequest -Uri $InstallerSourceUrl -UseBasicParsing
#get href link.
$SiteHrefs = $SiteContents.Links
#dynamic array for storing Notepad++ version extracted from site.
$ApplicationVersion = [system.Collections.ArrayList]@()
#filter only uri contains the Notepad++ versions.
foreach ($SiteHref in $SiteHrefs)
{
if ($SiteHref.href -match "https://notepad-plus-plus.org/downloads/v\d.*/$")
{
$UrlVersion = $SiteHref.href -replace "https://notepad-plus-plus.org/downloads/", ""
$VersionNumber = $UrlVersion -replace "/", ""
$ApplicationVersion.Add($VersionNumber) | Out-Null
}
}
#get latest Notepad++ installer version.
$LatestApplicationVersion = $ApplicationVersion[0]
#remove space from installer version.
$VersionNumber = $( $LatestApplicationVersion -replace "v", "" )
#get OS architecture for Notepad++ binary file name.
if ($OSArchitecture[0] -eq $true)
{
#for 32-bit
if ($OSArchitecture[1] -eq "32-bit")
{
#get latest Notepad++ binary file name.
$BinaryFileName = "npp.$VersionNumber.Installer.exe"
}
#for 64-bit
if ($OSArchitecture[1] -eq "64-bit")
{
#get latest Notepad++ binary file name.
$BinaryFileName = "npp.$VersionNumber.Installer.x64.exe"
}
}
#if no available for OS Architecture, then use 32-bit Notepad++ binary file name.
if ($OSArchitecture[0] -eq $false)
{
#get latest Notepad++ binary file name.
$BinaryFileName = "npp.$VersionNumber.Installer.exe"
}
#get full path of Notepad++ binary source url.
$BinarySourceUrl = "https://github.com/notepad-plus-plus/notepad-plus-plus/releases/download/$LatestApplicationVersion/$BinaryFileName"
#get Notepad++ download destination directory for specific user.
$InstallerDownloadDirectory = "$( $env:USERPROFILE )\Downloads\$BinaryFileName"
#download latest Notepad++ binary file from site.
Invoke-WebRequest -Uri $BinarySourceUrl -OutFile $InstallerDownloadDirectory -Verbose -TimeoutSec 60
#throw exception if no available for Notepad++ installer.
if (!$( Test-Path -Path $InstallerDownloadDirectory -PathType Leaf ))
{
Write-Error -Message "[$InstallerDownloadDirectory] does not exist." -Category ObjectNotFound -ErrorAction Stop
}
}
End
{
if (!$( Test-Path -Path $InstallerDownloadDirectory -PathType Leaf ))
{
Write-Error -Message "[$InstallerDownloadDirectory] does not exist." -Category ObjectNotFound -ErrorAction Stop
}
#return full path for Notepad++ installer binary file.
return $InstallerDownloadDirectory
}
}