我正在尝试将文件从本地计算机复制到远程位置。当我运行脚本时,它将检查远程位置上的文件夹是否存在。如果未创建,它将创建一个目录。创建目录后,它将从源位置复制所有文件。
我的脚本为此工作正常,但我想在屏幕上输出正在复制的文件。
下面的代码可以很好地复制文件,但没有显示正在复制的文件名。
$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
}
}
此代码应该起作用。我更改了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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句