I'm new to PowerShell scripts and would like to add error logging to the script below. The script is sending *.txt files to an FTP server.
How can I improve the error logging in this script? I'd like to catch any potential errors that might be triggered and maybe email them to an email address.
Right now, it's giving me the following error "Cannot find a variable with the name 'MyTestSession'." Why is this?
Import-Module PSFTP
[string] $User = 'testuser'
[string] $LocalFolder ='c:\EDI\Export\'
[string] $BackupFolder = 'C:\EDI\Backup\'
[string] $RemoteFolder ='/Inbound'
[string] $FTPServ = '00.000.00.000'
[object] $objCred = $null
[System.Security.SecureString] $Pw
$Pw= ConvertTo-SecureString -String "2}bIed_d" -AsPlainText -Force
$objCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($User, $Pw)
$GetFiles=Get-ChildItem -Filter *.txt -Path $LocalFolder
if ($null -ne $GetFiles) {
Set-FTPConnection -Credentials $objCred -Server $FTPServ -Session MyTestSession -UsePassive -UseBinary
$Session = Get-FTPConnection -Session MyTestSession
foreach ($i in $GetFiles)
{
$timestamp=(Get-Date).tostring("MMddyyyyhhmmss")
$NewFile = $i.FullName -Replace ".txt", ".$timestamp.txt"
Rename-Item -Path $i.FullName -NewName $NewFile
Add-FTPItem -Session $Session -Path $RemoteFolder -Overwrite -LocalPath $NewFile
move-Item $NewFile -Destination $BackupFolder
set-content C:\EDI\FTPScripts\Errors\Export.log $error
}
}