Power Shell File Upload to SFTP with File Existence Check - powershell

I have a requirement to upload multiple files to SFTP server from my machine using powershell.i have created the following scripts for the same . the script is working fine . but need to check file existence before upload to SFTP server .
the following are the script i used for upload
$username = "UserName"
$password = "Password" | ConvertTo-SecureString -AsPlainText -Force
$server = "xxxxx"
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList
$username,$password
Get-SFTPSession | Remove-SFTPSession
$SFTPSession=New-SFTPSession -ComputerName $server -Credential $credential -Port xxxx
$FilePath = get-childitem "D:\Test\xx*"
$SftpPath = '/D:/xxx/xxx/Test'
ForEach ($LocalFile in $FilePath)
{
Set-SFTPFile -SessionId $SFTPSession.SessionID -LocalFile $LocalFile.fullname -RemotePath $SftpPath -Overwrite
}
Please any body advice me on to check multiple files existence before uploading and only upload files which is not exist in server .
Thanks in advance

Assuming you are using Posh-SSH, you want to use the Test-SFTPPath cmdlet:
if (Test-SFTPPath -SFTPSession $session -Path $Path)
{
...
}

Related

Check if files exist in a FTP remote directory outside our domain

We have been asked to check if files exists in a SFTP remote directory, on a server outside our organisation, and send an email if it does exist.
I have the IP address, an account, and a password.
I found this powershell script https://www.tech2tech.fr/powershell-surveiller-des-fichiers-avec-envoi-de-mail/ , but... How can i make it work on a remote directory, using credential to access it?
I also try using ftp command, but can't find a way to check if many files exist, and not only one.
Does anyone have a solution or a way to help me?
Thanks in advance !
You need to start from here:
$cred = Get-StoredCredential -Target creds;
$SFTPSession = New-SFTPSession -ComputerName sftp.server.org -Credential $cred
Test-SFTPPath $SFTPSession "/path/filename.txt" # check if file exist
Remove-SFTPSession -SessionID 0 -Verbose
Get-SFTPSession # ensure the session is closed
Than you can add a condition and send mail:
Send-MailMessage -To “<recipient’s email>” -From “<sender’s email>” -Subject
“message subject” -Body “Some text!” -Credential (Get-Credential) -SmtpServer
“<smtp server>” -Port 587
Try using the Posh-SSH module.
You can store your password as a plain string in your script, but it would be better to use Import-Clixml to securely store credentials.
Check out online documentation of both modules.
# Install and import module
Install-Module Posh-SSH
Import-Module Posh-SSH
$host = <sftp-location>
$port = <port>
#Set Credentials
$user = <username>
$pass = ConvertTo-SecureString <password> -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($user, $pass)
$path = <location to check>
# Initiate SFTP session
$session = New-SFTPSession -ComputerName $host -Credential $Credential -Port $port -AcceptKey:$true
# Check if files exist on specified location
Test-SFTPPath -SFTPSession $session -Path $path
# Check if more than one file
$items = Get-SFTPChildItem -SFTPSession $session -Path $path
if($items -gt 1) {
<your code>
}
# close session
Remove-SFTPSession -SFTPSession $session
Thank you all !
With your help I was able to complete the task.
Here is what I used:
Install and import module
Install-Module Posh-SSH
Import-Module Posh-SSH
$SFTPhost = "My_SFTP_Server"
$port = "22"
#Set Credentials
$user = "test_sftp"
$pass = ConvertTo-SecureString -String "******" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $pass
$path = "/Prod/saved"
# Initiate SFTP session
$session = New-SFTPSession -ComputerName $SFTPhost -Credential $Credential -Port $port -AcceptKey:$true
# Check if files exist on specified location
Test-SFTPPath -SFTPSession $session -Path $path
# Check if more than one file
$items = (Get-SFTPChildItem -SFTPSession $session -Path $path | measure-object).count
if($items_path -gt 1) {
Send-MailMessage -To 'toto <toto#MyDomain.com>' -From 'No Reply Check <no_reply_CheckFiles#MyDomain.com>' -Subject 'Files on server' -Body 'There is files in PROD repertory' -SmtpServer “My_SMTP_server_Address”
}
# close session
Remove-SFTPSession -SFTPSession $session

