【问题标题】:Powershell - Variables Array's - Finding Folders then Files in FolderPowershell - 变量数组 - 查找文件夹然后文件夹中的文件
【发布时间】:2015-04-10 19:26:10
【问题描述】:

我真的需要一些关于 Powershell 的帮助,完全是 Powershell 的新手

我有下面的命令,它输出一个路径列表,用于搜索在共享上多次创建的名为“xyz”的文件夹,用作变量

$FOLDERLISTS = (Get-ChildItem \\server\share -Recurse | Where-Object { ( $._PSIsContainer -eq $true) -and ($_.Name -like "xyz" -and ( $_.mode -match "d") | % { Write-Host $_.FullName })

如何使用多个文件夹路径,我可以将其设置为变量吗?

基本上我只想获取文件夹路径,然后针对上述命令输出的每个文件夹路径运行另一个 Get-ChildItem,因此如果它是单个变量,则命令看起来像;

Get-ChildItem "@ABOVECOMMAND" -Recurse | Where-Object ( !($_.PSIsContainer) -and $_.lenght -le 1000000 )

我可以以某种方式使用 ForEach 来运行多个路径吗?

foreach ($FOLDERLIST in $FOLDERLISTS)
{
Get-ChildItem -Recurse | Where-Object { !($_.PSIsContainer) -and $_.lenght -le 1000000 }
}

或者

$FOLDERLISTS | ForEach-Object{
Get-ChildItem -Recurse | Where-Object { !($_.PSIsContainer) -and $_.lenght -le 1000000 }

或者只是将路径导出到文本文件并导入到命令中?完全卡住了。

【问题讨论】:

  • 你卡在哪里了?不明白为什么您的最后两个建议中的任何一个都不起作用
  • 最后两个想法实际上都不会像所写的那样工作。两者都没有变量来引用循环的当前迭代。第一个想法是 $FOLDERLIST。在第二个想法中,它将是 $_。

标签: arrays variables powershell


【解决方案1】:

你的第一次尝试应该更像这样:

foreach ($FOLDERLIST in $FOLDERLISTS)
{
Get-ChildItem $FOLDERLIST -Recurse | Where-Object { !($_.PSIsContainer) -and $_.lenght -le 1000000 }
}

或者你的第二次尝试是这样的:

$FOLDERLISTS | ForEach-Object{
Get-ChildItem $_ -Recurse | Where-Object { !($_.PSIsContainer) -and $_.lenght -le 1000000 }

【讨论】:

  • 谢谢坎贝尔我想从你的评论中我可以看出我哪里出错了,get-childitem 没有通过任何东西!我会试试这个看起来像赢家!
猜你喜欢
  • 1970-01-01
  • 2017-12-22
  • 2018-05-04
  • 1970-01-01
  • 2017-05-18
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多