相关疑难解决方法(0)

如何在PowerShell复制脚本中正确过滤多个字符串

我正在使用此答案中的PowerShell脚本来执行文件复制.当我想使用过滤器包含多个文件类型时出现问题.

Get-ChildItem $originalPath -filter "*.htm"  | `
   foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` 
 New-Item -ItemType File -Path $targetFile -Force;  `
 Copy-Item $_.FullName -destination $targetFile }
Run Code Online (Sandbox Code Playgroud)

像梦一样工作.但是,当我想使用过滤器包含多个文件类型时,会出现问题.

Get-ChildItem $originalPath ` 
  -filter "*.gif","*.jpg","*.xls*","*.doc*","*.pdf*","*.wav*",".ppt*")  | `
   foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` 
 New-Item -ItemType File -Path $targetFile -Force;  `
 Copy-Item $_.FullName -destination $targetFile }
Run Code Online (Sandbox Code Playgroud)

给我以下错误:

Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified method is not supported.
At F:\data\foo\CGM.ps1:121 char:36
+ Get-ChildItem $originalPath …
Run Code Online (Sandbox Code Playgroud)

powershell copy filter powershell-2.0

72
推荐指数
2
解决办法
10万
查看次数

Powershell的Copy-Item的-container参数是什么意思?

我正在为MS PowerShell编写脚本.该脚本使用该Copy-Item命令.此命令的可选参数之一是" -container".该参数的文档声明指定此参数"在复制操作期间保留容器对象".

这一切都很好,因为在复制操作期间,我将是最后一个想要未预留容器对象的人.但严肃地说,这个论点有什么作用?特别是在我将磁盘目录树从一个地方复制到另一个地方的情况下,这对Copy-Item命令的行为有何不同?

powershell copy-item

36
推荐指数
2
解决办法
2万
查看次数

如何使用子文件夹复制文件夹?

此脚本在PowerShell中完美运行.它复制具有特定类型的所有文件.但我希望复制文件与文件夹和子文件夹.

$dest  = "C:\example"
$files = Get-ChildItem -Path "C:\example" -Filter "*.msg" -Recurse

foreach ($file in $files) {
    $file_path = Join-Path -Path $dest -ChildPath $file.Name

    $i = 1

    while (Test-Path -Path $file_path) {
        $i++
        $file_path = Join-Path -Path $dest -ChildPath
        "$($file.BaseName)_$($i)$($file.Extension)"
    }

    Copy-Item -Path $file.FullName -Destination $file_path
}
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0 powershell-3.0

17
推荐指数
1
解决办法
7万
查看次数

如何将文件和文件夹树复制到远程计算机?

我有两台服务器A和服务器B,我想使用PowerShell将所有文件和文件夹树从服务器A复制到服务器B.

我已经尝试了下面给出的命令,但它只复制文件夹中的文件而不创建文件夹树:

Copy-Item E:\TestSource\* //TestDestination/ -recurse -force
Run Code Online (Sandbox Code Playgroud)

directory powershell copy file copy-item

5
推荐指数
2
解决办法
1万
查看次数

在 PowerShell 中使用 Get-ChildItem 复制项目

我正在尝试使用 PowerShell 将大量文件从 Windows 系统复制到 Unix 共享。我似乎能够到目前为止,但不是整个解决方案。

只要我不介意它们存储在哪里,我似乎就可以复制这些文件,因为它会丢失所有文件夹结构。我尝试过这里和网上其他地方发布的各种方法,但均无济于事。

代码:

gci -path "e:\company folders\\*" | Where-Object {$_.LastAccessTime -le (Get-Date).addyears(-2)} | Copy-Item -destination "\\\networkfolder\archive\company folders\"
Run Code Online (Sandbox Code Playgroud)

我尝试了此脚本的几种变体,包括在 -path 之后和 -destination 之后使用 -recurse

最大的成功来自于

gci -path "e:\company folders\*" | Where-Object {$_.lastaccesstime -le (Get-Date).addyears(-2)} | Copy-Item -destination "\\\networkshare\archive\company folder\" -recurse -container
Run Code Online (Sandbox Code Playgroud)

但这只复制了 43 个文件夹中的 5 个。

我究竟做错了什么?

powershell

2
推荐指数
1
解决办法
5698
查看次数