【问题标题】:Output file doesn't match Write-Host输出文件与 Write-Host 不匹配
【发布时间】:2020-02-03 00:11:59
【问题描述】:

当我从我的 .ps1 文件中Write-Host 时,我看到:

Parentfolder >> ChildFolder

当我输出到文件时,我看到:

ParentFolder
>>
ChildFolder

我用的是简单的write-host ($childgroup.name), ">>", ($object.samaccountname)

当我尝试使用ReturnOut-FileExport to CSV 等输出相同的信息时...我得到 3Write-Host 打印为 单行.

我只希望输出文件的格式与Write-Host 输出的格式相同。

根据要求:

function getchildgroups($groupname) {

    # Get initial group details and members
    $childgroup = get-adgroup $groupname -properties member

    # Only continue if this group has members
    if (($childgroup.member).count -gt 0) {

        # Loop through each member of the group
        foreach ($memberobject in $childgroup.member) {

            try {
                $object = get-adobject $memberobject -properties *;

                # If the member of the group is another group
                if ($object.objectclass -eq "group")  {

                    # Print it to the screen

                    write-host ($childgroup.name),">>", ($object.samaccountname) 
                   #$cgname = $childgroup.name
                    #$objname =$object.samaccountname
                    #Return (($cgname, ">>", $objname)) >> 
c:\Temp\NestedGroups.txt

                    # Recursive lookup the members of the sub-group (if 
not self-nested)
                    if ($memberobject -ne $object.distinguishedname) {

                        getchildgroups($object.distinguishedname);
                    }
                }
            } catch {}
        }
    }
}

# Run the function with your group name
$Groups = Get-Content C:\temp\ListOfFolders.txt
Foreach ($Group in $Groups){
getchildgroups("$group") 
}

【问题讨论】:

  • 那是无法理解的。请edit您的问题并显示您的脚本内容以及您是如何运行该脚本的。
  • 无法理解???简单的问题。当我使用 Write-Host 时,我从脚本中得到一个带有 result1 >> result2 的单行。当我将它输出到文件时,我在一行上得到 result1,在下一行得到 >>,在第三行得到 result2。我的问题是如何让输出文件的输出与 Write-Host 输出相匹配。
  • 您尝试过什么,您尝试过什么失败了?理想情况下,您应该提供一个minimal reproducible example 您尝试过的内容,并包含有关它如何失败的具体信息,以及错误消息和/或错误输出。 Stack Overflow 不是代码编写服务;最好的问题是那些提供有用信息的问题,以便回答的人可以指导您设计自己的正确答案。请参阅How to Ask 一个好问题。
  • 无法仅仅因为管道的位置而产生不同的输出。您确定问题不在于您如何查看文件吗?例如,您可能在打开自动换行的情况下使用记事本进行查看,或者您的文本编辑器与控制台可能会以不同的方式解释不可见的字符。
  • 我尝试过:out-File、Return、Export-Csv、Transcript、Write-Output 和其他一些无济于事。

标签: powershell output-formatting write-host


【解决方案1】:

警告

  • Write-Host 用于显示输出,而不是用于输出数据 - 它绕过 PowerShell成功输出流(PowerShell 的标准输出等效项),因此来自 Write-Host 的输出不能(直接[1])在变量中捕获,也不能重定向到文件 - 请参阅 this answer 的下半部分更多信息。

  • 使用 Write-Output 或 - 最好 - PowerShell 的 implicit 输出 行为来输出数据,适合进一步程序化处理

除了这个根本区别之外,Write-HostWrite-Output 在处理参数的方式上也有所不同

# What Write-Host prints to the display is a *single string* that is 
# the space-separated list of the (stringification of) its arguments.
PS> Write-Host file1, '>>', file2
file1 >> file2  # printed to *display* only

# Write-Output outputs each argument - whatever its data type - *separately*
# to the success output stream.
# In the case of *string* arguments, each string renders *on its own line*.
PS> Write-Output file1, '>>', file2
file1
>>
file2

使用隐式输出,相当于上面的Write-Output命令是:

# Send an array of 3 strings to the success stream.
PS> 'file1', '>>', 'file2'
file1
>>
file2

如果您重定向Write-Output 命令或其隐式等效于文件(使用> / Out-FileSet-Content[2]),您将获得相同的 3 行表示。

此外,Write-Host 对复杂对象执行简单的.ToString() 字符串化,这通常会导致无用的输出;相比之下,Write-Output / 隐式输出使用 PowerShell 丰富的格式化系统:

# Write-Host: Unhelpful representation; entries are enumerated
#             and .ToString() is called on each.
PS> Write-Host @{ foo = 1; bar = 2 }
System.Collections.DictionaryEntry System.Collections.DictionaryEntry

# Write-Output / implicit output: rich formatting
PS> @{ foo = 1 }

Name                           Value
----                           -----
foo                            1
bar                            2

注意:如果您使用 Out-Host cmdlet 并将命令通过管道传递给它(例如
@{ foo = 1; bar = 2 } | Out-Host),您会得到通常的、丰富的输出格式通过Write-Output / 默认情况下 - 同时仍仅打印到显示器


如果您确实想将单行输出为数据,请使用带引号的字符串;要引用变量并在字符串中嵌入子表达式,请使用可扩展字符串(字符串插值)"..."(参见about_Quoting_Rules) ,或使用字符串连接 (+)

$arg1 = 'file1'; $arg2 = 'file2'

# Expandable string
PS> "$arg1 >> $arg2"
file1 >> file2

# String concatenation
PS> $arg1 + ' >> ' + $arg2
file1 >> file2

[1] 在 PowerShell v5 或更高版本中,您可以通过信息输出流捕获/重定向Write-Host 输出,编号为6;例如:$writeHostOutput = & { Write-Host hi } 6>&1。但是请注意,信息输出流主要设计用于 PSv5+ Write-Information cmdlet 和常见的-InformationAction 参数。

[2] 对于 字符串> / Out-File 的行为与 Set-Content 相同,除了在 Windows PowerShell 中的默认值不同字符适用(不在 PowerShell Core 中)。有关差异的更多信息,请参阅this answer

【讨论】:

    猜你喜欢
    • 2012-03-06
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多