使用Icinga2 Monitoring的参数执行另一个Powershell脚本

时间:2019-06-24 11:33:54

标签: powershell

我有如下的powershell脚本。如果我按照以下方式运行 test1.ps1 check_host 10.0.6.104 check_port 1433

param (
           $check_host=$args[0],
           $check_port=$args[1]
)

#$check_host="10.0.6.104"
#$check_port="1433"

# Icinga Exit Codes
#0 = OK
#1 = Warning
#2 = Critical
#3 = Unknown

$t = New-Object Net.Sockets.TcpClient

$t.Connect($check_host,$check_port)
    if($t.Connected)
    {
        "Connection to $check_host via $check_port is OK"
        $returncode=0
        $t.Close()
    }
    else
    {

        "Cannot connect to $check_host via $check_port !"
        $returncode=2
    }

exit ($returncode)

但无法以icinga格式运行

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -command "&'C:\Program Files\ICINGA2\sbin\test1.ps1 check_host 10.0.6.104 check_port 1433'" ;exit $LastExitCode

1 个答案:

答案 0 :(得分:0)

脚本的内容对这个问题并不重要;问题是如何使用参数执行Powershell脚本。您可以这样做:

powershell -File "%ProgramFiles%\ICINGA2\sbin\test1.ps1" -check_host 10.0.6.104 -check_port 1433

powershell.exe的文档涵盖了这种情况:

powershell -?

-File
    Runs the specified script in the local scope ("dot-sourced"), so that the
    functions and variables that the script creates are available in the
    current session. Enter the script file path and any parameters.
    File must be the last parameter in the command, because all characters
    typed after the File parameter name are interpreted
    as the script file path followed by the script parameters.