【问题标题】:Powershell: How to show output onscreen and output to file?Powershell:如何在屏幕上显示输出并输出到文件?
【发布时间】:2020-08-28 10:40:31
【问题描述】:

希望您能提供帮助。 PS新手,请耐心等待:)

我对下面的脚本有疑问。如果我将 Out-File 命令放在第 6 行,结果会打印在 powershell 屏幕中,但 txt 文件是空白的。如果我将 Out-File 命令放在第 3 行的末尾,则会填充文本文件,但它不会在 powershell 窗口中输出结果。 我希望两者都做。 郁闷:(

import-module activedirectory
$username = Read-Host 'Please enter Username!'
Get-ADPrincipalGroupMembership $username | Get-ADGroup -Properties * | select name, description | Format-Table -Wrap -AutoSize
$wsh = New-Object -ComObject Wscript.Shell
$wsh.Popup("List has been saved to C:\Group_List.txt")
Out-File C:\Group_List.txt
Read-Host -Prompt "Press Enter to exit"

【问题讨论】:

  • 您希望它显示在弹出窗口中吗?也许为您的查询分配一个变量,然后以这种方式使用它?或者,如果您希望它显示在终端中,您可以执行 write-host

标签: powershell


【解决方案1】:

首先,您无需在任何地方捕获来自Get-ADPrincipalGroupMembership cmdlet 的结果,而是使用Format-Table 将其发送到屏幕。

其次,输出不显示属于这些组成员的用户,因此如果您正在输入另一个用户,该文件不会显示这些组对哪个用户有效..

最后,插入一个测试,检查输入的用户名是否真的是现有用户。

我会输出一个 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.."
}

【讨论】:

  • 非常优雅。我非常喜欢这个。谢谢西奥!
【解决方案2】:

您可以使用Tee-Object(受经典的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

【讨论】:

  • 我欠你一杯啤酒 Mathias!效果很好,谢谢!
  • @Phill 很高兴听到!虽然我的回答提供了解决问题的方法,但根据目的,您可能需要查看建议并采用Theo's answer 中的方法
【解决方案3】:
  1. 尝试在“Get-ADPrincipalGroupMembership”之后添加 outfile 命令。 应该看起来像这样。

    获取 ADPrincipalGroupMembership $username |获取 ADGroup -属性 * |选择名称、描述 |格式表 -Wrap -AutoSize|输出文件 -FilePath $path
  2. 使用导出-csv

    `$结果 | export-csv -Path $csvFileName -NoTypeInformation`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 2011-09-02
    • 2013-10-16
    • 2011-04-18
    • 2019-12-16
    • 1970-01-01
    • 2020-04-19
    相关资源
    最近更新 更多