Creating PowerShell File Using New-Item & Add-Content Cmdlets

This is the first time posting on SO (and really on any public platform), so I apologize if this question was already answered or if the question isn't structured appropriately.
I am brand new to creating PowerShell Scripts and I'm hoping someone can assist me on my new scripting adventure!
The script I am trying to create is to gather user input on OS user credentials, the OS user password, and the remote server's IP Address\FQDN\Hostname. From there I want to create a ps1 file that a scheduled task will point to, which will copy a particular folder from one server to a remote server on a daily basis (replacing the remote servers folder). I am getting caught up on properly adding the needed lines using the New-Item and Add-Content cmdlets
Here is my script I created so far:
#Clear the Screen
Clear-Host
#Create Variable for OS User Account
$User = read-host "What is the OS User that has permissions to the remote web server?"
#Clear the Screen
Clear-Host
#Password for OS User Account
$Password = Read-Host "What is the password of the OS account that has permissions to the remote web server?"
#Clear the Screen
Clear-Host
#Convert Password String
$PWord = ConvertTo-SecureString -String $Password -AsPlainText -Force
#Combine User Name and Password into One Entity
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
#IP Address of Destination ICA Web Server
$DestServer = Read-Host "What is the IP address\FQDN\Hostname of the Destination Web Server"
#Create a PowerShell File that will be used for Task Scheduler
New-Item "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" -ItemType File -Force -Value "$PWord = ConvertTo-SecureString -String $Password -AsPlainText -Force"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" ""
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" ""
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "$Session = New-PSSession -ComputerName $DestServer -Credential $Credential"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "Copy-Item `"C:\Web\Web\Download`" -Destination `"C:\Web\Web\`" -ToSession $Session -Recurse -Force"
This is the results that I get in the ps1 that gets created (Used 'TestUser' for the OS user, 'PW' for the password, and '10.10.10.10' for the remote server):
System.Security.SecureString = ConvertTo-SecureString -String PW -AsPlainText -Force
System.Management.Automation.PSCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList TestUser, System.Security.SecureString
= New-PSSession -ComputerName 10.10.10.10 -Credential System.Management.Automation.PSCredential
Copy-Item "C:\Web\Web\Download" -Destination "C:\Web\Web\" -ToSession -Recurse -Force
On the first line, for some reason it displays 'System.Security.SecureString' rather than the $PWord variable.
The second line displays 'System.Management.Automation.PSCredential' & System.Security.SecureString rather than the $Credential & $PWord variables.
The third line does not display the $Session variable. It also displays 'System.Management.Automation.PSCredential' rather than the $Credential variable.
The fourth line does not display the $Session variable
So it looks like Powershell doesn't like me adding variables using the New-Item & Add-Content cmdlets.
Any input/suggestions is greatly appreciated!! Thank you!
UPDATE: Thank you, mklement0, for providing the missing piece to my puzzle!
Here is an updated working script based off the information provided by mklement0
#Clear the Screen
Clear-Host
#Create Variable for OS User Account
$User = read-host "What is the OS user account that has permissions to the remote web server?"
#Clear the Screen
Clear-Host
#Password for OS User Account
$Password = Read-Host "What is the password of the OS user account that has permissions to the remote web server?"
#Clear the Screen
Clear-Host
#Convert Password String
$PWord = ConvertTo-SecureString -String $Password -AsPlainText -Force
#Combine User Name and Password into One Entity
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
#IP Address of Destination ICA Web Server
$DestServer = Read-Host "What is the IP address, FQDN, or Hostname of the remote web server"
#Create a PowerShell File that will be used for Task Scheduler
New-Item "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" -ItemType File -Force -Value "`$OSUser = `"$User`""
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" ""
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "`$PWord = ConvertTo-SecureString -String `"$Password`" -AsPlainText -Force"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "`$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList `$OSUser, `$PWord"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" ""
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "`$Session = New-PSSession -ComputerName `"$DestServer`" -Credential `$Credential"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "Copy-Item `"C:\Web\Web\Download`" -Destination `"C:\Web\Web\`" -ToSession `$Session -Recurse -Force"
$ characters that you want to retain as-is inside an expandable (double-quoted) string ("...") must be escaped as `$, otherwise they and what follows are subject to string interpolation; e.g.:
$foo = 'bar'
# Note the ` (backtick) before the first $, which escapes it.
# The second $foo, which is unescaped is expanded (interpolated).
"Variable `$foo contains $foo"
The above yields verbatim:
Variable $foo contains bar

