Results 1 to 8 of 8
  1. #1
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts

    PowerShell: Multi-file to multi-machine copy

    Hey Y'all,

    I've written this PS program:
    Code:
    Param (
      [parameter(Mandatory=$False)]
      [String[]]$SrcFiles
    )
    
    
    if ($SrcFiles -eq $Null) {
      Write-Host "No Files to Copy!"
      Exit
    }
    
    $comps = @("\\DellXPS14z\LT-BEKDocs",
               "\\BRUCESLT1564\BEKDocs",
               "\\DELL137000JLT\JEMDocs",
               "\\MYBOOKLIVE\CMShared\BEKDocs")
    
    foreach ($putr in $comps) { 
    
        if (Test-Path -path "$putr" ) {
          "$putr`: `tAccessable"
          ForEach ($Src in $SrcFiles) {
            Write-Host "Copying: $src"
            $DestDir = $src.Trim("G:\BEKDocs\")
    
            Try {
             Copy-Item -Path "$Src" -Destination "$putr\$DestDir" -Force
            }
            Catch {
             Write-Host "Could not write: $src to `n$putr\$DestDir"
            }
    
          }    #End ForEach ($Src...
        }      #End If (Test-Path...
        ELse {
          "$putr`: `tNo Access"
        }
    
    } #End ForEach ($putr...
    This works just fine from the PS Console called like this:
    Code:
     .\Copy-MultiMachines.ps1 -SrcFiles @("G:\BEKDocs\Scripts\CMsPCInfo-FormVersion.ps1",
                                          "G:\BEKDocs\Scripts\Dynamic-Uninst-Win10BuiltinApps.ps1")
    Where I have a problem is setting it up as a shortcut on the File Explorer context menu. Here's the shortcut that is placed in:

    C:\Users\[USERID]\AppData\Roaming\Microsoft\Windows\SendTo

    Shortcut:
    Code:
    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
        -File G:\BEKDocs\Scripts\Copy-MultiMachines.ps1 -SrcFiles
    The shortcut works fine if I select a single file. The problem is when I select multiple files the program errors and blinks so fast I can't read the message. I've tried message boxes to stop the program so I can see the errors but it dies before they can execute.

    I'm pretty sure the problem is how Explorer passes the multiple files. My program is expecting an Array. I've done a lot of googling but can't find info on how these files are passed.

    Ideas?

    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Hey Y'all,

    OK, first problem solved. I discovered that if I didn't define a Parameter in the PS code I could use the $Args variable to count the number of filespecs passed from File Explorer and iterate through them using a FOR loop.

    PowerShell Code:
    Code:
    <#
      +-------------------------------------------------------------+
      | Computer Mentors Copy Files to Multiple Machines            |
      |                                                             |
      | Programmed by: RetiredGeek (WSL) aks Computer Mentor        |
      | Created: 11/25/15   Version 1.0                             |
      | Updated:                                                    |
      +-------------------------------------------------------------+
    #>
    <#
      +-------------------------------------------------------------+
      |                                                             |
      |  Windows Shortcut file in SendTo Directory:                 |
      |                                                             |
      |  Shortcut TARGET:                                           |
      |                                                             |
      |  C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe  |
      |        -File G:\BEKDocs\Scripts\Copy-MultiMachines.ps1      |
      |                                                             |
      +-------------------------------------------------------------+
    #>
    
    if ($Args.count -eq 0) {
      Write-Host "No Files to Copy!"
      Exit
    }
    
    #Folder List Array of destinations to copy files to:
    
    $comps = @("\\DellXPS14z\LT-BEKDocs",
               "\\BRUCESLT1564\BEKDocs",
               "\\DELL137000JLT\JEMDocs",
               "\\MYBOOKLIVE\CMShared\BEKDocs")
    
    foreach ($putr in $comps) { 
    
        if (Test-Path -path "$putr" ) {
          "$putr`: `tAccessable"
          for ($i = 0; $i -lt $Args.count; $i++) {
            Write-Host "Copying: $($Args[$i])"
            $DestDir = $Args[$i].Trim("G:\BEKDocs\")
    
            Try {
             Copy-Item -Path "$($Args[$i])" -Destination "$putr\$DestDir" -Force
            }
            Catch {
             Write-Host "Could not write: $src to `n$putr\$DestDir"
            }
    
          }    #End For ($i...
        }      #End If  (Test-Path...
        ELse {
          "$putr`: `tNo Access"
        }
    
    } #End ForEach ($putr...
    
    # Pause display to allow user to view results
    Read-Host "Press any key to continue"
    Calling Program from File Explorer:

    Example-Copy-MultiMachines.JPG

    Sample Output:
    Code:
    \\DellXPS14z\LT-BEKDocs:        Accessable
    Copying: G:\BEKDocs\Scripts\Copy-MultiMachines.ps1
    Copying: G:\BEKDocs\Scripts\Copy-MultiMachinesV1.ps1
    Copying: G:\BEKDocs\Scripts\ReceiveInputFromFileExplorer.ps1
    \\BRUCESLT1564\BEKDocs:         No Access
    \\DELL137000JLT\JEMDocs:        No Access
    \\MYBOOKLIVE\CMShared\BEKDocs:  Accessable
    Copying: G:\BEKDocs\Scripts\Copy-MultiMachines.ps1
    Copying: G:\BEKDocs\Scripts\Copy-MultiMachinesV1.ps1
    Copying: G:\BEKDocs\Scripts\ReceiveInputFromFileExplorer.ps1
    Press any key to continue:
    Now the only remaining problem to solve is why, even though I can access it via File Explorer, the program will NOT copy to my Wife's machine {DELL137000JLT\JEMDocs}. The only difference between this laptop and the other two laptops {DellXPS14z\LT-BEKDocs & BRUCESLT1564\BEKDocs} is that each of my laptops has a Bruce Account witht the same PW as my Desktop (source machine in this case) while the Wife's does not. However, as I said, the credentials for her machine have been entered and I can access the machine and directory via File Explorer.

    Something to think about while gnawing at that Turkey tomorrow!

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,207
    Thanks
    49
    Thanked 989 Times in 919 Posts
    Have you looked in Event Viewer on both machines to see if it's a permission problem?

    cheers, Paul

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Paul,

    Thanks for the Idea. However, I've discovered that it must have been that my wife's machine was asleep as it worked today while she was actively using the machine.

    Code:
    \\DellXPS14z\LT-BEKDocs:        No Access
    \\BRUCESLT1564\BEKDocs:         No Access
    \\DELL137000JLT\JEMDocs:        Accessable
    Copying: G:\BEKDocs\Scripts\Copy-MultiMachines.ps1
    \\MYBOOKLIVE\CMShared\BEKDocs:  Accessable
    Copying: G:\BEKDocs\Scripts\Copy-MultiMachines.ps1
    Press any key to continue:
    Happy Thanksgiving {I'm assuming your portion of Earth in in the U.S.}
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,207
    Thanks
    49
    Thanked 989 Times in 919 Posts
    Nope!
    Thanks anyway.

    cheers, Paul

  6. #6
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Hey Y'all,

    Here's version 2 that now has a selection of the UNCs available so you don't have to change the code if you don't want a particular machine to get the files. Just include all machines in the $comps array.
    UNCSelection.JPG

    Version 2 Code: Copy-MultiMachines.zip {Updated}

    HTH
    Last edited by RetiredGeek; 2015-11-28 at 21:38.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  7. #7
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,207
    Thanks
    49
    Thanked 989 Times in 919 Posts
    Nice selection screen!

    cheers, Paul

  8. #8
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Hey Y'all,

    On to version 3.1 which adds a Results Screen tab and cleans up the code a bit.
    MultiCopyResults.JPG

    Version 3.1 Code: Copy-MultiMachinesV3-1.zip

    Enjoy,

    Last edited by RetiredGeek; 2015-12-12 at 20:33.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •