Created
June 15, 2020 11:10
-
-
Save Jonatantwn/cf5a17a80327adcb6a25e11524a766c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
$domainname = ( | |
"testmappe.dk", | |
"testmappe2.dk" | |
), | |
$fullremotepathdelete = $remotepathsdelete, | |
$fileNameDelete = "*", | |
$remotepathsdelete = ( | |
"$($dn)/pluginfil/$($fileNameDelete)", | |
"$($dn)/mappe/$($fileNameDelete)" | |
), | |
$fileNameUpload = "*", | |
$localpathupload = "C:\Users\Dern\Desktop\REPLACEME\*", | |
$remotepathsupload = ( | |
"$($dn)/$($fileNameUpload)", | |
"$($dn)/$($fileNameUpload)" | |
) | |
) | |
try | |
{ | |
# Load WinSCP .NET assembly | |
Add-Type -Path "C:\Users\Dern\Desktop\WINSCP PS\WinSCPnet.dll" | |
# Setup session options | |
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{ | |
Protocol = [WinSCP.Protocol]::ftp | |
HostName = "xxx" | |
UserName = "xxx" | |
Password = "xxx" | |
} | |
$session = New-Object WinSCP.Session | |
# REMOVE UNWANTED DATA | |
foreach ($dn in $domainname) { | |
try | |
{ | |
# Connect | |
$session.SessionLogPath = "C:\Users\Dern\Desktop\WINSCP-Logs\deletelog.txt" | |
$session.Open($sessionOptions) | |
foreach ($rd in $remotepathsdelete) { | |
# Download files | |
$transferOptions = New-Object WinSCP.TransferOptions | |
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary | |
# Download the file and throw on any error | |
# $Session.RemoveFiles("$rd", "C:\Users\Dern\Desktop\WINSCP PS DOWNLOAD\", $False, $transferOptions) | |
$removalResult = $session.RemoveFiles("$rd") | |
if ($removalResult.IsSuccess) | |
{ | |
Write-Host "Removing of file $rd succeeded" | |
} | |
else | |
{ | |
Write-Host "Removing of file $rd failed" | |
} } | |
# UPLOAD WANTED DATA | |
foreach ($rp in $remotepathsupload) | |
{ | |
Write-Host "Uploading to $localpathupload $rp ..." | |
$session.PutFiles($localpathupload, $rp).Check() | |
} | |
} | |
finally | |
{ | |
# Disconnect, clean up | |
$session.Dispose() | |
} | |
} | |
exit 0 | |
} | |
catch | |
{ | |
Write-Host "Error: $($_.Exception.Message)" | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment