PowerShell backup Script Issue

Copper Contributor

Dear People, 

 

I'm struggling with a strange issue and I can't see what's wrong and hoping if someone can put some lights. 

 

 

I have few PowerShell scripts to download data from a web server, the problems I'm having.

I'm using SSH key and stored in the same machine. 

 

  • Each time when I tried to run PowerShell scripts it comes up with the error says (doesn't let me log in) "error connecting"
  • Each time when I'm using the same user credentials via GIT BASH it works fine (lets me log in) from the same PC
  • Using the same user credentials via Filezilla (sftp) doesn't let me connect. 

Problem: Powershell can't connect so can't download from the path. 

 

The script I'm using 

 

# defines all variables (we don't acctually need the user and password as we are using a SSH key, but it prompts for user and password despite that, so i left it here just for the sake of it)
$today = get-date -Format dd-MM-yyyy
$password = "PASSWORD"
$User = "USERNAME"
$computername = "DOmain.com"
$website = "WEBSITE"
$key = 'C:\User\Admin\.ssh\exported-openssh'
$source = "/home/USERNAME/www/domain.com/backup/$today"
$port = "1000"
# ---------------------------- Shouldn't be a need to edit code beyond this point -------------------------
$year = get-date -Format yyyy
$localbackup = "E:\Backups\$year\Websites\$website\"
$localreport = "E:\Backups\$year\Reports\"
$path = "E:\Backups\$year\Reports\$today"
$errorlog = "E:\Backups\$year\Reports\$today\Error-logs"
$localreportfile = "E:\Backups\$year\Reports\$today\$today-$website.txt"
$destination = "E:\Backups\$year\Websites\$website\$today"
$timestamp1 = get-date


# compiles and automates credentials pass
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)

# session connection setup
$session = New-SFTPSession -ComputerName $ComputerName -Port $port -Credential $Credentials -keyfile $key

# -------------------------------------- website Backup ---------------------------------------------------

# creates a local directory for reports files
If(!(test-path $path))
{
New-Item -path $localreport -ItemType directory -Name $today
}
Add-Content $localreportfile "$timestamp1 - Backup Started."

# Crates a local directory for backup files
If(!(test-path $destination))
{
New-Item -path $localbackup -ItemType directory -Name $today
}

Add-Content $localreportfile "Files Copied:"
Try {
# download todays website backup and sends to local directory
Get-SFTPChildItem $session -Path $source -Recursive | ForEach-Object{
if ($_.Fullname -like '*')
{
Get-SFTPFile $session -RemoteFile $_.FullName -LocalPath $destination -Overwrite
}

write-output $_.FullName | Out-File -FilePath $localreportfile -Append

}

# Remove website File from SFTP Server (WARNING this will delete the server files)
#Remove-SftpItem -session $session -path $source -Force -Verbose

$timestamp2 = get-date
Add-Content $localreportfile "$timestamp2 - Backup Completed."
}
Catch {
If(!(test-path $errorlog))
{
New-Item -path $path -ItemType directory -Name "Error-logs"
}
Add-Content "$errorlog\$today-error-$website.txt" "webserver-backup-$website.ps1 failed to run"
remove-item "$localreportfile"
}
# -------------------------------------- End session -------------------------------------------------------
Remove-SftpSession $session -verbose

 

I hope to find the correct answer. 

 

Many thanks 

0 Replies