【问题标题】:powershell arguments with nrpe带有 nrpe 的 powershell 参数
【发布时间】:2020-10-21 09:22:59
【问题描述】:

我想通过 powershell 脚本传递两个论点。

这是常规检查

test = cmd /c echo scripts\test.ps1 ;退出($lastexitcode) | powershell.exe -command -

这就是我想要的想法。设置警告和关键。

test = cmd /c echo scripts\test.ps1 -w 10 -c 50 ;退出($lastexitcode) | powershell.exe -command -

如果警告设置为超过 10 则将返回退出 1

如果 critical 设置超过 50,那么它将返回 exit 2

不确定如何在我的脚本中执行此操作。

这是它现在的样子。

$condition = (Get-Service | Where-Object Status -eq "Running").Count

if ($argument warn) {
    Write-Output "Warning:" $condition
    exit 1 
}

ElseIf ($argument critical) {
        Write-Output "Critical:" $condition
        exit 2
}

【问题讨论】:

    标签: powershell nrpe


    【解决方案1】:

    虽然我不太确定这是否是您的意思,但您可以使用 param() 块启动脚本,这样它就可以接受像

    这样的参数
    param (
        [int]$WarningLevel = 0,
        [int]$CriticalLevel = 0
    )
    
    $condition = (Get-Service | Where-Object {$_.Status -eq "Running"}).Count
    
    if ($CriticalLevel -gt 0 -and $condition -gt $CriticalLevel) {
        Write-Output "Critical: Way too many proc's: $condition"
        Exit 2
    }
    elseif ($WarningLevel -gt 0 -and  $condition -gt $WarningLevel) {
        Write-Output "Warning: proc's filling up: $condition"
        Exit 1 
    }
    Write-Output "All OK"
    Exit 0
    

    【讨论】:

    • 如果脚本存储为D:\Test\script.ps1,您可以将其称为powershell -File "D:\Test\script.ps1" -WarningLevel 10 -CriticalLevel 50。附:我已经编辑了代码,因为我之前不知何故忘记了一个右大括号。
    • 谢谢@Theo。这就是我想要的。
    • @Larsvontrierpung 我很高兴它对你有用。如果您觉得我的回答解决了您的问题,请考虑通过单击左侧的 ✓ 图标来接受答案。见How to accept SO answers。这将帮助其他有类似问题的人更轻松地找到它。
    • 会的!但是出现了一些问题。 powershell -File .\test.ps1 Critical: Way too many proc: 116 如果我不输入任何参数应该没问题
    • @Larsvontrierpung 嗯,如果超过 50 个服务处于运行状态,则输出。数字 50 是一个非常低的数字。目前在我的电脑上有 104。确定您认为关键的数字并相应地调整值,例如powershell -File "D:\Test\script.ps1" -WarningLevel 100 -CriticalLevel 200。毕竟它们是决定的参数。
    【解决方案2】:

    你可以使用,

    powershell -file "(Path to your PowerShell Script).ps1 -w10 -c50"

    在顶部的 PowerShell 脚本中,插入以下内容:

    param ($w, $c)

    然后使用脚本中的变量。

    了解有关命名参数的更多信息,因此您不必按使用顺序输入参数,并且在您想要进行更改时脚本中断的可能性较小。

    【讨论】:

    • 谢谢@Shaqil Ismail :)
    猜你喜欢
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    相关资源
    最近更新 更多