Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

copy item - Powershell Recursively writing to the same directory

I am trying to grab certain logs from a folder and copy them to my device. The code works, but the output is not what I expect (the other three log files are in the Logs folder). I would just like to have my three files appear. The output I get it below. The console complains about the directory already existing, but that is because it is created prior to my loop I think?

$filename = $env:COMPUTERNAME

$files = @('CAS.log', 'LocationServices.log', 'AppDiscovery.log', 'CCMExec.log')

foreach ($file in $files)
{
    Copy-Item "c:WindowsCCMLogs" -Filter $file -Destination 
    "\REMOTECOMPUTERNAMEc$RemoteLogStore$filename" -Recurse
}

What shows up in RemoteLogStore

question from:https://stackoverflow.com/questions/65888144/powershell-recursively-writing-to-the-same-directory

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

1 Answer

0 votes
by (71.8m points)

The $filename in "\REMOTECOMPUTERNAMEc$RemoteLogStore$filename" is the same for every pass of foreach.

Update $filename in the loop to what you actually want as a name, or just use the values from $files


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...