Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all
Below is the code i am trying to execute for coping multiple files from one folder to other on remote server but i am getting this error

PS C:\Windows> D:\ScriptRepo\Smart_Backup_Create folders_by_Date.ps1
Hit Line breakpoint on 'D:\ScriptRepo\Smart_Backup_Create folders_by_Date.ps1:5'
[DBG]: PS C:\Windows>> 
\\$ComputerName\$DriveLetter$\BMCSoftware\BMCPortalKit\webserver\tools\apache-openssl\logs : The term 
'\\$ComputerName\$DriveLetter$\BMCSoftware\BMCPortalKit\webserver\tools\apache-openssl\logs' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\ScriptRepo\Smart_Backup_Create folders_by_Date.ps1:5 char:12
+ $SrcPath = \\$ComputerName\$DriveLetter$\BMCSoftware\BMCPortalKit\webserver\tool ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\$ComputerName...he-openssl\logs:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
[DBG]: PS C:\Windows>> 
\\$ComputerName\$DriveLetter$\Log_Backup_$((Get-Date).ToString('yyyy-MM-dd')) : The term '\\$ComputerName\$DriveLetter$\Log_Backup_$((Get-Date).ToString('yyyy-MM-dd'))' is not recognized as 
the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\ScriptRepo\Smart_Backup_Create folders_by_Date.ps1:6 char:13
+ $DestPath = \\$ComputerName\$DriveLetter$\Log_Backup_$((Get-Date).ToString('yyyy ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\$ComputerName...('yyyy-MM-dd')):String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
[DBG]: PS C:\Windows>> 


    Directory: \\USDC1-BEMAPDC01\D$


Mode                LastWriteTime     Length Name                                                                                                                                             
----                -------------     ------ ----                                                                                                                                             
d----         8/14/2017   5:35 AM            Log_Backup_2017-08-14                                                                                                                            
[DBG]: PS C:\Windows>> 
Copy-Item : Cannot find path 'D:\BMCSoftware\BMCPortalKit\webserver\tools\apache-openssl\logs' because it does not exist.
At D:\ScriptRepo\Smart_Backup_Create folders_by_Date.ps1:8 char:1
+ Copy-Item -Recurse -Filter *.* -path $SrcPath -destination $DestPath -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (D:\BMCSoftware\...he-openssl\logs:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand


Please let me know where i am doing wrong. Both the source and destination folders are in same server

What I have tried:

$ComputerName = "ServerName"
$DriveLetter = "D"
$CreateFolderPath = "Log_Backup_$((Get-Date).ToString('yyyy-MM-dd'))"

$SrcPath = \\$ComputerName\$DriveLetter$\BMCSoftware\BMCPortalKit\webserver\tools\apache-openssl\logs
$DestPath = \\$ComputerName\$DriveLetter$\Log_Backup_$((Get-Date).ToString('yyyy-MM-dd'))
New-Item -Path \\$ComputerName\$DriveLetter$\$Path -type directory -Force // Creating folder
Copy-Item -Recurse -Filter *.* -path $SrcPath -destination $DestPath -Force
Posted
Comments
Jochen Arndt 14-Aug-17 6:09am    
Enclose the variable content with double quotes:
$SrcPath = "\\$ComputerName\$DriveLetter ..."

Do that too when passing as arguments (they may contain spaces which would get the string treated as multiple arguments)
Copy-Item -path "$SrcPath"


You might get still problems with
"Log_Backup_$((Get-Date).ToString('yyyy-MM-dd'))"
Solving that requires knowing what that should do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900