Copy-Item正在复制意外的文件夹 [英] Copy-Item is copying unexpected folder

查看:80
本文介绍了Copy-Item正在复制意外的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力了解PowerShell如何处理递归和 Copy-Item 命令.

I'm struggling to understand how PowerShell handles recursion and Copy-Item command.

$date=Get-Date -Format yyyyMMdd
$oldfolder="c:\certs\old\$date"
New-PSDrive -Name "B" -PSProvider FileSystem -Root "\\(server)\adconfig"
$lastwrite = (get-item b:\lcerts\domain\wc\cert.pfx).LastWriteTime
$timespan = new-timespan -days 1 -hours 1

Write-Host "testing variables..."
Write-Host " date = $date" `n "folder path to create = $oldfolder" `n 
"timespan = $timespan"

if (((get-date) - $lastwrite) -gt $timespan) {
#older
Write-Host "nothing to update."
}
else {
#newer
Write-Host "newer certs available, moving certs to $oldfolder"
copy-item -path "c:\certs\wc" -recurse -destination $oldfolder 
copy-item b:\lcerts\domain\wc\ c:\certs\ -recurse -force
}

现有文件位于 c:\ certs \ wc \ cert.pfx 我进行了测试",比较了 b:\ lcerts \ domain \ wc \ 文件夹中的 cert.pfx 和当前时间之间的时间.如果证书在过去1天1个小时内已被修改,则脚本应继续:

Existing files exist at c:\certs\wc\cert.pfx I have the "test" comparing the time between the cert.pfx in the b:\lcerts\domain\wc\ folder and the current time . If the cert has been modified in the past 1 day and 1 hour, then the script should continue:

cert.pfx c:\ certs \ wc \复制到c:\ certs \ old \ $ date \ cert.pfx

cert.pfx b:\ lcerts \ domain \ wc复制到c:\ certs \ wc \ cert.pfx

我显然不了解PowerShell的命名法,因为我第一次运行此脚本时,它可以正常工作.第二次在 c:\ certs \ wc \ $ date \ wc \ cert.pfx 中创建另一个文件夹.

I obviously don't understand PowerShell nomenclature for this because the first time I run this script, it works fine. The second time it creates another folder inside c:\certs\wc\$date\wc\cert.pfx.

如何通过"c:\ certs \ wc \ $ date \ cert.pfx 已经存在而失败?"

How do I get it to fail with "c:\certs\wc\$date\cert.pfx already exists?"

我不想通过指定实际文件名来将其限制为 cert.pfx 文件,我希望该文件夹中的所有文件最终都将有多个文件.

I don't want to restrict this to just the cert.pfx file by specifying the actual file name, I want all files in the folder as eventually there will be more than one file.

推荐答案

-Path 参数中指定目录时, Copy-Item 的行为取决于是否在 -Destination 参数中指定的目录存在.

The behavior of Copy-Item when a directory is specified in the -Path parameter depends on whether the directory specified in the -Destination parameter exists.

Copy-Item -Path "c:\certs\wc" -Recurse -Destination "c:\certs\old\$date"

如果 c:\ certs \ old \ $ date 目录不存在,则将复制 wc 目录并将其命名为 c:\ certs \ old\ $ date .

If the c:\certs\old\$date directory does not exist, then the wc directory is copied and named c:\certs\old\$date.

如果存在 c:\ certs \ old \ $ date 目录,则将 wc 目录复制到 c:\ certs \ old \ $ date下目录.因此,它成为 c:\ certs \ old \ $ date \ wc .

If the c:\certs\old\$date directory exists, the wc directory is copied under the c:\certs\old\$date directory. Therefore, it becomes c:\certs\old\$date\wc.

因此,您一定要提前检查目录是否存在.

So you are sure to check in advance if the directory exists.

if(Test-Path $oldfolder) { throw "'$oldfolder' is already exists." }
Copy-Item -Path "c:\certs\wc" -Destination $oldfolder -Recurse

这篇关于Copy-Item正在复制意外的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