-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInstall-WinSCPNetAssembly.ps1
127 lines (87 loc) · 4.48 KB
/
Install-WinSCPNetAssembly.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
#Requires -Version 3.0
Function Install-WinSCPNetAssembly {
<#
.SYNOPSIS
This cmdlet is used to install/update WinSCP NET Assembly DLL on a machine. It can also be used to simply download the installer
.DESCRIPTION
Download the lates WinSCP installed for Windows, verify that hash and instal the file
.PARAMETER OutFile
Define where to save the NET assembly zip file. Default location is your Temp directory
.PARAMETER DownloadOnly
Switch parameter to specify you only want to download the installer
.PARAMETER TryTLSv13
Switch parameter that tells PowerShell to try download file using TLSv1.3. This seems to fail as of 3/28/2023 due to 1.3 being so new
.EXAMPLE
Install-WinSCPNetAssembly
# This example downloads the WinSCP automation and verifies the checksum before installing it
.EXAMPLE
Install-WinSCPNetAssembly -OutFile "$env:TEMP\WinSCP-Version-Automation.zip"
# This example downloads the WinSCP automation and verifies the checksum before installing it
.EXAMPLE
Install-WinSCPNetAssembly -OutFile "$env:TEMP\WinSCP-Version-Automation.zip" -DownloadOnly
# This example downloads the WinSCP automation and verifies the checksum
.NOTES
Author: Robert Osborne
Contact: [email protected]
.LINK
https://www.vinebrooktech.com
.INPUTS
None
.OUTPUTS
System.Management.Automation.PSObject
#>
[OutputType([System.Management.Automation.PSObject])]
[CmdletBinding()]
param(
[Parameter(
Position=0,
Mandatory=$False
)] # End Parameter
[ValidateScript({$_ -like "*.zip"})]
[String]$OutFile = "$env:TEMP\WinSCP-Version-Automation.zip",
[Parameter(
Position=1,
Mandatory=$False
)] # End Parameter
[String]$DestinationPath = "$env:ProgramData\WinSCP",
[Parameter(
Mandatory=$False
)] # End Parameter
[Switch]$DownloadOnly,
[Parameter(
Mandatory=$False
)] # End Parameter
[Switch]$TryTLSv13
) # End param
$TlsVersion = "TLSv1.2"
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
If ($TryTLSv13.IsPresent) {
$TlsVersion = "TLSv1.3"
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls13
} # End If
Write-Verbose -Message "[v] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') utilizing $TlsVersion"
$DLUserAgent = "Wget"
$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox
$DlUrl = 'https://winscp.net/eng/download.php'
$Version = ((Invoke-WebRequest -Uri $DlUrl -UseBasicParsing -Method GET -UserAgent $UserAgent -ErrorAction Stop -Verbose:$False).Links | Where-Object -FilterScript { $_.outerHTML -like "*List of all changes*" }).href.Split('=')[-1]
$Uri = "https://winscp.net/download/WinSCP-$Version`-Automation.zip"
Write-Verbose -Message "[v] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Downloading WinSCP NET Assembly from their website"
Try {
Invoke-WebRequest -Uri $Uri -UseBasicParsing -Method GET -UserAgent $DLUserAgent -OutFile $OutFile -ContentType 'application/octet-stream' -Verbose:$False | Out-Null
$CheckSum = (((Invoke-WebRequest -Uri $Uri -UseBasicParsing -Method GET -UserAgent $UserAgent -Verbose:$False).Content).Split("`n") | Select-String -Pattern "SHA-256:").ToString().Trim().Replace('</li>','').Split(" ")[-1]
} Catch {
Throw $Error[0]
} # End Try Catch Catch
$FileHash = (Get-FileHash -Path $OutFile -Algorithm SHA256).Hash.ToLower()
If ($FileHash -eq $CheckSum) {
Write-Verbose -Message "[v] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Successfully verified hash of newly downloaded file for WinSCP"
If ($DownloadOnly.IsPresent -and (Test-Path -Path $OutFile)) {
Write-Output -InputObject "[*] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Successfully downloaded file and verified hash.`n[i] File saved to $OutFile"
} Else {
Write-Verbose -Message "[v] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Extracting zip archive to $DestinationPath"
Expand-Archive -Path $OutFile -DestinationPath $DestinationPath -Force -Confirm:$False -Verbose:$False
} # End If Else
} Else {
Throw "[x] $(Get-Date -Format 'MM-dd-yyyy hh:mm:ss') Failed to validate hash of newly downloaded file for WinSCP"
} # End If Else
} # End Function Install-WinSCPNetAssembly