powershell 在此对象上找不到属性‘name’,验证该属性是否存在

jxct1oxe  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(188)

我的脚本正在运行,但我收到以下错误:
在此对象上找不到属性‘name’。验证该属性是否存在。
我在循环复制文件夹。知道这是怎么回事吗?

$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.
gmxoilav

gmxoilav1#

$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
        }
    }
}

相关问题