Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

Powershell script to search for file types from a list of machines, then copy results into directories named after the machine

Here's what I have working so far. It's searching a list of computers placing all the files from all the machines in one folder.

I'm trying to get the files located to be placed in a folder named after the machine that they came from. Any ideas anyone?

Get-Content C:computers.txt | Foreach-Object {
    $ComputerName = $_    
    Get-Childitem "\$ComputerNamec$Documents and Settings**desktop","\$ComputerNamec$Documents and Settings**My Documents" -Include *.xls*, *.doc*, *.txt, *.pdf, *.jpg -Recurse -Force
} | Copy-Item -Destination \destinationshare
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Move the Copy-Item inside the loop and add a statement to create the destination folder:

$extensions = '*.xls*', '*.doc*', '*.txt', '*.pdf', '*.jpg', '*.pub'

Get-Content C:computers.txt | % {
  $ComputerName = $_

  $dst = "\destinationshare$ComputerName"
  $src = "\$ComputerNamec$Documents and Settings**desktop",
         "\$ComputerNamec$Documents and Settings**My Documents"

  New-Item -ItemType Directory $dst

  Get-Childitem $src -Include $extensions -Recurse -Force |
    Copy-Item -Destination $dst
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...