Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

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

$
0
0

Greeetings!

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:\Users\xxxx\Desktop\windows-servers-test.txt"
$filename = "bp*.bat"
foreach ($server in $servers)
{
$disks = "c$"
    foreach ($disk in $disks)
    {
    $filepath = $Null
    $filepath = get-childitem -path "\\$server\$disk\Program Files\Veritas\NetBackup\bin" -Recurse -Force -include $filename -ErrorAction SilentlyContinue
       
        if ($filepath)
            {
            $filepath.fullname
            Copy-Item -literalPath $filepath.FullName -Destination "C:\Users\xxxx\Desktop\server\$server-$filename"
            }
       
    }

}

However, I get the error:

Copy-Item : Cannot bind argument to parameter 'LiteralPath' because it is null.
At C:\Users\xxxx\Documents\FindFilesOnServers.ps1:14 char:35
+             Copy-Item -literalPath <<<<  $filepath.FullName -Destination "C:\Users\xxxx\Desktop\server\$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!
 


Viewing all articles
Browse latest Browse all 6937

Trending Articles