Powershell:如何在屏幕上显示输出并输出到文件?
希望你能帮忙。PS非常新,请耐心等待:) 我对下面的脚本有问题。如果我在第6行输入Out File命令,结果将打印在powershell屏幕上,但txt文件为空。如果我在第3行末尾发出Out File命令,文本文件将被填充,但它不会在powershell窗口中输出结果。 我想两者都做。 令人沮丧:(Powershell:如何在屏幕上显示输出并输出到文件?,powershell,Powershell,希望你能帮忙。PS非常新,请耐心等待:) 我对下面的脚本有问题。如果我在第6行输入Out File命令,结果将打印在powershell屏幕上,但txt文件为空。如果我在第3行末尾发出Out File命令,文本文件将被填充,但它不会在powershell窗口中输出结果。 我想两者都做。 令人沮丧:( 尝试在“Get ADPrincipalGroupMembership”之后添加outfile命令。 应该是这样的 Get-ADPrincipalGroupMembership $username |
您可以使用
Tee对象
(受经典的Tee
unix命令行工具启发)将输入流分叉到变量或磁盘上的文件中!如果要将格式化的表按原样写入文件,只需将其固定到输出到屏幕的管道表达式的末尾即可(您可以跳过select
命令,Format Table
也会获取属性列表)
Get-ADPrincipalGroupMembership$username | Get-ADGroup-Properties description | Format Table-Property name,description-Wrap-AutoSize | Tee Object-FilePath C:\Group_List.txt
如果输出文件供其他计算机/程序稍后使用,我会更改策略并将其导出为CSV文件。要捕获数据并将其同时输出,我们可以将初始查询的结果分配给一个变量,然后在单独的语句中输出两次:
$groupsWithDescription = Get-ADPrincipalGroupMembership $username |Get-ADGroup -Properties description |Select-Object Name,Description
# Output to screen, format as table
$groupData |Format-Table -Wrap -AutoSize
# Output to file, export data as CSV
$groupData |Export-Csv -Path C:\Group_List.txt -NoTypeInformation
您可以使用
Tee对象
(受经典的Tee
unix命令行工具启发)将输入流分叉到变量或磁盘上的文件中!如果要将格式化的表按原样写入文件,只需将其固定到输出到屏幕的管道表达式的末尾即可(您可以跳过select
命令,Format Table
也会获取属性列表)
Get-ADPrincipalGroupMembership$username | Get-ADGroup-Properties description | Format Table-Property name,description-Wrap-AutoSize | Tee Object-FilePath C:\Group_List.txt
如果输出文件供其他计算机/程序稍后使用,我会更改策略并将其导出为CSV文件。要捕获数据并将其同时输出,我们可以将初始查询的结果分配给一个变量,然后在单独的语句中输出两次:
$groupsWithDescription = Get-ADPrincipalGroupMembership $username |Get-ADGroup -Properties description |Select-Object Name,Description
# Output to screen, format as table
$groupData |Format-Table -Wrap -AutoSize
# Output to file, export data as CSV
$groupData |Export-Csv -Path C:\Group_List.txt -NoTypeInformation
首先,您不会在任何地方捕获来自
Get-ADPrincipalGroupMembership
cmdlet的结果,而只是使用格式表将其发送到屏幕
其次,输出不会显示属于这些组的用户,因此如果您键入的是其他用户,则文件不会显示这些组对哪个用户有效
最后,插入一个测试来检查输入的用户名是否确实是现有用户
我会输出一个CSV文件,您只需在Excel中打开即可。如下所示:
Import-Module activedirectory
$outFile = 'C:\Group_List.csv'
$username = Read-Host 'Please enter Username!'
# do some error checking to see if this is an existing user
$user = Get-ADUser -Filter "SamAccountName -eq '$username'" -ErrorAction SilentlyContinue
if ($user) {
$groups = Get-ADPrincipalGroupMembership -Identity $user.DistinguishedName |
Get-ADGroup -Properties Description | ForEach-Object {
# output an object with properties you need
[PsCustomObject]@{
User = $username
Group = $_.Name
Description = $_.Description
}
}
# show on screen
$groups | Format-Table -Wrap -AutoSize
# write to CSV file
$groups | Export-Csv -Path $outFile -UseCulture -NoTypeInformation
$wsh = New-Object -ComObject Wscript.Shell
$wsh.Popup("List has been saved to $outFile")
# clean up COM object after use
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wsh)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
else {
Write-Warning "User $username does not exist.."
}
首先,您不会在任何地方捕获来自Get-ADPrincipalGroupMembership
cmdlet的结果,而只是使用格式表将其发送到屏幕
其次,输出不会显示属于这些组的用户,因此如果您键入的是其他用户,则文件不会显示这些组对哪个用户有效
最后,插入一个测试来检查输入的用户名是否确实是现有用户
我会输出一个CSV文件,您只需在Excel中打开即可。如下所示:
Import-Module activedirectory
$outFile = 'C:\Group_List.csv'
$username = Read-Host 'Please enter Username!'
# do some error checking to see if this is an existing user
$user = Get-ADUser -Filter "SamAccountName -eq '$username'" -ErrorAction SilentlyContinue
if ($user) {
$groups = Get-ADPrincipalGroupMembership -Identity $user.DistinguishedName |
Get-ADGroup -Properties Description | ForEach-Object {
# output an object with properties you need
[PsCustomObject]@{
User = $username
Group = $_.Name
Description = $_.Description
}
}
# show on screen
$groups | Format-Table -Wrap -AutoSize
# write to CSV file
$groups | Export-Csv -Path $outFile -UseCulture -NoTypeInformation
$wsh = New-Object -ComObject Wscript.Shell
$wsh.Popup("List has been saved to $outFile")
# clean up COM object after use
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wsh)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
else {
Write-Warning "User $username does not exist.."
}
您希望它显示在弹出窗口中吗?可能为您的查询分配一个变量,然后以这种方式处理它?或者如果您希望它显示在终端中,您可以执行以下操作:您希望它显示在弹出窗口中吗?可能为您的查询分配一个变量,然后以这种方式处理它?或者如果您希望它显示在终端中,您可以执行以下操作:rite hostI欠你一杯啤酒Mathias!效果很好,谢谢!@Phill好听!虽然我的回答提供了所述问题的解决方案,但根据目的,您可能希望查看建议并采用“我欠你一杯啤酒Mathias!”中的方法!效果很好,谢谢!@Phill好听!而我的回答提供了解决问题的方法如前所述的问题,根据目的,你可能希望审查建议并采用非常优雅的方法。我非常喜欢这个。谢谢西奥!非常优雅。我非常喜欢这个。谢谢西奥!