簡體   English   中英

我在使用 Copy-Item 和 Veriable 作為目標時遇到問題

[英]I have an issue with using Copy-Item and a Veriable for destination

這可能很容易。 但是我可以弄清楚我的簡單復制腳本出了什么問題。 我有一個共享目錄,我正在從中復制項目。 我正在打印控制台的目標路徑,所以我知道它是正確的但是我收到了一個我不明白的 powershell 錯誤。

這是我的腳本

#Files to copy
#Get Installers from folder
$APPS = Get-ChildItem \\Server1\shared\APPS -Name 

#ForEach loop to identify and move files
ForEach($APP in $APPS) {
    $dest = "\\Server1\Shared\APPS\$APP"
    #Write-host to see destination path on console
    write-host $dest 
    #copy item from destination path to local directory
    Copy-Item $dest -Destination "c:\apps\"
    
 }

這看起來很簡單。 但我不明白為什么我收到以下錯誤

 \\Server1\Shared\APPS\LTCDesktopSetup.exe
Copy-Item : The filename, directory name, or volume label syntax is incorrect.
At C:\Users\computer1\documents\PowerShell\Moving Installer to local drive.ps1:13 char:2
    +     Copy-Item $dest -Destination "c:\apps\"
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

奧古斯托,

我建議使用這種語法:

$APPS = (Get-ChildItem "\\mybooklive\CMShared\NAS-Downloads" -filter *.exe).FullName

ForEach ($App in $Apps) {
    copy-item "$APP" -Destination "G:\Test\Copy-Test" -Force
}

或更緊湊的:

$APPS = (Get-ChildItem "\\mybooklive\CMShared\NAS-Downloads" -filter *.exe).FullName |
    copy-item -Destination "G:\Test\Copy-Test" -Force

獲取 FullName 與 Name 以便您不必重新添加源路徑。使用 -Filter 以便您只獲取 .exe 文件(這是變量名稱 $Apps 的假設)。 force 會處理一些 IO 問題,比如文件已經存在。

HTH 當然,您必須用您的路徑替換我的測試路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM