Created
November 16, 2020 04:01
-
-
Save cybergavin/320de8b30935aa7e24119b068c123e2c to your computer and use it in GitHub Desktop.
SCP files from remote host
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # --------------------------------------------------------------------- | |
| # SCP files from remote host | |
| # --------------------------------------------------------------------- | |
| # | |
| # Variables | |
| # | |
| $myHost=$env:COMPUTERNAME | |
| $scriptPath=[string] ($MyInvocation.MyCommand.Definition) | |
| $rootDir="$($scriptPath.substring(0,$scriptPath.LastIndexOf('\')))" | |
| $scpKey="${rootDir}\id_rsa" | |
| $sshConf="${rootDir}\sshconfig.txt" # Required to configure OpenSSH client to prefer public key authentication | |
| $logFile="${rootDir}\$($scriptName.replace(`".ps1`",`".log`"))" | |
| $remoteHost="abc.contoso.com" | |
| $remoteUser="cybergavin" | |
| $remoteFiles="/somedir/files*" | |
| $scpCmd="C:\Windows\System32\OpenSSH\scp.exe" # Requires the OpenSSH client Windows Capability installed | |
| # | |
| # Functions | |
| # | |
| function logMessage ($severity, $message) | |
| { | |
| $logTime=$(Get-Date -Format "dd-MMM-yyyy : HH:mm:ss") | |
| if ( $severity -ceq 'INFO' ) { | |
| Write-Output "$logTime : $myHost : $severity : $message" >> $logFile | |
| } else { | |
| Write-Output "$logTime : $myhost : $severity : $message" >> $logFile | |
| } | |
| } | |
| # | |
| # Main | |
| # | |
| & $scpCmd -i $scpKey -F $sshConf ${remoteUser}@${remoteHost}:${remoteFiles} $rootDir | |
| if ($?) { | |
| logMessage "INFO" "Fetched files from $remoteHost" | |
| } else { | |
| logMessage "ERROR" "Failed to fetch files from $remoteHost" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment