Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.