Post a reply

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Re: This Powershell script works except...

martin wrote:

You have to call $removeResult.Check() right after $removeResult = $session.RemoveFiles($RemovalFile)

This worked perfectly.
Thanks so much!

Re: This Powershell script works except...

You have to call $removeResult.Check() right after $removeResult = $session.RemoveFiles($RemovalFile)

This Powershell script works except...

I've kludged this together while learning to script and it works except when there are no files older than $RemovalDate. Running the script in the IDE throws no exception but when running with task scheduler, the script throws the error "You cannot call a method on a null-valued expression." and fires off the email.
I've tried excluding this exception with -ErrorAction and -ErrorVariable but neither produced the desired results.

I would like to check if there are any files older than $RemovalDate and if NOT exit otherwise continue the script and only email if there is a problem deleting old files that are actually there.

Thanks in advance for any help.

    # Load WinSCP .NET assembly

    Add-Type -Path "WinSCPnet.dll"
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = ""
        PortNumber = "12321"
        UserName = "*********"
        Password = "******************"
        SshHostKeyFingerprint = "ssh-rsa 1024 4d:...:71"
   $session = New-Object WinSCP.Session
        # Date delimiter
            $RemovalDate = (Get-Date).Adddays(-3)
        # Connect
        # List of files in the directory
            $Directory = $session.ListDirectory("/")

     foreach ($FileInfo in $Directory.Files)
     # Compare LastWriteTime and ignores directory listing for ".."
        If ($FileInfo.Name -ne $null)
            If ($FileInfo.LastWriteTime -lt $RemovalDate -and $FileInfo.Name -ne "..") 
                $RemovalFile = ($FileInfo.Name)
                write-host $RemovalFile
                $removeResult = $session.RemoveFiles($RemovalFile)

        # Throw on error
exit 0

## Error results emailed
$From = ""
$To = ""
$Date = (Get-Date)
$DateStr = $Date.ToString("MMddyyyy")
$SubjectStr = "Delete Failure " + $($_.Exception.Message)
$Subject = $SubjectStr + $DateStr
$Body = @"


$SMTPServer = ""
$SMTPPort = "587"
$password = Get-Content "C:\Pwd2\Pwd.txt" | ConvertTo-SecureString
$login = ""
$credentials = New-Object System.Management.Automation.Pscredential -Argumentlist $login,$password
Send-MailMessage -From $From -to $To -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -UseSsl -Port $SMTPPort `
-Credential ($credentials) #-Attachments $Attachment

Write-Host "Error: $($_.Exception.Message)"
exit 1

    # Disconnect, clean up