如何使用PowerShell中的-last参数选择要复制的文件?
每天,我们创建生产数据库的副本,然后用户将其从服务器复制到本地机器。我想用一个脚本来自动化这个过程,该脚本将选择特定路径中的最后三个文件并复制它们 我可以用如何使用PowerShell中的-last参数选择要复制的文件?,powershell,file-io,Powershell,File Io,每天,我们创建生产数据库的副本,然后用户将其从服务器复制到本地机器。我想用一个脚本来自动化这个过程,该脚本将选择特定路径中的最后三个文件并复制它们 我可以用 dir $HS3BackupPath | sort LastWriteTime | select -last 3 要正确选择并显示我要复制的三个文件,但我没有成功地将结果通过管道传输到复制命令 看了之后,我试着 dir $HS3BackupPath | sort LastWriteTime | select -last 3 | cop
dir $HS3BackupPath | sort LastWriteTime | select -last 3
要正确选择并显示我要复制的三个文件,但我没有成功地将结果通过管道传输到复制命令
看了之后,我试着
dir $HS3BackupPath | sort LastWriteTime | select -last 3 |
copy-item $HS3BackupPath $Dest
得到了这个错误:
copy-item : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At C:\scripts\RefreshHS3.ps1:96 char:63
+ dir $HS3BackupPath | sort LastWriteTime |select -last 3 | copy-item $HS3Back ...
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (HS3PREcompress-...11-38-17-62.mdb:PSObject) [Copy-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Container cannot be copied onto existing leaf item.
At C:\scripts\RefreshHS3.ps1:98 char:1
+ Copy-Item $HS3BackupPath $Dest | sort LastWriteTime | select -last 3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (\\DC1\HS3_db_Backups\:String) [Copy-Item], PSArgumentException
+ FullyQualifiedErrorId : CopyContainerItemToLeafError,Microsoft.PowerShell.Commands.CopyItemCommand
试着改变,我现在在这里
Copy-Item $HS3BackupPath $Dest | sort LastWriteTime | select -last 3
得到这个错误:
copy-item : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At C:\scripts\RefreshHS3.ps1:96 char:63
+ dir $HS3BackupPath | sort LastWriteTime |select -last 3 | copy-item $HS3Back ...
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (HS3PREcompress-...11-38-17-62.mdb:PSObject) [Copy-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Container cannot be copied onto existing leaf item.
At C:\scripts\RefreshHS3.ps1:98 char:1
+ Copy-Item $HS3BackupPath $Dest | sort LastWriteTime | select -last 3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (\\DC1\HS3_db_Backups\:String) [Copy-Item], PSArgumentException
+ FullyQualifiedErrorId : CopyContainerItemToLeafError,Microsoft.PowerShell.Commands.CopyItemCommand
有人知道正确复制这些文件的语法吗
dir $HS3BackupPath | sort LastWriteTime | select -last 3 | ForEach-Object{ copy-item $_.FullName $Dest}
复制项的输入对象是问题。一种解决方法是使用ForEach对象
,并使用全名
属性作为复制项的路径
正如Ansgar所说,只要使用命名参数-Destination
dir $HS3BackupPath | sort LastWriteTime | select -last 3 | Copy-Item -Destination $Dest
Copy Item
仅为-Path
和-Destination
的2个位置参数留出空间。在您的示例中,这些被指定为没有为管道输入留下任何空间。删除-Path
允许管道输入匹配。为确保没有位置冲突,需要命名目标
复制项的输入对象是问题。一种解决方法是使用ForEach对象
,并使用全名
属性作为复制项的路径
正如Ansgar所说,只要使用命名参数-Destination
dir $HS3BackupPath | sort LastWriteTime | select -last 3 | Copy-Item -Destination $Dest
Copy Item
仅为-Path
和-Destination
的2个位置参数留出空间。在您的示例中,这些被指定为没有为管道输入留下任何空间。删除-Path
允许管道输入匹配。为确保没有位置冲突,需要命名目标
复制项的输入对象是问题。一种解决方法是使用ForEach对象
,并使用全名
属性作为复制项的路径
正如Ansgar所说,只要使用命名参数-Destination
dir $HS3BackupPath | sort LastWriteTime | select -last 3 | Copy-Item -Destination $Dest
Copy Item
仅为-Path
和-Destination
的2个位置参数留出空间。在您的示例中,这些被指定为没有为管道输入留下任何空间。删除-Path
允许管道输入匹配。为确保没有位置冲突,需要命名目标
复制项的输入对象是问题。一种解决方法是使用ForEach对象
,并使用全名
属性作为复制项的路径
正如Ansgar所说,只要使用命名参数-Destination
dir $HS3BackupPath | sort LastWriteTime | select -last 3 | Copy-Item -Destination $Dest
Copy Item
仅为-Path
和-Destination
的2个位置参数留出空间。在您的示例中,这些被指定为没有为管道输入留下任何空间。删除-Path
允许管道输入匹配。为确保没有位置冲突,需要命名目标 不需要使用循环,因为复制项目
可以从管道中读取。您只需在此处使用-Destination
:…|复制项目-目标$Dest
。不需要使用循环,因为复制项目
可以从管道中读取。您只需在此处使用-Destination
:…|复制项目-目标$Dest
。不需要使用循环,因为复制项目
可以从管道中读取。您只需在此处使用-Destination
:…|复制项目-目标$Dest
。不需要使用循环,因为复制项目
可以从管道中读取。您只需在此处使用-Destination
:…|复制项目-目的地$Dest
。