在Write-Host和Read-Host之后,PowerShell命令的输出不会显示结果 [英] Output of a PowerShell command doesn't show result until after Write-Host and Read-Host

查看:774
本文介绍了在Write-Host和Read-Host之后,PowerShell命令的输出不会显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到我在代码末尾输入回车符后,代码才会返回Get-ADUser命令的结果

Code will not return the results of the Get-ADUser command until after I hit a carriage return at the end of the code

    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

我在这里想念什么?

推荐答案

写入主机项目在执行命令期间立即显示。

The Write-Host items are immediately displayed during the execution of your command.

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.

如果您确实要显示输出,可以添加 |在 Get-ADUser 调用的末尾写入主机。如果您想将此连接到另一个脚本,则可以将值捕获到一个变量中,然后同时写入主机,然后写入输出将其重新添加到管道中。

If you really and truly just want to display the output, you can add | Write-Host to the end of your Get-ADUser call. 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.

另请参见:了解Windows PowerShell管道

这篇关于在Write-Host和Read-Host之后,PowerShell命令的输出不会显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