如何将 foreach 循环输出重定向到文件?

问题描述 投票:0回答:1

我正在尝试使用以下 powershell 代码将 foreach 循环输出重定向到文件:

PS C:\> foreach ( $dir in "c:\ProgramData", "c:\Program Files", "c:\Program Files (x86)" ) { echo "=> $dir :" ; dir -r -fo $dir 2>$null | % FullName | sls 'exe$' } > C:\temp\VDI_Exclustion_List.txt

但输出显示在标准输出上,我收到此错误:

> : The term '>' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:157
+ ... ir :" ; dir -r -fo $dir 2>$null | % FullName | sls 'exe$' } > C:\temp ...
+                                                                 ~
    + CategoryInfo          : ObjectNotFound: (>:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


PS C:\> ls C:\temp\VDI_Exclustion_List.txt
ls : Cannot find path 'C:\temp\VDI_Exclustion_List.txt' because it does not exist.
At line:1 char:1
+ ls C:\temp\VDI_Exclustion_List.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\temp\VDI_Exclustion_List.txt:String) [Get-ChildItem], ItemNotFoundEx
   ception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

PS C:\>

首先,我尝试用

>
替换
 | Out-File C:\temp\VDI_Exclustion_List.txt
重定向,但这没有用:

Au caractère Ligne:1 : 138
+ ... 6)" ) { dir -r -fo $dir 2>$null | % FullName | sls 'exe$' } | Out-Fil ...
+                                                                 ~
Un élément de canal vide n’est pas autorisé.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement


PS C:\>

然后我尝试将

>
重定向替换为
 6>&1 | Out-File C:\temp\VDI_Exclustion_List.txt
在此处找到),但这也不起作用:

PS C:\> foreach ( $dir in "c:\ProgramData", "c:\Program Files", "c:\Program Files (x86)" ) { echo "=> $dir :" ; dir -r -fo $dir 2>$null | % FullName | sls 'exe$' } 6>&1 | Out-File C:\temp\VDI_Exclustion_List.txt
......
6>&1 : The term '6>&1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:157
+ ... :" ; dir -r -fo $dir 2>$null | % FullName | sls 'exe$' } 6>&1 | Out-F ...
+                                                              ~~~~
    + CategoryInfo          : ObjectNotFound: (6>&1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


PS C:\> 

我希望输出位于文件 C: emp\VDI_Exclustion_List.txt 内,但该文件甚至没有创建。

powershell loops io-redirection
1个回答
0
投票

正如您已经发现的,流控制语句不能用来代替命令或值表达式 - 但您可以将它们包装在脚本块中,然后重定向调用它的输出:

& {
  foreach ($dir in "c:\ProgramData", "c:\Program Files", "c:\Program Files (x86)") { 
    echo "=> $dir :" 
    dir -r -fo $dir 2>$null | % FullName | sls 'exe$'
  }
} > C:\temp\VDI_Exclustion_List.txt

话虽如此,这里实际上并不需要循环!

您可以将输入数据直接通过管道传输到

Get-ChildItem
,而
Get-ChildItem
发现的文件又可以通过管道传输到
Where-Object
以过滤其扩展名:

"c:\ProgramData", "c:\Program Files", "c:\Program Files (x86)" |Get-ChildItem -Recurse -File -Force |Where-Object Extension -eq '.exe' |ForEach-Object FullName |Set-Content C:\temp\VDI_Exclustion_List.txt
© www.soinside.com 2019 - 2024. All rights reserved.