Powershell脚本在“复制项”命令中失败

问题描述

我是Powershell新手。我编写了一个.ps1脚本来执行许多操作,所有这些操作均能完美执行,除非涉及“ copy-Item”命令,该命令用于将(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 ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo:InvalidArgument:(D:\ DropBox \ DB_AppData \ Desktop *:String)[copy-Item],ArgumentException
    • FullyQualifiedErrorId:CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.copyitemcommand

copy-Item:路径中的非法字符。 在D:\ DropBox \ DB_AppData \ PowerShell_and_Bat \ copyToBackup_2.ps1:32 char:5

  • copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo:InvalidArgument:(desktop.ini:FileInfo)[copy-Item],ArgumentException
    • FullyQualifiedErrorId:copyDirectoryInfoItemArgumentError,Microsoft.PowerShell.Commands.copyitemcommand

copy-Item:路径中的非法字符。 在D:\ DropBox \ DB_AppData \ PowerShell_and_Bat \ copyToBackup_2.ps1:32 char:5

  • copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo:InvalidArgument:(DropBox.lnk:FileInfo)[copy-Item],ArgumentException
    • FullyQualifiedErrorId:copyDirectoryInfoItemArgumentError,Microsoft.PowerShell.Commands.copyitemcommand

copy-Item:路径中的非法字符。 在D:\ DropBox \ DB_AppData \ PowerShell_and_Bat \ copyToBackup_2.ps1:32 char:5

  • copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo:InvalidArgument:(OpenApps.lnk:FileInfo)[copy-Item],ArgumentException
    • FullyQualifiedErrorId:copyDirectoryInfoItemArgumentError,Microsoft.PowerShell.Commands.copyitemcommand

copy-Item:路径中的非法字符。 在D:\ DropBox \ DB_AppData \ PowerShell_and_Bat \ copyToBackup_2.ps1:32 char:5

  • copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo:InvalidArgument:(D:\ DropBox \ DB_AppData \ Desktop *:String)[copy-Item],ArgumentException
    • FullyQualifiedErrorId:CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.copyitemcommand

(***错误输出结束:***)

(一堆代码继续在这里运行,都很好)

END

解决方法

您在错误的位置输入了星号。它必须位于源目录路径中,而不是目的地中,如下所示:

$SourcePathRoot = "C:\Users\Sfrn\Desktop\*"
$DestinationPath = "D:\folder1\subfolder\"

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...