简体   繁体   English

通过网络仅复制批处理和VBS脚本文件的文件和文件夹

[英]Copying files and folders of only batch and vbs script files over network

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. 我正在将服务器从2003迁移到2008,并尝试编写一个Powershell脚本,该脚本会将所有vbs和bat的文件从2003服务器复制到2008服务器,并保持文件夹结构完好,因为对于要运行的程序。

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. 它将在新服务器上创建D:\\folder ,但不会复制任何内容。 It also creates the log.txt with all of the names of the vbs and bat files and there paths. 它还使用所有vbs和bat文件的名称以及路径创建了log.txt

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? 从其他答案中读取注释似乎只是在过滤某些文件以与Copy-Item一起使用时遇到问题,是吗? In most cases you will see suggestions of using Get-ChildItem to isolate the files you want and piping to Copy-Item . 在大多数情况下,您会看到有关使用Get-ChildItem隔离所需文件并将其传递到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.). $files包含您要隔离的文件(如果需要,可以将其通过管道传递到Where-Object以获取特定大小的文件。)。 Take files and output the results to an external txt file (Using Export-CSV can make for cleaner output of object FYI). 获取文件并将结果输出到外部txt文件(使用Export-CSV可以更清晰地输出对象FYI)。 Then we simply pipe it into Copy-Item . 然后我们将其简单地传递到Copy-Item No need to try and file again. 无需尝试再次归档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM