[英]Output of a PowerShell command doesn't show result until after Write-Host and Read-Host
Code will not return the results of the Get-ADUser command until after I hit a carriage return at the end of the code 直到我在代码末尾输入回车符后,代码才会返回Get-ADUser命令的结果
Write-Host "Please enter the user account name. Ex. jsmith1" -ForegroundColor Yellow -BackgroundColor Black
$Username = Read-Host
"`n"
Write-Host "Is this the correct username? $Username" -ForegroundColor Yellow -BackgroundColor Black
Write-Host "If Yes Type 1; If No type 2" -ForegroundColor Yellow -BackgroundColor Black
$Continue1 = Read-Host
IF ($Continue1 -eq 1)
{
Get-ADUser -Server "contoso.com" -filter * -Properties * | Where {$_.SamAccountName -match $Username} | Select DisplayName, SamAccountName, EmployeeID, EmployeeNumber
}
ELSE
{}
Write-Host "Would you like to update the Employee ID and the Employee Number?" -ForegroundColor Yellow -BackgroundColor Black
Write-Host "If Yes Type 1; If No type 2" -ForegroundColor Yellow -BackgroundColor Black
$Continue2 = Read-Host
What am I missing here? 我在这里想念什么?
The Write-Host
items are immediately displayed during the execution of your command. 在执行命令期间,将立即显示Write-Host
项目。
The Get-ADUser
results are on the output pipeline. Get-ADUser
结果在输出管道上。
Normally this allows you to return the value into another script, for example, for further processing. 通常,这使您可以将值返回到另一个脚本中,例如,以进行进一步处理。 Since you don't assign it or capture the output, it simply goes to a default formatter and displays after everything else before the execution ends. 由于您没有分配它或捕获输出,因此它只会转到默认格式化程序并在执行结束之前显示在所有其他内容之后。
If you really and truly just want to display the output, you can add | Write-Host
如果确实要显示输出,可以添加| Write-Host
| Write-Host
to the end of your Get-ADUser
call. | Write-Host
Get-ADUser
调用的末尾。 If you ever want to connect this to another script, you can capture the value to a variable, then both Write-Host
, and then Write-Output
to add it back on the pipeline. 如果您想将此连接到另一个脚本,则可以将值捕获到变量,然后捕获Write-Host
,然后捕获Write-Output
以将其重新添加到管道中。
See also: Understanding the Windows PowerShell Pipeline 另请参阅: 了解Windows PowerShell管道
I found that you can also just add some formatting to the end and it will output as you would expect, when you expect it. 我发现您还可以在末尾添加一些格式,并在您期望的时候按预期输出。 I added this formatting to the end: 我将这种格式添加到最后:
| | FT -AutoSize FT-自动尺寸
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.