-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-SFTPItem02.ps1
45 lines (35 loc) · 1.8 KB
/
Get-SFTPItem02.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
#Install module
#install-module posh-ssh -Confirm:$false
#Get-Command *sftp*
#This script connects to a SFTP server
#Downloads files from SFTP to a folder with today's date
#Compares file size with files in the main folder
#If there is a difference copies new files to main folder
#Removes folders older than 3 days
$ErrorActionPreference = "Stop"
$password = ConvertTo-SecureString "NCrcANVLKq1YV6mknzbkboioLTx" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("stwebsprint.user2", $password)
$session = New-SFTPSession -Computername stwebsprint.blob.core.windows.net -Port 22 -Credential $creds -Verbose -AcceptKey
$source = "/webdoc"
$items = Get-SFTPChildItem -Recursive $session -Path $source
$localDestination = "D:\AVD"
$date = Get-Date -Format 'yyyy-MM-dd-HH-mm'
if (Test-Path -Path "$localDestination\Filer_$date") {
Write-Output "Folder Filer_$date exists"
}else{
New-Item -Path "$localDestination\Filer_$date" -ItemType Directory -Force
}
foreach ($item in $items) {
Get-SFTPItem -SessionId $session.SessionID -Path $item.FullName -Destination "$localDestination\Filer_$date" -Force
}
$latestFolder = Get-ChildItem -Path $localDestination -Directory | Sort-Object -Descending -Property LastWriteTime | select -First 1
$lastestFiles = Get-ChildItem -Path $localDestination\$latestFolder
foreach ($file in $lastestFiles) {
if($file.Length -eq (Get-ChildItem -Path $localDestination | where {$_.Name -eq $file.Name}).Length) {
Write-Output "The same size: $file"}
else{
Copy-Item -Path $localDestination\$latestFolder\$file -Destination $localDestination -Force
}
}
$oldFolders = Get-ChildItem $localDestination | where {$_.Name -match "Filer" -and $_.CreationTime -lt (Get-Date).AddDays(-3)}
$oldFolders | Remove-Item -Force -Confirm:$false