Copy-Item is copying unexpected folder

早过忘川 提交于 2021-01-29 08:03:47

问题


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
}

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:

Copy cert.pfx from c:\certs\wc\ to c:\certs\old\$date\cert.pfx

Copy cert.pfx from b:\lcerts\domain\wc to c:\certs\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.

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

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.


回答1:


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"

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

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



回答2:


You are not testing if the destination folder exists. Seeing you are creating its name using a current date, it is more than likely this folder does not yet exist, so you need to create it first.

Also, There should be no need to use the New-PSDrive cmdlet, because Copy-Item is perfectly able to use UNC paths.

Something like this perhaps:

$server      = '<NAME OF THE SERVER>'
$serverPath  = "\\$server\adconfig\lcerts\domain\wc"
$testFile    = Join-Path -Path $serverPath -ChildPath 'cert.pfx'
$localPath   = 'c:\certs\wc'
$date        = Get-Date -Format yyyyMMdd
$timespan    = New-TimeSpan -Hours 1 -Minutes 1
$oldfolder   = "c:\certs\old\$date"

# check if this output path exists. If not, create it
if (!(Test-Path -Path $oldfolder -PathType Container)) {
    Write-Host "Creating folder '$oldfolder'"
    New-Item -ItemType Directory -Path $oldfolder | Out-Null
}

Write-Host "testing variables..."
Write-Host "date = $date`r`nfolder path to create = $oldfolder`r`ntimespan = $timespan"

# test the LastWriteTime property from the cert.pfx file on the server
$lastwrite = (Get-Item $testFile).LastWriteTime
if (((Get-Date) - $lastwrite) -gt $timespan) {
    #older
    Write-Host "Nothing to update."
}
else {
    #newer
    Write-Host "Newer cert(s) available; copying all from '$localPath' to '$oldfolder'"
    Copy-Item -Path $localPath -Filter '*.pfx' -Destination $oldfolder
    Copy-Item -Path $serverPath -Filter '*.pfx' -Destination $localPath -Force
}   


来源:https://stackoverflow.com/questions/55447917/copy-item-is-copying-unexpected-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!