Solved

Script to find and copy a file to a network share

Posted on 2014-10-28
8
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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 eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
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:…

734 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