Powershell:复制项通配符目标路径

时间:2016-03-17 01:14:51

标签: powershell path copy-item

尝试将更新的快捷方式复制到通配符路径。当我在本地计算机上的测试场景中运行代码时,代码可以正常运行:

$Source1 = "C:\Temp\Updated Shortcut\MyShortcut.lnk"
$destination1 = "U:\Users\*\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\MyShortcut.lnk"
Get-ChildItem -Path $destination1 | ForEach-Object { Copy-Item -Path $Source1 -Destination $_.DirectoryName }

但是在生产目标路径上运行它不起作用:

$Source1 = "C:\Temp\Updated Shortcut\MyShortcut.lnk"
$destination1 = "U:\Users\JohnSmith\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\MyShortcut.lnk"
Get-ChildItem -Path $destination1 | ForEach-Object { Copy-Item -Path $Source1 -Destination $_.DirectoryName }

如果我删除通配符并使用实际路径,它也会起作用:

{{1}}

U:\ Users文件夹中包含1181个文件夹(1181个用户各占一个),所以不确定这是否也是个问题?

1 个答案:

答案 0 :(得分:4)

仅仅因为代码可以写在一行或使用管道,并不意味着代码易于调试或维护。

打破你的代码,调试它,添加一些日志记录等等。

$Source1 = "C:\Temp\Updated Shortcut\MyShortcut.lnk"
$destination1 = "U:\Users\*\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\MyShortcut.lnk"
$items = Get-ChildItem -Path $destination1

Write-Verbose "Number of items: $($items.Count)" -Verbose

foreach ($item in $items)
{
    Write-Verbose "Item: $item" -Verbose
    # use -Force here? Does $_ have a DirectoryName property?
    #
    #Copy-Item -Path $Source1 -Destination $_.DirectoryName

    Copy-Item -Path $Source1 -Destination $item -Force -WhatIf
}

我的猜测是,一旦你开始研究诊断,你就会迅速找出问题。