Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jonatantwn/cf5a17a80327adcb6a25e11524a766c0 to your computer and use it in GitHub Desktop.
Save Jonatantwn/cf5a17a80327adcb6a25e11524a766c0 to your computer and use it in GitHub Desktop.
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