PowerShell团队消息包装器手动发送但不会自动发送
我有一个脚本,旨在每分钟在站点到站点的VPN隧道上ping另一个主机。 10分钟后,它检查了测试连接的平均正常运行时间,如果它低于一定阈值,它会发送团队消息告诉我们检查事物。
当我手动在原位运行脚本时,这很好,但是,当我将其作为后台作业运行时,它并没有发送团队消息。
我的问题是:作为一个相对较新的Sysadmin,我的工具包中的工具非常有限。有人对我应该开始寻找的地方有一个很好的提示,以解决此问题吗?为了排除我的脚本潜在问题,我还在下面包括了它。但是我怀疑这个问题与让脚本在我登录的服务器上运行更多有关。所讨论的服务器正在运行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
然后最小化终端并与服务器断开连接。我怀疑问题与此有关,我缺少一些很明显的东西。
I have a script that's designed to ping another host across a site-to-site VPN tunnel every minute. After 10 minutes, it checks the average uptime of test-connection, and if it falls below a certain threshold, it sends a Teams message telling us to check things out.
This works perfectly well when I manually run the script in situ, however, when I leave it to run as a background job, it isn't sending the Teams messages.
My question is this: as a relatively new sysadmin, the tools in my toolkit are pretty limited. Does anyone have a good tip for where I should start looking, to troubleshoot this issue? To rule out potential problems with my script, I've also included it below. But I suspect the issue is more to do with leaving the script to run on a server that I then log out of. The server in question is running Windows Server 2012 (yes I know, migration is on my to-do list).
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
And the module code:
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
Right now, I'm calling the main file with:
start-job -file C:\path\to\file.ps1
Then minimizing the terminal and disconnecting from the server. I suspect the problem is something to do with this, that I'm missing something really obvious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,这个问题与另一个问题非常相似,尽管它们的措辞非常不同。
基本上,我需要做的是将脚本作为NT Authority \ System在启动时运行。 在Windows上运行无限命令服务器即使有人登录
As it turns out, this question is quite similar to another, though they're phrased very differently.
Basically, what I need to do is to run the script as NT AUTHORITY\System on startup. Run an infinite command on Windows Server even if someone is logged out