Solved

Script to find and copy a file to a network share

Posted on 2014-10-28
8
298 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
 
LVL 39

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Are your corporate email signatures appalling?

Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now