I am in the process migrating my servers from 2003 to 2008 and trying to write a powershell script that will copy all files that are vbs and bat from the 2003 server to the 2008 server and also keep the folder structure in tact as it is vital for the program to run.
Here is what i have so far. It works kinda. it will create the D:\\folder
on the new servers but it will not copy anything over. It also creates the log.txt
with all of the names of the vbs and bat files and there paths.
New-PSDrive -name source -PSProvider FileSystem -root "\\servername\d$\folder" |Out-Null
$targetdirectory = "d:\folder"
$sourcedirectory = "\\servername\d$\folder"
Get-ChildItem -Path $sourcedirectory -filter "*.bat","*.vbs" -Recurse| Out-File D:\log.txt
if ( -Not (Test-Path $targetdirectory)) {New-Item -path $targetdirectory -Type Directory | out-null }
Copy-Item -Path $sourcedirectory -filter "*.bat","*.vbs" -Destination $targetdirectory -recurse -force
remove-psdrive -name source
The part that is not working is this
Copy-Item -Path $sourcedirectory -filter "*.bat","*.vbs" -Destination $targetdirectory -recurse -force
I just changed it to
Get-ChildItem -Path $sourcedirectory -Include "*.bat","*.vbs" -Recurse| Out-File D:\log.txt
if ( -Not (Test-Path $targetdirectory)) {New-Item -path $targetdirectory -Type Directory | out-null }
Copy-Item -Path $sourcedirectory -Include *.bat, *.vbs -Destination $targetdirectory -recurse -force
使用robocopy会更容易。
robocopy source dest *.vbs *.bat /s
Reading the comments from the other answer it just looks like you are having an issue filtering certain files for use with Copy-Item
yes? In most cases you will see suggestions of using Get-ChildItem
to isolate the files you want and piping to Copy-Item
. You are almost already doing this.
$files = Get-ChildItem -Path $sourcedirectory -filter "*.bat","*.vbs" -Recurse
$files | Out-File D:\log.txt
$files | Copy-Item -Destination $targetdirectory -Force
$files
contains which files you wanted to isolate (You could pipe it into Where-Object
to get files of a certain size if need be.). Take files and output the results to an external txt file (Using Export-CSV
can make for cleaner output of object FYI). Then we simply pipe it into Copy-Item
. No need to try and file again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.