【问题标题】:Powershell script failingi at "Copy-Item" commandPowershell 脚本在“复制项目”命令中失败
【发布时间】:2020-12-31 01:47:33
【问题描述】:

我是 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 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

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

(这里继续运行一堆代码,一切正常)

结束

【问题讨论】:

    标签: powershell copy-item


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢凯瑟琳!完美!
    • 我赞成你的回答,但我显然没有足够的分数来计算。 :-(
    • 不客气,很高兴我能提供帮助。作为问题作者,您应该能够将其标记为已接受的答案。我认为这不会受到声誉的阻碍。
    猜你喜欢
    • 2020-12-21
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2022-07-22
    • 2019-02-16
    相关资源
    最近更新 更多