Powershell:Get-ChildItem-传递给Rename-Item和Copy-Item的管道

莉莉丝女神

我正在尝试编写一个脚本,该脚本收集服务器名称列表,然后遍历每个服务器名称,并将第二个服务器名称抓取到目录中的最新文件夹,将其重命名,然后将其复制到另一台服务器。我很难让它在一个命令中全部工作,并且我不确定将其拆分并使其正常工作的最佳方法是什么。尝试其他操作时出现错误,主要是“重命名项目:无法评估参数” NewName”,因为其参数被指定为脚本块且没有输入”。这是导致此错误的代码段:

$serverNames = Get-Content -Path "C:\servers.txt"

foreach ($serverName in $ServerNames) {

$reportServer = "a56741035"  

Get-ChildItem "\\$($serverName)\d$\mposlogs\device" | where {  $_.PSIsContainer } | Sort CreationTime -Descending | Select -Skip 1 | Select -First 1 | Rename-Item -NewName { "$serverName" + "_" + $_.Name } | Copy-Item $_.FullName -Destination "\\$($reportServer)\c$\temp\mpos\logs" }

另一个是“无法将参数绑定到参数'Path',因为它为空”。这是导致此错误的代码:

$serverNames = Get-Content -Path "C:\servers.txt"

foreach ($serverName in $ServerNames) {

$reportServer = "a56741035"  

Get-ChildItem "\\$($serverName)\d$\mposlogs\device" | where {  $_.PSIsContainer } | Sort CreationTime -Descending | Select -Skip 1 | Select -First 1 | ForEach-Object { Rename-Item -NewName { "$serverName" + "_" + $_.Name } | Copy-Item $_.FullName -Destination "\\$($reportServer)\c$\temp\mpos\logs" } }

我觉得我很亲密,只是看不到东西。非常感谢您的协助,非常感谢。祝你有美好的一天。

编辑以包括最新代码:

 Get-ChildItem "\\$($serverName)\d$\mposlogs\device" | where {  $_.PSIsContainer } | Sort CreationTime -Descending | Select -Skip 1 | Select -First 1 | ForEach-Object { Rename-Item -Path $_.FullName -NewName ( "$serverName" + "_" + $_.Name ) ; Copy-Item $_.FullName -Destination "\\$($reportServer)\c$\temp\mpos\logs" } 

最新评论代码:

Get-Child-Item "\\$serverName\d$\mposlogs\device" | where {$_.PSIsContainer} | Sort CreationTime -Descending | Select -Skip 1 | Select -First 1 | ForEach-Object { Rename-Item -Path $_.FullName -NewName ( "$serverName" + "" + $.Name ) ; Copy-Item ( "$serverName" + "" + $.Name ) -Destination "\\$reportServer\c$\temp\mpos\logs" } 
迈克·加鲁乔(Mike Garuccio)

因此,事实证明这比我想象的要困难得多,诀窍在于Rename-Item似乎基本上消耗了流水线对象,而没有给您机会在后续名称中使用该对象(使用脚本块也并非如此)理想,使用简单的conatted字符串会容易得多),因此您需要将Rename-Item循环部分稍微更改

Rename-Item -Path $_.FullName -NewName ("$serverName" + "_" + $_.Name)

至于语法变化的建议,建议您可以制作它们但不需要,并且imo会增加歧义性(Get-Childitem不需要转义字符来访问管理共享,即使没有使用$($)语法也没有错访问媒体资源)。

编辑

在评论中稍作修改后,这里是完整GCI管道的更新版本。我在我的环境中成功测试了它,所以用手指交叉它可以为您工作。请让我知道其中的任何部分是否有意义。

Get-ChildItem "\\$serverName\d$\mposlogs\device" | where {$_.PSIsContainer} | Sort CreationTime -Descending | Select -Skip 1 | Select -First 1 | ForEach-Object { Rename-Item -Path $_.FullName -NewName ( "$serverName" + "_" + $_.Name ) -PassThru | Copy-Item  -Destination "\\$reportServer\c$\temp\mpos\logs" }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Powershell的Get-Item / Get-ChildItem

PowerShell:Get-Item与Get-ChildItem输出类型

Powershell Get-ChildItem排除项和管道

使用Get-ChildItem存储到数组时,Powershell Item-Property不会扩展

Powershell Get-Item在某些目录(例如AppData)上不起作用,而Get-ChildItem确实在

Powershell 比较 Get-ChildItem 和 Get-Content 对象

如何将get-childitem的结果传递给命令?

Power Shell Get-ChildItem然后通过管道传递到Get-Content

Powershell:为什么Rename-Item不能用作管道命令?

Powershell 5.0和5.1中的Get-ChildItem

Get-ChildItem和不间断空格

“ [”和“]”字符弄乱了get-childitem

Get-ChildItem,通配符和过滤

用管道输送Get-ChildItem的所有输出

带有变量和字符串的PowerShell Get-ChildItem

Powershell Get -ChildItem:过滤CSV文件和-Recurse无法正常工作

Powershell Get-ChildItem 扩展(非 * 或 ?)通配符

ISE 中的 Powershell Get-Childitem

给定多个-Filters的powershell Get-ChildItem

PowerShell Get-ChildItem如何捕获异常

PowerShell 连接 Get-ChildItem 的输出

get-childitem 的 Powershell 数组元素结果

PowerShell (Get-ChildItem).长度错误

Get-ChildItem 内的 Get-ChildItem

传递包含Get-ChildItem路径的变量不会产生结果

通过字符串变量传递Powershell Get-ChildItem过滤器参数不起作用

get-childitem中-include和-filter之间的区别

DIR / S和Get-ChildItem -Recurse之间的区别

AWS CLI dynamoDB 如何将变量传递给 get-item --key 映射