在 PowerShell 中在屏幕和文件中显示输出 [英] Show output on screen and in file in PowerShell
本文介绍了在 PowerShell 中在屏幕和文件中显示输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能让所有这些不仅输出到屏幕上,而且保存到 CSV 格式?
How can I get all this to not only output on the screen, but to save to a text file in CSV format?
$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
-SearchBase "OU=GA,OU=EAST,DC=corp,DC=chm,DC=com" | Select distinguishedName
ForEach ($OU In $OUs)
{
$OU.distinguishedName
Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
-Filter * | Select Name
}
我试过了
$OUs = Get-ADObject -LDAPFilter "(objectCategory=organizationalUnit)" `
-SearchBase "OU=GA,OU=EAST,DC=corp,DC=chartercom,DC=com" | Select distinguishedName
ForEach ($OU In $OUs)
{
$OU.distinguishedName
Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
-Filter * | Select Name
} | | export-CSV c:\temp\outfile.csv –noType
还有许多其他格式,但我总是收到错误:
And many other formats, but I always get the error:
不允许使用空管道元素.
An empty pipe element is not allowed.
推荐答案
使用 Tee-对象
cmdlet.
Use the Tee-Object
cmdlet.
Tee-Object cmdlet 使您能够在 Windows PowerShell 窗口中显示数据并将相同的数据保存到文本文件中,所有这些都只需一个命令.
The Tee-Object cmdlet enables you to display data in the Windows PowerShell window and to save that same data to a text file, all with a single command.
dir | Tee-Object -file dir.txt
你应该像这样使用它
ForEach ($OU In $OUs)
{
$OU.distinguishedName
Get-ADComputer -SearchBase $OU.distinguishedName -SearchScope OneLevel `
-Filter * | Select Name
} | Tee-Object -file c:\temp\outfile.txt
注意:它有一个别名,tee
,与Unix'tee
.
Note: It has an alias, tee
, which is the same as Unix' tee
.
这篇关于在 PowerShell 中在屏幕和文件中显示输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文