【问题标题】:Running netstat from within Powershell script从 Powershell 脚本中运行 netstat
【发布时间】:2015-07-10 10:40:33
【问题描述】:

我正在尝试监控在我的 Windows 7 笔记本电脑上运行的 F5 VPN 客户端,当它建立连接时。我设计了一个简单的 Powershell 脚本,该脚本将利用“netstat”命令来查找某个字符串变量,该变量仅在建立连接时可用。我正计划使用微软的任务计划程序每隔一段时间触发脚本来监控连接/字符串变量。

如果我在 Powershell 窗口中运行以下 netstat 命令,它会很好地返回信息:

    PS C:\tmp> $c = netstat -ban | select-string "F5FltSrv"; $c.count
    9

但是,如果我在 Powershell 脚本中运行类似的运算符和变量,它会一直返回“0”(零),因此会失败。

这是我正在使用的实际 Powershell 脚本:

    $VPN = netstat -ban | select-string "F5FltSvr"

    Write-Host "DEBUG: " $VPN.count

    if($VPN.count -gt 7) {
    Write-Host "F5 VPN Session is enabled." -b Green
    exit
    }
    else {
    Write-Host "F5 VPN Session is down!" -b Red -f Yellow
    }

执行时

    PS C:\tmp> .\F5_VPN_Check.ps1
    0
    F5 VPN Session is down!

谁能告诉我脚本哪里出了问题? Powershell 窗口正在以管理员权限运行,并且 UAC 已关闭。

【问题讨论】:

  • 你在哪个账户下运行这个?

标签: powershell powershell-4.0


【解决方案1】:

会不会是笔误?而不是这个:

$VPN = netstat -ban | select-string "F5FltSvr"

你想要这个:

$VPN = netstat -ban | select-string "F5FltSrv"

至少这是您使用的有效示例。注意SvrSrv 之间的区别。

【讨论】:

  • 哇。一个错字。这就是为什么最好有第二双眼睛。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-13
  • 2019-12-02
  • 2019-09-30
  • 2021-09-11
  • 1970-01-01
  • 2011-02-04
  • 1970-01-01
相关资源
最近更新 更多