【问题标题】:Powershell script not working right with "pause" in the endPowershell脚本最终无法在“暂停”下正常工作
【发布时间】:2020-04-26 22:03:54
【问题描述】:

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

代码如下:

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 年的第二台服务器上,它无法正常工作。

所以我开始调查并得出结论,如果我删除“暂停” 在 2016 服务器上的脚本结束时它运行良好,这很奇怪..

最后是“暂停”,我得到了这个结果:

Showing open files:

Press Enter to continue...: 

非常感谢您的帮助:)

【问题讨论】:

  • @Olaf 我想运行脚本,最后它不会关闭脚本窗口
  • @Olaf 我发现我可以添加powershell -version 5.1 -noexit 但我不明白我什么时候添加它?在乞讨还是在剧本的结尾?
  • @Olaf,谢谢你的帮助,我做到了,但它没有用,它显示相同的输出
  • 它没有显示任何打开的文件(并且有),它只显示“显示打开的文件:按 Enter 继续...:”
  • 现在我明白你的意思了。我以为你在抱怨命令Pause。 ;-) ...您在代码d:\Shares\Share1\app.exe 中引用了本地路径。其他服务器上可能不存在该本地路径。

标签: powershell powershell-4.0


【解决方案1】:

格式表(隐含)无限期地等待 2 个对象。

嗯,格式表在暂停之前永远不会出现。

[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

【讨论】:

  • 它会自动为具有 5 个以下属性的对象运行。
  • 对不起,我不明白
  • 你是我的英雄!这实际上有效! - 只有当我添加了$open | format-list 它才有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多