Search list of servers for file (with varying name) and copying it to repository if it exists

by Apr 11, 2014


First post. Also, I'm new to Powershell. I found Piotrek82's script online and want to use it to help me find a particular set of files on all of my servers. If the files exist, I want them copied to a repository on my workstation and also prepended with the name of the server (so i know where it came from).

The script works great but only if the file name is static. However, the files I'm looking for all have their names varied a bit depending on what policy they're tied to (e.g. BPStart_Policy123.bat, BPStart_Policy456.bat, etc). There are dozens of policies.

So, in my oversimplistic brain, I thought I could just use a wildcard for the file name so that anything that starts with "bp" and is a .bat file will be copied to my repository…

Here is Piotrek82's script as modified for my use:

$servers = get-content "C:UsersxxxxDesktopwindows-servers-test.txt"
$filename = "bp*.bat"
foreach ($server in $servers)
$disks = "c$"
    foreach ($disk in $disks)
    $filepath = $Null
    $filepath = get-childitem -path "\$server$diskProgram FilesVeritasNetBackupbin" -Recurse -Force -include $filename -ErrorAction SilentlyContinue
        if ($filepath)
            Copy-Item -literalPath $filepath.FullName -Destination "C:UsersxxxxDesktopserver$server-$filename"


However, I get the error:

Copy-Item : Cannot bind argument to parameter 'LiteralPath' because it is null.
At C:UsersxxxxDocumentsFindFilesOnServers.ps1:14 char:35
+             Copy-Item -literalPath <<<<  $filepath.FullName -Destination "C:UsersxxxxDesktopserver$server-$filename"
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand

I'm sure this is laughably simple to you guys, but any help is greatly appreciated nonetheless. THANK YOU!