Powershell-输出目录递归到控制台和文件
我使用以下代码将共享文件夹结构输出到文本文件:Powershell-输出目录递归到控制台和文件,powershell,Powershell,我使用以下代码将共享文件夹结构输出到文本文件: $path = "\\server\share" $file = "c:\share-folders.txt" get-childitem "$path" -recurse -directory | select -expand fullname >> "$file" 我还想将其写入控制台,同时作为跟踪状态的一种方式。我曾尝试使用tee对象附加文件,然后通过管道写入主机,但充其量只能输出到文本文件或控制台,但不能同时输出到文本文件和控
$path = "\\server\share"
$file = "c:\share-folders.txt"
get-childitem "$path" -recurse -directory | select -expand fullname >> "$file"
我还想将其写入控制台,同时作为跟踪状态的一种方式。我曾尝试使用tee对象附加文件,然后通过管道写入主机,但充其量只能输出到文本文件或控制台,但不能同时输出到文本文件和控制台。有人知道实现这一目标的正确方法吗
解决方案:将-Append添加到Tee-Object cmdlet的末尾解决了该问题
$path = "\\server\share"
$file = "c:\share-folders.txt"
Get-ChildItem "$Path" -Recurse -Directory | Select -Expand FullName | Tee-Object -FilePath "$File" -Append
这对我有用
get-childitem "$path" -recurse -directory | select -expand fullname | %{ Write-Output $_ ; $_ >> "$file"}
接下来,使用Tee命令:
get-childitem "$path" -recurse -directory | select -expand fullname | tee -file "$file"
这对我有用
get-childitem "$path" -recurse -directory | select -expand fullname | %{ Write-Output $_ ; $_ >> "$file"}
接下来,使用Tee命令:
get-childitem "$path" -recurse -directory | select -expand fullname | tee -file "$file"
或者试试这个:
首先选择数据,然后foreach对象将其添加到文本文件并将其写入控制台
$path = "\\server\share"
$file = "c:\share-folders.txt"
Get-ChildItem "$path" -Recurse -Directory | Select-Object -ExpandProperty FullName | ForEach-Object {Add-Content -Value $_ -Path $file; Write-Host -Object $_}
或者试试这个:
首先选择数据,然后foreach对象将其添加到文本文件并将其写入控制台
$path = "\\server\share"
$file = "c:\share-folders.txt"
Get-ChildItem "$path" -Recurse -Directory | Select-Object -ExpandProperty FullName | ForEach-Object {Add-Content -Value $_ -Path $file; Write-Host -Object $_}
请参阅文档中的Tee Object。您是否尝试了中的所有示例?我感谢所有的回复,并为延迟回复表示歉意-项目被搁置了几个星期,以处理另外两个起火的项目。我尝试了Tee对象,但未成功,但我将根据下面的建议和ss64.com文档再次尝试,并很快发布更新。请参阅文档中的Tee对象。您是否尝试了中的所有示例?我感谢所有回复,并为延迟回复道歉-项目被搁置了几个星期,以处理另外两个起火的项目。我尝试了Tee对象,但没有成功,但我将根据下面的建议和ss64.com文档再试一次,并很快发布更新。