通过powershell.exe传递参数

时间:2017-03-07 13:42:22

标签: powershell

我写了这段代码

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$true)]
    [Int32]$BoxAlert,
    [Parameter(Mandatory=$true)]
    [Int32]$MailAlert
)
)

powershell.exe -WindowStyle Hidden {
    if ($timeSpan.Days -ge $BoxAlert) {
        drawPopupBox $result
    }
    if ($timeSpan.Days -ge $MailAlert) {
        sendMail $result;
    }
}

如何在$BoxAlert脚本块中传递$MailAlertpowershell.exe

1 个答案:

答案 0 :(得分:1)

只需在脚本块之后添加-args开关,并在param()定义中添加脚本块。

是一个简单的版本
$x = bar    
powershell.exe -command {param($x) write-host "foo, $x"} -args $x

提供以下输出

foo, bar

将此逻辑应用于您的代码

 PowerShell.exe -WindowStyle Hidden -command {
  param($BoxAlert, $MailAlert)

  if($timeSpan.Days -ge $BoxAlert)
  {
      drawPopupBox $result
  }
  if($timeSpan.Days -ge $MailAlert)
  {
        sendMail $result;
  }

} -args $BoxAlert, $MailAlert