【问题标题】:Testing for Remoting in Powershell?在 Powershell 中测试远程处理?
【发布时间】:2015-11-17 11:54:33
【问题描述】:

我有一个脚本可以从计算机列表中检索某些事件日志条目。我在带有 Get-EventLog -Computername $c... 的计算机列表上使用 foreach 循环来获取事件。我使用 Test-Connection MyComputer -Quiet 来确保每台计算机都已启动。如果启用了远程处理,我无法弄清楚如何以编程方式进行测试。我试过了

if (!(Test-WSMan MyComputer))

尝试连接到远程处理以测试远程处理是否启用时仍然会引发错误。好像是第 22 条军规。您只能通过连接到 Remoting 来测试是否启用了 Remoting,如果未启用,则会出现错误。

我也尝试将它包装在 Try...Catch 中,但没有发现错误。

有没有办法从脚本中检查计算机是否有 Remoting 可用?

注意:我在所有安装 PowerShell 的地方都运行 PowerShell 4.0。

【问题讨论】:

  • -ErrorAction SilentlyContinue
  • 您应该将其添加为答案,以便我可以标记它。这要简单得多。谢谢!!

标签: powershell remoting powershell-remoting


【解决方案1】:

如果您只想抑制错误消息,那么-ErrorAction SilentlyContinue 应该足以满足您的需求:

if (!(Test-WSMan MyComputer -ErrorAction SilentlyContinue))

如果您不想将错误添加到$Error 自动变量,也可以使用-ErrorAction Ignore

【讨论】:

    【解决方案2】:

    你可以使用这个功能:

    Function Test-PSRemoting
    {
    Param($Computer)
    
    $ErrorActionPreference = "Stop"
        Try 
        {
            if ((Invoke-Command -ComputerName $Computer -ScriptBlock {1}) -eq "1")
            {
            return $true
            }
    
                else
                {
                return $false
                }
        }
    
        Catch
        {
        return $false
        }
    
    }
    

    像这样使用它:

    Test-PSRemoting ComputerName
    True
    

    【讨论】:

      猜你喜欢
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2018-12-24
      • 2010-12-14
      相关资源
      最近更新 更多