Powershell 脚本最终无法与 "pause" 一起正常工作

Powershell script not working right with "pause" in the end

我的脚本有一个奇怪的问题,它在两台不同的服务器上运行,在一台服务器上运行良好,而在另一台服务器上却表现得很奇怪。

代码如下:

write-host "Cheking if App open please wait"

write-host "`n"

$open = Get-SmbOpenFile | 
          Where-Object {$_.Path -eq "d:\Shares\Share1\app.exe"} |
            Select-Object @{l="Who is using App?";e="ClientUserName"}, Path

if ($open) {
 write-host "Showing open files:"
  $open

}

else {
  write-host "all closed"
}

pause

在装有 powershell 3.0 版的服务器 2012 r2 上运行良好, 在 2016 年的第二台服务器上,它无法正常工作。

所以我开始调查并得出结论,如果我删除 "pause" 在 2016 服务器上的脚本末尾,它运行完美,这很奇怪..

最后 "pause",我得到了这个结果:

Showing open files:

Press Enter to continue...: 

非常感谢您的帮助:)

Format-table(暗示)无限期等待 2 个对象。

嗯,格式-table在暂停之前永远不会出来。

[pscustomobject]@{name='joe'}; pause

Press Enter to continue...:
name
----
joe

有了format-list,马上就出来了:

[pscustomobject]@{name='joe'}|format-list; pause

name : joe


Press Enter to continue...: 

这应该有效:

write-host "Cheking if App open please wait"

write-host "`n"

$open = Get-SmbOpenFile | 
          Where-Object {$_.Path -eq "d:\Shares\Share1\app.exe"} |
            Select-Object @{l="Who is using App?";e="ClientUserName"}, Path

if ($open) {
 write-host "Showing open files:"
  $open | format-list

}

else {
  write-host "all closed"
}

pause