Skip to content

Instantly share code, notes, and snippets.

@cybergavin
Created November 16, 2020 04:01
Show Gist options
  • Select an option

  • Save cybergavin/320de8b30935aa7e24119b068c123e2c to your computer and use it in GitHub Desktop.

Select an option

Save cybergavin/320de8b30935aa7e24119b068c123e2c to your computer and use it in GitHub Desktop.
SCP files from remote host
# ---------------------------------------------------------------------
# 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