Passing parameters to PSCP in Powershell script

This topic contains 2 replies, has 2 voices, and was last updated by  Rocky Cabral 10 months, 2 weeks ago.

  • Author
    Posts
  • #62682

    Rocky Cabral
    Participant

    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

    }

  • #62697

    Daniel Krebs
    Moderator

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

  • #63237

    Rocky Cabral
    Participant

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

You must be logged in to reply to this topic.