将多个文件复制到远程位置并在powershell中显示进度详细信息

时间:2016-03-17 08:30:35

标签: powershell

我正在尝试将文件从本地计算机复制到远程位置。 当我运行脚本时,它将检查远程位置上存在的文件夹。如果没有创建,那么它将创建一个目录。创建目录后,它将从源位置复制所有文件。

我的脚本工作正常,但我想在屏幕上输出正在复制的文件。

以下是可以正常复制文件的代码,但它没有显示正在复制的文件名。

$machine = $env:COMPUTERNAME

$dest= "\\192.168.1.5\d$\test\"

$source = "C:\windows\logs\"

$newPath = Join-Path $dest -childpath $machine

 if(!(Test-Path -Path $newPath )){
        New-Item $newPath -type directory
             foreach ($file in $source)
             {
                    write-host "Copying Files: " $file  -foregroundcolor DarkGreen -backgroundcolor white
                    Copy-Item $file -Destination $newPath -force

             }

        }else{
        foreach ($file in $source)
        {
        write-host "Copying Files: " $file  -foregroundcolor DarkGreen -backgroundcolor white
    Copy-Item -Path $file -Destination $newPath -recurse -Force

        }   
    }

1 个答案:

答案 0 :(得分:2)

此代码应该有效。我修改了if语句,因此你没有多余的代码和改编的$ source,所以foreach将使用你的写进程

$machine = $env:COMPUTERNAME

$dest= "\\192.168.1.5\d$\test\"

$sourcePath = 'C:\windows\logs\'

$source = Get-ChildItem $sourcePath

$newPath = Join-Path $dest -childpath $machine

if(!(Test-Path -Path $newPath )){
    New-Item $newPath -type directory
}

$count = $source.count
$operation = 0

foreach ($file in $source)
{
    $operation++
    write-host 'Copying File: ' $file  -foregroundcolor DarkGreen -backgroundcolor white
    Write-Progress -Activity 'Copying data' -Status 'Progress' -PercentComplete ($operation/$count*100)
    Copy-Item $file -Destination $newPath -force

}
相关问题