Powershell 脚本在 "Copy-Item"命令处失败

标签 powershell copy-item

我是 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 ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  • 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


  • (*** 错误输出结束:***)
    (一堆代码在这里继续运行,一切正常)
    结尾

    最佳答案

    你在错误的位置有一个星号。它需要在源目录路径中,而不是目标路径中,如下所示:

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

    关于Powershell 脚本在 "Copy-Item"命令处失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63865656/

    相关文章:

    powershell - Jenkins中Powershell脚本中调用参数

    powershell - Powershell意外输出$ _。在字符串中

    PowerShell:复制项目找不到路径

    powershell - 使用 PowerShell 在更改时复制文件

    Powershell 复制项目目标不明确

    windows - Copy-Item 使用凭据将文件从本地复制到远程服务器

    windows - 如何获取EFI分区的存储信息?

    regex - PowerShell使用正则表达式来分割字符串

    powershell - WinSCP .NET 程序集 SynchronizeDirectories : Upload of file . .. 成功,但设置权限和/或时间戳时出错

    powershell - [Powershell] : 2nd Execution of Copy-Item creates subfolder (5. 1.17763.1007)