Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell Error Copying a folder to remote server

am trying for a long time to copy the following folder to remote server but unfortunately giving always error.
I try alot of ways :((((

I really need a help

The code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


$ServerLists = Get-Content -Path "c:\scripts\serverslist.txt"
$NewRFCName = Read-Host -Prompt 'Enter New RFC Name'
 foreach ($server in $ServerLists)
        {
           $from =  "C:\Releases\" + $NewRFCName
           $exchange = New-PSSession -ComputerName $server -Credential $Credential
          #    #######Copy the new pakage to Website folder : #######
          Write-Host "server :$server \\ Copying the new pakgae to Website folder "
          Invoke-Command -Session $exchange -ScriptBlock {Copy-Item $from  C:\site -Recurse}
        }

The Error:

Cannot bind argument to parameter ‘Path’ because it is null.

>Solution :

The scriptblock passed to Invoke-Command will be executed on the remote machine, where the $from variable doesn’t exist.

You can force PowerShell to copy it’s value to the remote session by specifying the using: prefix:

$ServerLists = Get-Content -Path "c:\scripts\serverslist.txt"
$NewRFCName = Read-Host -Prompt 'Enter New RFC Name'

foreach ($server in $ServerLists) {
    $from = "C:\Releases\" + $NewRFCName
    $exchange = New-PSSession -ComputerName $server -Credential $Credential
    #    #######Copy the new pakage to Website folder : #######
    Write-Host "server :$server \\ Copying the new pakgae to Website folder "
    Invoke-Command -Session $exchange -ScriptBlock { Copy-Item $using:from  C:\site -Recurse }
}

Or by explicitly passing it as an argument to the remote session:

Invoke-Command -Session $exchange -ScriptBlock { param([string]$Path) Copy-Item $Path C:\site -Recurse } -ArgumentList $from
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading