无法在此对象上找到属性“名称”。验证该属性是否存在

问题描述 投票:0回答:1

我的脚本正在运行,但我收到此错误:

无法在此对象上找到属性“名称”。验证该属性是否存在。

我正在循环复制文件夹。知道发生了什么事吗?

    $source50 = "c:\folder\"
    $destination50 = "c:\folder1\"

    for ($i = 1; $i -le 7; $i++) {
        $d = ((Get-Date).AddDays( + $i))
        $d2 = $d.ToString("yyyy-MM-dd")
        $v = Get-ChildItem $source50 -Recurse -Include "$d2"
 foreach ($file in $v) 
      {

               if ( Test-Path  $v.FullName)


                 {  
                    if (-not (Test-Path -Path $destination50$d4)){
                    New-Item -ItemType directory -Path $destination50$d4 

                    }


                     Write-Output "Copy ok " $v.FullName     
                     $bd= Copy-Item $v.FullName -Destination $destination50$d4 
                     break 


                  }

       }

代码给出了错误:

ok C:\folder\2017-12-27
ok C:\folder\2017-12-28
The property 'Name' cannot be found on this object. Verify that the property
exists.
At line:16 char:11
+       if ($v.Name -eq $d2) {
+           ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

ok C:\folder\2017-12-30
The property 'Name' cannot be found on this object. Verify that the property
exists.
powershell
1个回答
0
投票

$ v.Name抛出异常,因为它是一个数组对象而不是“System.IO”。具有属性名称。

尝试使用此代码:

$source50 = "c:\folder\"
$destination50 = "c:\folder1\"

for ($i = 1; $i -le 7; $i++)
{
    $d = ((Get-Date).AddDays( + $i))
    $d2 = $d.ToString("yyyy-MM-dd")
    $v = Get-ChildItem  $source50 -Recurse -include "$d2"
    foreach($file in $v)
    {
        if ($file.Name -eq $d2)
        {
            Copy-Item $file.FullName -Destination $destination50 -Force
            Write-Host "ok" $file.FullName  | Out-File -FilePath  c:\folder\logggg.txt
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.