我是 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 : 路径中的非法字符。
在 D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy-Item : 路径中的非法字符。
在 D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy-Item : 路径中的非法字符。
在 D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy-Item : 路径中的非法字符。
在 D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy-Item : 路径中的非法字符。
在 D:\Dropbox\DB_AppData\PowerShell_and_Bat\CopyToBackup_2.ps1:32 char:5
Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(*** 错误输出结束:***)
(一堆代码在这里继续运行,一切正常)
结尾
最佳答案
你在错误的位置有一个星号。它需要在源目录路径中,而不是目标路径中,如下所示:
$SourcePathRoot = "C:\Users\Sfrn\Desktop\*"
$DestinationPath = "D:\folder1\subfolder\"
关于Powershell 脚本在 "Copy-Item"命令处失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63865656/