Solved

Script to find and copy a file to a network share

Posted on 2014-10-28
8
309 Views
Last Modified: 2014-10-29
hi, i need to run a powershell script that will find all *.sh files on a PC and then copy said files to a network unc changing the name to be the pc it came from.

I have come up with this which finds the files

dir -R c:\*.* *.sh -file | where length -eq 1226

Is there a way to do the copying?

thanks
0
Comment
Question by:timb551
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40408457
will you be running this from the PC's themselves or do you want to do this remotely for a list of PC's?
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 400 total points
ID: 40408466
If you are running from the actual computer then this will work

$source = "C:\"
$destination = "\\youruncpath\$env:COMPUTERNAME"
New-Item -ItemType directory -Path $destination
Get-ChildItem -Path $source -recurse | Where-Object { $_.extension -eq ".sh" } | Copy-Item -Force -Destination $destination

Open in new window


For running remotely for multiple computers

$computers = Get-Content C:\Listofcomputers.txt
foreach ($computer in $computers) {
$destination = "\\youruncpath\$computer"
New-Item -ItemType directory -Path $destination
Get-ChildItem -Path \\$computer\C$ -recurse | Where-Object { $_.extension -eq ".sh" } | Copy-Item -Force -Destination $destination
}

Open in new window

0
 

Author Comment

by:timb551
ID: 40408612
ill be running the script using wpkg on the actual PCs themselves.

thanks for the examples, i will test now
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 40

Assisted Solution

by:footech
footech earned 100 total points
ID: 40409159
Instead of
Get-ChildItem -Path $source -recurse | Where-Object { $_.extension -eq ".sh" }
I suggest you substitute
Get-ChildItem -Path $source -Filter *.sh -Recurse
It should be much faster.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40409198
I should've thought of that lol
0
 

Author Comment

by:timb551
ID: 40410327
ok thanks.

what about adding file size is equal to a certain size?
0
 

Author Closing Comment

by:timb551
ID: 40410426
Thanks for all your help.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40410565
-lt Less than
-le Less than or equal to
-gt Greater than
-ge Greater than or equal to
-eq Equal to
-ne Not equal to



For size, you may be able to do something like this, the size or "length" is measured in bytes.

Get-ChildItem -Path $source -Filter *.sh -Recurse | Where {$_.Length -gt 1024}

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This article explains how to install and use the NTBackup utility that comes with Windows Server.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question