[英]Output file doesn't match Write-Host
When I Write-Host
from my.ps1 file I see:当我从 my.ps1 文件
Write-Host
时,我看到:
Parentfolder >> ChildFolder
When I output to a file, I see:当我 output 到一个文件时,我看到:
ParentFolder
>>
ChildFolder
I am using a simple write-host ($childgroup.name), ">>", ($object.samaccountname)
我正在使用一个简单的
write-host ($childgroup.name), ">>", ($object.samaccountname)
When I try to output the same information using Return
, Out-File
, Export to CSV
, etc... I get 3 lines for what Write-Host
prints as a single line .当我尝试使用
Return
, Out-File
, Export to CSV
等 output 相同的信息时......我得到了3行, Write-Host
打印为单行。
I just want the output file to be in the same format as the Write-Host
output.我只希望 output 文件的格式与
Write-Host
output 的格式相同。
as requested:按照要求:
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")
}
Caveat :警告:
Write-Host
is meant for to-display output , not for outputting data - it bypasses PowerShell's success output stream (PowerShell's stdout equivalent), so that output from Write-Host
cannot (directly [1] ) be captured in a variable, nor redirected to file - see the bottom half of this answer for more information. Write-Host
is meant for to-display output , not for outputting data - it bypasses PowerShell's success output stream (PowerShell's stdout equivalent), so that output from Write-Host
cannot (directly [1] ) be captured in a variable, nor redirected归档 - 有关更多信息,请参阅此答案的下半部分。
Use Write-Output
or - preferably - PowerShell's implicit output behavior to output data , suitable for further programmatic processing .对output数据使用
Write-Output
或 - 最好 - PowerShell 的隐式output行为,适用于进一步的编程处理。
In addition to this fundamental difference, Write-Host
and Write-Output
also differ in how they handle arguments :除了这个根本区别之外,
Write-Host
和Write-Output
在处理arguments的方式上也有所不同:
# 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
Using implicit output, the equivalent of the above Write-Output
command is:使用隐式output,上面的
Write-Output
命令等效为:
# Send an array of 3 strings to the success stream.
PS> 'file1', '>>', 'file2'
file1
>>
file2
If you redirect the Write-Output
command or its implicit equivalent to a file (with >
/ Out-File
or Set-Content
[2] ), you'll get the same 3-line representation.如果您重定向
Write-Output
命令或其隐式等效文件(使用>
/ Out-File
或Set-Content
[2] ),您将获得相同的 3 行表示。
Additionally, Write-Host
performs simple .ToString()
stringification on complex objects , which often results in unhelpful output ;此外,
Write-Host
对复杂对象执行简单的.ToString()
字符串化,这通常会导致无用的 output ; by contrast, Write-Output
/ implicit output uses PowerShell's rich formatting system:相比之下,
Write-Output
/ 隐式 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
Note: If you use the Out-Host
cmdlet and pipe a command to it (eg注意:如果您使用
Out-Host
cmdlet和 pipe 命令(例如@{ foo = 1; bar = 2 } | Out-Host
@{ foo = 1; bar = 2 } | Out-Host
) you do get the usual, rich output formatting that you get via Write-Output
/ by default - while still printing to the display only . @{ foo = 1; bar = 2 } | Out-Host
)你会得到通常的,丰富的 output 格式,你通过Write-Output
/ 默认情况下获得 - 同时仍然只打印到显示器。
If you do want to output a single line as data , use a quoted string ;如果您确实想将 output单行作为数据,请使用带引号的字符串; to reference variables and embed subexpressions in a string, use an expandable string (string interpolation),
"...
" (see about_Quoting_Rules ), or use string concatenation ( +
)要在字符串中引用变量和嵌入子表达式,请使用可扩展字符串(字符串插值)、
"...
”(请参阅 about_Quoting_Rules )或使用字符串连接( +
)
$arg1 = 'file1'; $arg2 = 'file2'
# Expandable string
PS> "$arg1 >> $arg2"
file1 >> file2
# String concatenation
PS> $arg1 + ' >> ' + $arg2
file1 >> file2
[1] In PowerShell v5 or higher, you can capture/redirect Write-Host
output, via the information output stream , number 6
; [1] In PowerShell v5 or higher, you can capture/redirect
Write-Host
output, via the information output stream , number 6
; eg: $writeHostOutput = & { Write-Host hi } 6>&1
.例如:
$writeHostOutput = & { Write-Host hi } 6>&1
。 However, note that the information output stream is primarily designed to work with the PSv5+ Write-Information
cmdlet and the common -InformationAction
parameter.但是,请注意,信息 output stream 主要用于与 PSv5+
Write-Information
cmdlet 和常见的-InformationAction
参数一起使用。
[2] With strings only , >
/ Out-File
behave the same as Set-Content
, except that in Windows PowerShell a different default character applies (not in PowerShell Core ). [2]仅使用字符串时,
>
/ Out-File
的行为与Set-Content
相同,除了在Windows PowerShell中应用不同的默认字符(不在 PowerShell Core中)。 For more information about the differences, see this answer .有关差异的更多信息,请参阅此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.