Copying files and folders from Server to Server with verification issue

Good morning all,

I am trying to copy files and folders over from one server to another which i have saved as a powershell script:

Param
(
[parameter(Mandatory=$true,Position=1,ValueFromPipeLine=$true)][string]$source,
[parameter(Mandatory=$true,Position=2,ValueFromPipeLine=$true)][string]$dest
)
$countsource = @(Get-Item-Path $source “\Servername\C$\davesdata”)
$countdest = @(Get-Item-Path $dest “\Servername\C$”)
IF($countsource = $countdest){
“$source equals $dest”
}
Else{
“$source does not match $dest”
}

Then below i run this command in the powershell console (Folder-Compare.ps1 is located on the C drive):

C:\Folder-Compare.ps1 -source “\servername\C$\davesdata” -dest “\servername\C$”
“\servername\C$\davesdata”\temp does not match “\servername\C$”

When i run this i get the following error:

At C:\Folder-Compare.ps1:2 char:3

  • (
  • ~
    Missing ‘)’ in function parameter list

Can anyone help why this is happening and is this script correct

I don’t get that error so perhaps the file you’ve saved is not the save as the script that you have posted.

There’s a fair bit wrong with the script:

$countsource = @(Get-Item-Path $source “\Servername\C$\davesdata”)
$countdest = @(Get-Item-Path $dest “\Servername\C$”)

Your path is determined when you call the script with the source and dest parameters. You have no choice but to specify them as they are both mandatory parameters. There’s no need to put the paths in quote marks after the variable names. Just specifying $source and $dest is enough. You also need a space between the cmdlet name Get-Item and the parameter -path.

IF($countsource = $countdest){

This is a common error. The equals sign is the assignment operator in PowerShell. That means that what you’re attempting to do is assign $countdest to $countsource.

To compare objects for equality you need to use the -eq operator.

Finally, and most importantly, the comparison you’re trying to make will always return false so your script, even if you make the corrections I suggest above, won’t work as intended.

This will copy all folders and files from source to destination and retain folder structure. By default the Copy-Item cmdlet should display an error if a folder exists in destination.
Check $Error[0].Exception.Message for error details.

Get-ChildItem -Path "\\Servername\C$\davesdata" | ForEach-Object {
 Copy-Item -Path $_.FullName -Destination "\\Servername\C$" -Recurse -Container}