Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

Powershell script to remote login a sftp server and delete a file

$
0
0

Hi Powershell Guru's,

I have a task that involves to remote login a sftp server and delete all the files from the archive folder that are older than 6 months.

I am new to powershell script, I tried to run my current script mentioned below, from a ps1 file.

When I execute the script, it successfully logs me in the server. However, it's not executing the commands further. In stead, it shows me the command prompt screen, prompting me to enter some command there.

So, I need your help to go further.

param(
[string]$user,
[string]$passwd,
[string]$sftphost,
[string]$state
)

psftp $user@$sftphost -pw $passwd

$TargetFolder = '.. /' + $state + '/ARCHIVE'

#----- define file extension ------#
$Extension = "*.bmp"

#----------- define LastWriteTime parameter based on $Days --------#
$LastWrite = $Now.AddDays(-$Days)

#----------- get files based on LastWrite filter and specified folder --------#
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

foreach ($File in $Files)
{
if ($File -ne $NULL)
{
write-host "Deleting File $File" -ForegroundColor "DarkRed"
Remove-Item $File.FullName | out-null
}
else
{
write-host "No more files to delete!" -Foregroundcolor "Green"
}
}


Viewing all articles
Browse latest Browse all 15370

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>