PowerShell script to list files from sftp server folder

I am trying to get list of files from an SFTP folder using the following PowerShell script:
$files = Get-SftpChildItem -Host mysftpserver -Port 22 -Username myusername -Password mypassword -RemoteLocation "/MySFTPFolder/outbound/"
ForEach ($f in $files)
{
"test" | Out-File $ConfigFile -Append
}
For some reason I am not able to read the 2 files that are in the remote outbound folder. Is there something I am missing?
You may be running an old version of the Posh-SSH module, since your syntax looks very different than mine, and the fact that you don't have help files installed. You should install the latest version with:
Find-Module Posh-SSH | Install-Module
Then you can create a SFTP session, and use that session to list the child items as you desire:
$userName = 'MyUserName'
$secStringPassword = 'MyPassword' | ConvertTo-SecureString -AsPlainText -Force
$Creds = [pscredential]$credObject = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)
$session = New-SFTPSession -computername mysftpserver -Credential $Creds
$files= Get-SFTPChildItem -SFTPSession $session -Path "/MySFTPFolder/outbound/"
ForEach ($f in $files)
{
"test" | Out-File $ConfigFile -Append
}

What is the best method to edtit a .txt file on a FTP server in the same time for 100 persons for example?

What is the best method to edit a .txt file on a FTP server?
I have a remote ftp file1.txt .100 persons or 100 servers mut add a value $y=" yes" to file1.txt in the same time...So it is not possible to use the method in my case:
Download file
Edit file locally (in memory)
Upload file
I Want to do this and I can use only a method with FTP for security reasons:
$pathftp="ftp:\\......file1.txt"
$y=yes
add-content - path $pathftp -$y
I have tried this but it doesn't work:
$a="test5"
$FTPServer = 'ftp://localhost'
$FTPUsername = 'username'
$FTPPassword = 'password'
$FTPSecurePassword = ConvertTo-SecureString -String $FTPPassword -asPlainText -Force
$FTPCredential = New-Object System.Management.Automation.PSCredential($FTPUsername,$FTPSecurePassword)
Set-FTPConnection -Credentials $FTPCredential -Server $FTPServer -Session MySession -UsePassive
$Session = Get-FTPConnection -Session MySession
$b=Get-FTPChildItem -Session $Session -Path / -Recurse | Where-Object{$_.Name -like "file1.txt"}
Add-Content -Path $b -Value -$a

Powershell : Module PSFTP - Download all files with a date more recent than local files

I'm new in Powershell and I want to download all the files whose date is more recent than the local files (folders and sub-folders of the FTP path).
Here is my current script :
$FTPServer = "ftp://xx.xx.xx.xx/xxxx/xxxx/xxxx"
$FTPSessionName = "GERA_FTPSession"
$localPath= "E:\xxxx\"
$username = "***"
$password = "*******"
# Import a module of FTP code
# https://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb
Import-Module PSFTP
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
# connnection FTP
Set-FTPConnection -Credentials $Cred -Server $FTPServer -Session GERA_FTP -UsePassive
$FTPSessionName = Get-FTPConnection -Session GERA_FTP
#import files and folders
Get-FTPChildItem -Session $FTPSessionName -Recurse | Get-FTPItem -Session $FTPSessionName -localpath $localPath -RecreateFolders -Overwrite -Verbose
Thanks for your help
Arnaud