【问题标题】:Powershell Teams message wrapper sending manually but not automaticallyPowershell Teams 消息包装器手动发送但不自动发送
【发布时间】:2022-06-16 01:16:42
【问题描述】:

我有一个脚本,旨在每分钟通过站点到站点 VPN 隧道 ping 另一台主机。 10 分钟后,它会检查测试连接的平均正常运行时间,如果低于某个阈值,它会发送一条 Teams 消息,告诉我们进行检查。

当我在原地手动运行脚本时效果很好,但是,当我让它作为后台作业运行时,它不会发送 Teams 消息。

我的问题是:作为一个相对较新的系统管理员,我的工具包中的工具非常有限。有没有人对我应该从哪里开始寻找以解决这个问题有一个很好的提示?为了排除我的脚本的潜在问题,我还将它包含在下面。但我怀疑这个问题更多地与让脚本在我然后注销的服务器上运行有关。有问题的服务器正在运行 Windows Server 2012(是的,我知道,迁移在我的待办事项清单上)。

Import-Module message_module # a module i wrote to wrap messages to Teams webhooks (included below)

# this array will accept output values from the ongoing test
$test_table = new-object system.collections.arraylist
# this index counts how many times we've checked recently
[int32[]]$test_index = 1
# our desired threshold for uptime / response
$uptime = .8
# how many minutes to count before testing
$count_length = 10
# IP to ping
$ping_ip = 'XXX.XXX.XXX.XXX'
$test_ip = '142.251.33.110' # google.com, used for testing

# here's the actual function that does the pinging and puts values in the arraylist
function Ping-VPN {
    $ping_host = test-connection $ping_ip -erroraction silentlycontinue
    if ( $ping_host ) {
        $test_table.add(1) > $null
    } else {
        $test_table.add(0) > $null
    }
}

# this function calculates the average of the values in test_table, and then clears them
function Get-Average-Uptime {
    $sum = 0
    foreach ($entry in $test_table) {
        $sum += $entry
    }
    $avg = $sum / $test_table.count
    return $avg
}

function Main-Loop {
    while ( $test_index -lt $count_length ) {
        Ping-VPN
        $test_index += 1
        start-sleep -seconds 60
    }
    $avguptime = Get-Average-Uptime
    $test_table.clear
    if ( $avguptime -lt $uptime ) {
        $title = "XXX/XXX VPN Down"
        $message = "XXXXXX response to ping from XXXXXXX at less than desired rate.  Please investigate."
        Send-TeamsMessage -Message $message -Title $title
        start-sleep -seconds 3600 # sleep for an hour, to avoid spamming us
    }
    $test_index = 0 # restart the testing interval
    Main-Loop
}

Main-Loop

以及模块代码:

function Send-TeamsMessage {

    Param( 
        [Parameter(Position = 0, Mandatory = $true)][String]$Message, 
        [Parameter(Position = 1, Mandatory = $true)][String]$Title
    ) 

    $JSONBody = [PSCustomObject][Ordered]@{
        "@type"      = "MessageCard"
        "@context"   = "http://schema.org/extensions"
        "themeColor" = '0078D7'
        "title"      = $Title
        "text"       = $Message
    }
    
    $TeamMessageBody = ConvertTo-Json $JSONBody -Depth 100
    
    $parameters = @{
        "URI"         = 'XXXXXXXX (webhook URI)'
        "Method"      = 'POST'
        "Body"        = $TeamMessageBody
        "ContentType" = 'application/json'
    }
    
    Invoke-RestMethod @parameters | Out-Null
}

Export-ModuleMember -Function Send-TeamsMessage

现在,我正在调用主文件:

start-job -file C:\path\to\file.ps1

然后最小化终端并断开与服务器的连接。我怀疑问题与此有关,我遗漏了一些非常明显的东西。

【问题讨论】:

    标签: powershell microsoft-teams webhooks windows-server


    【解决方案1】:

    事实证明,这个问题与另一个问题非常相似,尽管它们的措辞非常不同。

    基本上,我需要做的是在启动时将脚本作为 NT AUTHORITY\System 运行。 Run an infinite command on Windows Server even if someone is logged out

    以重复的形式结束我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 2020-12-01
      相关资源
      最近更新 更多