PowerShell 将文件夹复制到远程服务器时出错

PowerShell Error Copying a folder to remote server

我尝试了很长时间将以下文件夹复制到远程服务器,但不幸的是总是出错。 我尝试了很多方法:((((

我真的需要帮助

代码:


$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}
        }

错误:

无法将参数绑定到参数 'Path',因为它为空。

传递给 Invoke-Command 的脚本块将在远程计算机 上执行,其中 $from 变量不存在。

您可以通过指定 using: 前缀强制 PowerShell 将它的值复制到远程会话:

$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 }
}

或者将其作为参数显式传递给远程会话:

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