?
Solved

Script to find and copy a file to a network share

Posted on 2014-10-28
8
Medium Priority
?
321 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 1600 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 40

Assisted Solution

by:footech
footech earned 400 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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:…
Suggested Courses

770 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