Copying files and folders from Server to Server with verification issue

Welcome Forums General PowerShell Q&A Copying files and folders from Server to Server with verification issue

This topic contains 2 replies, has 3 voices, and was last updated by

3 years, 8 months ago.

  • Author
  • #34006

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member

    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:

    $countsource = @(Get-Item-Path $source "\\Servername\C$\davesdata")
    $countdest = @(Get-Item-Path $dest "\\Servername\C$")
    IF($countsource = $countdest){
    "$source equals $dest"
    "$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

  • #34032

    Topics: 1
    Replies: 302
    Points: 144
    Helping Hand
    Rank: Participant

    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.

  • #34043

    Topics: 4
    Replies: 269
    Points: 115
    Helping Hand
    Rank: Participant

    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}

The topic ‘Copying files and folders from Server to Server with verification issue’ is closed to new replies.