-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-RemoteFileAge.ps1
More file actions
75 lines (68 loc) · 2.71 KB
/
Get-RemoteFileAge.ps1
File metadata and controls
75 lines (68 loc) · 2.71 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
Function Get-RemoteFileAge {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]$server
,
[Parameter(Mandatory = $false)]
[ValidateSet("ftp", "ftps", "sftp")]
[string]$protocol = 'sftp'
,
[Parameter(Mandatory = $false)]
[int]$port = $(if ($protocol -eq "ftp") {21} elseif ($protocol -eq "ftps") {21} elseif ($protocol -eq "sftp") {22})
,
[Parameter(Mandatory = $false)]
[switch]$activeMode
,
[Parameter(Mandatory = $false)]
[string]$certificate
,
[Parameter(Mandatory = $false)]
[string]$user
,
[Parameter(Mandatory = $false)]
$password
,
[Parameter(Mandatory = $false)]
[string]$remoteFolder
,
[Parameter(Mandatory = $false)]
[string]$remoteFile
)
begin {
Write-Verbose -Message ((Get-ResStr 'STARTING_FUNCTION') -f $myInvocation.Mycommand)
$initialVariables = Get-CurrentVariables -Debug:$DebugPreference
}
process {
if ($password.GetType().Name -eq 'String') {
[securestring]$password = ConvertTo-SecureString -String $password -AsPlainText -Force
}
if (Deny-RemoteFingerprint -server $server -protocol $protocol -port $port) {
Throw ((Get-ResStr('DENY_FINGERPRINT')) -f $server)
} else {
if ($protocol -eq 'sftp') {
$result = Get-SftpFileAge -server $server -port $port -certificate $certificate -user $user -password $password -remoteFolder $remoteFolder -remoteFile $remoteFile
} else {
$result = Get-FtpFileAge -server $server -protocol $protocol -Port $port -activeMode:$activeMode -user $user -password $password -remoteFolder $remoteFolder -remoteFile $remoteFile
}
}
}
end {
Get-CurrentVariables -InitialVariables $initialVariables -Debug:$DebugPreference
Return $result
}
<# Test:
$pesterFolder = Resolve-Path -path ".\source\tests"
$iniPath = Join-Path -path $pesterFolder "pester.ini"
$ini = Read-IniFile -path $iniPath
$path = $ini['SFTP']['SecurePasswordPath']
$path = $path -replace '\$home', $HOME
$secure = Import-Clixml -path $path
$server = $ini['SFTP']['Server']
$user = $ini['SFTP']['User']
$result = Get-RemoteFileAge -server $server -protocol sftp -user $user -password $secure -remoteFile 'License.md'
Write-Host "$result seconds from today"
# $result is a int32 value that indicates the file age until today
# The file 'License.md' example belongs to the ftp server test environment we recommend.
#>
}