Script di Powershell non riuscito al comando "Copy-Item"

Sep 13 2020

Sono un principiante di Powershell. Ho scritto uno script .ps1 per fare una serie di cose, che vengono tutte eseguite perfettamente, tranne quando si tratta di un comando "Copy-Item", che sto usando per copiare i contenuti del desktop (Windows 10) un'altra cartella.

Non riesce al trattino in "Copia elemento". Ho la sensazione che mi manchi un "*" in uno dei percorsi. o averne uno dove non dovrebbe essere. L'ho provato con varie combinazioni, ma nessuna gioia.

A scopo di test, ho 3 elementi sul desktop: 1) un collegamento alla cartella, 2) un collegamento allo script Powershell e 3) una "cartella" GodMode.

Grazie in anticipo per il vostro aiuto. Di seguito è riportato un sottoinsieme del codice:

$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

(*** Ecco dove fallisce: ***)

Copy-Item -Recurse -Path $SourcePathRoot -Destination $DestinationPath -Force #   this is line 32

(*** Ecco l'output dell'errore: il carattere 5 è il trattino in "Copy-Item": ***)

Copia-oggetto: caratteri illegali nel percorso. In D: \ Dropbox \ DB_AppData \ PowerShell_and_Bat \ CopyToBackup_2.ps1: 32 caratteri: 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

Copia-oggetto: caratteri illegali nel percorso. In D: \ Dropbox \ DB_AppData \ PowerShell_and_Bat \ CopyToBackup_2.ps1: 32 caratteri: 5

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

Copia-oggetto: caratteri illegali nel percorso. In D: \ Dropbox \ DB_AppData \ PowerShell_and_Bat \ CopyToBackup_2.ps1: 32 caratteri: 5

  • Copy-Item -Recurse -Path $SourcePathRoot -Destination $Destinatio ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo: InvalidArgument: (Dropbox.lnk: FileInfo) [Copy-Item], ArgumentException
    • FullyQualifiedErrorId: CopyDirectoryInfoItemArgumentError, Microsoft.PowerShell.Commands.CopyItemCommand

Copia-oggetto: caratteri illegali nel percorso. In D: \ Dropbox \ DB_AppData \ PowerShell_and_Bat \ CopyToBackup_2.ps1: 32 caratteri: 5

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

Copia-oggetto: caratteri illegali nel percorso. In D: \ Dropbox \ DB_AppData \ PowerShell_and_Bat \ CopyToBackup_2.ps1: 32 caratteri: 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

(*** Fine dell'output dell'errore: ***)

(un mucchio di codice continua a funzionare qui, il che va bene)

FINE

Risposte

2 catherinemohan Sep 13 2020 at 06:16

Hai un asterisco nel punto sbagliato. Deve essere nel percorso della directory di origine, non nella destinazione, in questo modo:

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