Skip to content

Instantly share code, notes, and snippets.

@ravenstar
Created June 13, 2018 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravenstar/eec010bbe4ee40ddc42830d1a0b4bfbd to your computer and use it in GitHub Desktop.
Save ravenstar/eec010bbe4ee40ddc42830d1a0b4bfbd to your computer and use it in GitHub Desktop.
## Install Posh-SSH by running the following from Powershell as Admin:
# iex (New-Object Net.WebClient).DownloadString("https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev")
#
## SFTP Config - Change these.
$Server_Ip = '10.10.9.50'
$Username = 'root'
$Password = 'hunter2'
$Upload_Path = '/some/folder/'
## Local filesystem watcher Config - Change these.
$watchDir = "C:\temp\"
$watchFilter = "*.txt
## Do Not Edit Below
$Password_Secure = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($Username, $Password_Secure)
# Load the Posh-SSH module
#Import-Module C:\Temp\Posh-SSH
function uploadFile($fullFileName)
{
$ThisSession = New-SFTPSession -ComputerName $Server_Ip -Credential $Credentials
# Upload local file to remote path
Set-SFTPFile -SessionId ($ThisSession).SessionId -LocalFile $fullFileName -RemotePath $Upload_Path
#Disconnect all SFTP Sessions
Get-SFTPSession | % { Remove-SFTPSession -SessionId ($_.SessionId) }
}
$watcher = New-object System.IO.FileSystemWatcher $watchDir
$watcher.EnableRaisingEvents = $true
$watcher.Filter = $watchFilter
$changed = Register-ObjectEvent $watcher "Changed" -Action {
Write-Host $eventArgs.ChangeType, $eventArgs.Fullpath
uploadFile $eventArgs.Fullpath
}
while($true) {
echo "."
start-sleep -s 10
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment