Author Posts

January 26, 2017 at 3:00 am

I'm trying to pass some parameters to a script that runs PSCP to transfer files from a Windows server to Linux servers. This is primarily the line that is wrapped in Powershell script:

$cmd = Invoke-Expression 'pscp -v -pw Password! -P -batch d:\Test.log joe@box01:/home/joe/Downloads/ 2>&1'

I like to use the script as so:
.\PSCPTransfer -pw Password! -P 22 -batch d:\Filename.log user@servername:/home/user/

Help commands for pscp:

PuTTY Secure Copy client
Release 0.67
Usage: pscp [options] [user@]host:source target
pscp [options] source [source...] [user@]host:target
pscp [options] -ls [user@]host:filespec
Options:
-V print version information and exit
-pgpfp print PGP key fingerprints and exit
-p preserve file attributes
-q quiet, don't show statistics
-r copy directories recursively
-v show verbose messages
-load sessname Load settings from saved session
-P port connect to specified port
-l user connect with specified username
-pw passw login with specified password
-1 -2 force use of particular SSH protocol version
-4 -6 force use of IPv4 or IPv6
-C enable compression
-i key private key file for user authentication
-noagent disable use of Pageant
-agent enable use of Pageant
-hostkey aa:bb:cc:...
manually specify a host key (may be repeated)
-batch disable all interactive prompts
-unsafe allow server-side wildcards (DANGEROUS)
-sftp force use of SFTP protocol

##########################
Script
##########################

$Date = Get-Date
$log = "D:\logs\PSCPLog.log"
$content1 = '################################################'
$content2 = 'Transfer successful!'

Param(
[Parameter(ParameterSetName='Password',Mandatory=$True,Position=1)]
[string]$pw,

[Parameter(ParameterSetName='Port',Mandatory=$True)]
[string]$P,

[Parameter(ParameterSetName='Batch',Mandatory=$True)]
[string]$batch
)

#$cmd = Invoke-Expression 'pscp -v -pw Password! -P -batch d:\20170125.log joe@box01:/home/joe/Downloads/ 2>&1'
$cmd = Invoke-Expression "pscp -v $pw $P $batch"
if ($LASTEXITCODE -ne 0) {

Write-Output $content1 | out-file $log -Append -Force
Add-content $log $Date
Add-Content $log $cmd[1]
Add-Content $log $cmd[4]
Add-Content $log $cmd[6]
Add-Content $log $cmd[7]
Add-Content $log $cmd[8]
Add-Content $log $cmd[13]
Add-Content $log $cmd[14]
Add-Content $log $cmd[15]
Add-Content $log $cmd[16]
Add-Content $log $cmd[17]
Add-Content $log $cmd[18]
Add-Content $log `n
Add-Content $log `n

}

else {
Add-content $log $content1
Add-Content $log $Date
Add-Content $log $content2
Add-content $log $content1
Add-Content $log `n
Add-Content $log `n

}

January 26, 2017 at 8:56 am

What does not work? At a first glance your script should work.

February 1, 2017 at 4:20 pm

It does work. I made a fundamental mistake in not declaring the Param section first. Thanks!