[英]Powershell script failingi at “Copy-Item” command
我是一個 Powershell 新手。 我寫了一個 .ps1 腳本來做很多事情,所有這些都完美地執行,除了“復制項目”命令,我用它來復制(Windows 10)桌面的內容到另一個文件夾。
它在“復制項目”中的連字符處失敗。 我有一種感覺,我要么在其中一條路徑中缺少“*”。 或者在不應該出現的地方放一個。 我嘗試了各種組合,但沒有任何樂趣。
出於測試目的,我在桌面上有 3 個項目:1)文件夾快捷方式,2)Powershell 腳本快捷方式,以及 3)GodMode“文件夾”。
預先感謝您的幫助。 以下是代碼的一個子集:
$env:path += ";D:\PowershellScriptsFolder" # this is the location of my ps1
# (a bunch of code here, all of which runs fine) #
# Desktop
# Define Variables
$DestinationPath = "D:\folder1\subfolder\*"
$SourcePathRoot = 'C:\Users\Sfrn\Desktop\' # this is the location of my Win 10 desktop
#
Remove-Item -Recurse -Path $DestinationPath -Force
(*** 這里是失敗的地方:***)
Copy-Item -Recurse -Path $SourcePathRoot -Destination $DestinationPath -Force # this is line 32
(*** 這是錯誤輸出 - 字符 5 是“Copy-Item”中的連字符:***)
Copy-Item : 路徑中的非法字符。 在 D:\\Dropbox\\DB_AppData\\PowerShell_and_Bat\\CopyToBackup_2.ps1:32 字符:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy-Item : 路徑中的非法字符。 在 D:\\Dropbox\\DB_AppData\\PowerShell_and_Bat\\CopyToBackup_2.ps1:32 字符:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy-Item : 路徑中的非法字符。 在 D:\\Dropbox\\DB_AppData\\PowerShell_and_Bat\\CopyToBackup_2.ps1:32 字符:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy-Item : 路徑中的非法字符。 在 D:\\Dropbox\\DB_AppData\\PowerShell_and_Bat\\CopyToBackup_2.ps1:32 字符:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy-Item : 路徑中的非法字符。 在 D:\\Dropbox\\DB_AppData\\PowerShell_and_Bat\\CopyToBackup_2.ps1:32 字符:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(*** 錯誤輸出結束:***)
結尾
您在錯誤的位置有一個星號。 它需要在源目錄路徑中,而不是目標路徑中,如下所示:
$SourcePathRoot = "C:\Users\Sfrn\Desktop\*"
$DestinationPath = "D:\folder1\subfolder\"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.