Solved

Script to find and copy a file to a network share

Posted on 2014-10-28
8
304 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
Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

 
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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

920 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

13 Experts available now in Live!

Get 1:1 Help Now