【问题标题】:Output of Powershell script displaying incorrectlyPowershell 脚本的输出显示不正确
【发布时间】:2019-03-31 06:31:48
【问题描述】:

我们在这里有一个脚本,用于显示组和组中的用户。该脚本正在运行并为我们提供输出中的信息。这是脚本:

Clear-Host
Write-Host "Group Query & Edit Tool." -ForegroundColor Yellow
Start-Sleep -Seconds 2
Start-Process "\\capeplc.net\IT-GroupData\Onsite Group Memberships\GBSSOnsiteGroupList.txt"
$Group = Read-Host "Enter the name of the group you require memberships pulling from"
Write-Host " "
Write-Host "Users who are members of $Group :"
Get-ADGroupMember -Identity $Group | Select Name,SAMAccountName -Wait
Start-Sleep -Seconds 1
$GroupRemove = Read-Host "Do you want to remove anyone from $Group ?"
if ($GroupRemove -eq "Y"){

$User = Read-Host "Enter the username of the user who needs removing 
(SAMAccountName)"
Remove-ADGroupMember -Identity $Group -Members $User

}
else {

Write-Host "Ending script..."
exit

}

然而,输出显示名称和 SAMAccountName 在整个脚本下方,在它说结束脚本的位置之后。我希望它显示在写着“作为 $Group 成员的用户”的写主机和 $GroupRemove = Read-Host “你想从 $Group 中删除任何人”之间

谁能告诉我在脚本中做错了什么,为什么它显示在输出的错误位置?

以下是输出供参考:

Group Query & Edit Tool.
Enter the name of the group you require memberships pulling from: GBSS-Onsite-Admin-Aldborough

Users who are members of GBSS-Onsite-Admin-Aldborough :

Do you want to remove anyone from GBSS-Onsite-Admin-Aldborough ?: N
Ending script...
Name         SAMAccountName
----         --------------
Test User    Test.User

【问题讨论】:

标签: powershell output


【解决方案1】:

powershell 对发送到标准输出的任何内容都有内置延迟。 Write-Host 转到主机,而不是标准输出。因此,如果显示系统认为途中可能有更多相同类型的对象,您可能会遇到延迟。我认为延迟是 300 毫秒,但我不确定。

解决方法是执行以下操作之一...

  • 不要将主机输出与任何其他旨在到达屏幕的输出混在一起 [grin] [aka - hee-haw “不要那样做!”]
  • | Out-Host 添加到非主机显示项目以强制项目到主机而不通过显示系统延迟例程

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 2013-01-07
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多