PowerShell backup Script Issue

%3CLINGO-SUB%20id%3D%22lingo-sub-874156%22%20slang%3D%22en-US%22%3EPowerShell%20backup%20Script%20Issue%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-874156%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20People%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20struggling%20with%20a%20strange%20issue%20and%20I%20can't%20see%20what's%20wrong%20and%20hoping%20if%20someone%20can%20put%20some%20lights.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20few%20PowerShell%20scripts%20to%20download%20data%20from%20a%20web%20server%2C%20the%20problems%20I'm%20having.%3C%2FP%3E%3CP%3EI'm%20using%20SSH%20key%20and%20stored%20in%20the%20same%20machine.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3EEach%20time%20when%20I%20tried%20to%20run%20PowerShell%20scripts%20it%20comes%20up%20with%20the%20error%20says%20(doesn't%20let%20me%20log%20in)%20%22error%20connecting%22%3C%2FLI%3E%3CLI%3EEach%20time%20when%20I'm%20using%20the%20same%20user%20credentials%20via%20GIT%20BASH%20it%20works%20fine%20(lets%20me%20log%20in)%20from%20the%20same%20PC%3C%2FLI%3E%3CLI%3EUsing%20the%20same%20user%20credentials%20via%20Filezilla%20(sftp)%20doesn't%20let%20me%20connect.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%3C%2FUL%3E%3CP%3EProblem%3A%20Powershell%20can't%20connect%20so%20can't%20download%20from%20the%20path.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20script%20I'm%20using%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRIKE%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23%20defines%20all%20variables%20(we%20don't%20acctually%20need%20the%20user%20and%20password%20as%20we%20are%20using%20a%20SSH%20key%2C%20but%20it%20prompts%20for%20user%20and%20password%20despite%20that%2C%20so%20i%20left%20it%20here%20just%20for%20the%20sake%20of%20it)%3C%2FEM%3E%3C%2FFONT%3E%3C%2FSTRIKE%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24today%20%3D%20get-date%20-Format%20dd-MM-yyyy%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24password%20%3D%20%22PASSWORD%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24User%20%3D%20%22USERNAME%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24computername%20%3D%20%22DOmain.com%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24website%20%3D%20%22WEBSITE%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24key%20%3D%20'C%3A%5CUser%5CAdmin%5C.ssh%5Cexported-openssh'%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24source%20%3D%20%22%2Fhome%2FUSERNAME%2Fwww%2Fdomain.com%2Fbackup%2F%24today%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24port%20%3D%20%221000%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23%20----------------------------%20Shouldn't%20be%20a%20need%20to%20edit%20code%20beyond%20this%20point%20-------------------------%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24year%20%3D%20get-date%20-Format%20yyyy%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24localbackup%20%3D%20%22E%3A%5CBackups%5C%24year%5CWebsites%5C%24website%5C%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24localreport%20%3D%20%22E%3A%5CBackups%5C%24year%5CReports%5C%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24path%20%3D%20%22E%3A%5CBackups%5C%24year%5CReports%5C%24today%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24errorlog%20%3D%20%22E%3A%5CBackups%5C%24year%5CReports%5C%24today%5CError-logs%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24localreportfile%20%3D%20%22E%3A%5CBackups%5C%24year%5CReports%5C%24today%5C%24today-%24website.txt%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24destination%20%3D%20%22E%3A%5CBackups%5C%24year%5CWebsites%5C%24website%5C%24today%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24timestamp1%20%3D%20get-date%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23%20compiles%20and%20automates%20credentials%20pass%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24secpasswd%20%3D%20ConvertTo-SecureString%20%24password%20-AsPlainText%20-Force%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24Credentials%20%3D%20New-Object%20System.Management.Automation.PSCredential(%24User%2C%20%24secpasswd)%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23%20session%20connection%20setup%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24session%20%3D%20New-SFTPSession%20-ComputerName%20%24ComputerName%20-Port%20%24port%20-Credential%20%24Credentials%20-keyfile%20%24key%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23%20--------------------------------------%20website%20Backup%20---------------------------------------------------%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23%20creates%20a%20local%20directory%20for%20reports%20files%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3EIf(!(test-path%20%24path))%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7B%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3ENew-Item%20-path%20%24localreport%20-ItemType%20directory%20-Name%20%24today%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7D%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3EAdd-Content%20%24localreportfile%20%22%24timestamp1%20-%20Backup%20Started.%22%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23%20Crates%20a%20local%20directory%20for%20backup%20files%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3EIf(!(test-path%20%24destination))%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7B%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3ENew-Item%20-path%20%24localbackup%20-ItemType%20directory%20-Name%20%24today%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7D%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3EAdd-Content%20%24localreportfile%20%22Files%20Copied%3A%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3ETry%20%7B%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23%20download%20todays%20website%20backup%20and%20sends%20to%20local%20directory%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3EGet-SFTPChildItem%20%24session%20-Path%20%24source%20-Recursive%20%7C%20ForEach-Object%7B%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3Eif%20(%24_.Fullname%20-like%20'*')%20%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7B%20%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3EGet-SFTPFile%20%24session%20-RemoteFile%20%24_.FullName%20-LocalPath%20%24destination%20-Overwrite%20%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7D%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3Ewrite-output%20%24_.FullName%20%7C%20Out-File%20-FilePath%20%24localreportfile%20-Append%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7D%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23%20Remove%20website%20File%20from%20SFTP%20Server%20(WARNING%20this%20will%20delete%20the%20server%20files)%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23Remove-SftpItem%20-session%20%24session%20-path%20%24source%20-Force%20-Verbose%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%24timestamp2%20%3D%20get-date%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3EAdd-Content%20%24localreportfile%20%22%24timestamp2%20-%20Backup%20Completed.%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7D%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3ECatch%20%7B%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3EIf(!(test-path%20%24errorlog))%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7B%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3ENew-Item%20-path%20%24path%20-ItemType%20directory%20-Name%20%22Error-logs%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7D%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3EAdd-Content%20%22%24errorlog%5C%24today-error-%24website.txt%22%20%22webserver-backup-%24website.ps1%20failed%20to%20run%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3Eremove-item%20%22%24localreportfile%22%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%7D%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3E%23%20--------------------------------------%20End%20session%20-------------------------------------------------------%3C%2FEM%3E%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23000000%22%3E%3CEM%3ERemove-SftpSession%20%24session%20-verbose%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20to%20find%20the%20correct%20answer.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMany%20thanks%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-874156%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Epowershell%20script%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Occasional 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