0

I have written this code

[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;
    }
}

How to pass that $BoxAlert and $MailAlert inside the powershell.exe scriptblock?

4
  • 1
    What is the purpose of launching PowerShell.exe inside powershell? Why not Invoke-Command or Start-Job? Commented Mar 7, 2017 at 13:53
  • As PowerShell is being called with a hidden window, I assume it's an attempt to hide/suppress the console output. If so using Out-Null or >$null 2>&1 would be a much easier solution. Commented Mar 7, 2017 at 14:04
  • Please take a step back and describe the actual problem you're trying to solve instead of what you perceive as the solution. What is the context this code is supposed to be running in? Why do you think you need powershell.exe -WindowStyle Hidden? Also, note that you have a spurious closing parenthesis after your Param() block. Commented Mar 7, 2017 at 14:15
  • Sorry for not elaborating, actually i was creating a script which will calculate current up-time of an System. so, if the uptime is -ge 2 days then a popup message box should appear or if uptime is -ge 4 days then a mail shuold be sent to the user. and i've used Powershell.exe so that the powershell window should not appear while running the script. I wanted to pass that two parameters to script, so that the threshold limit of popup message box and mail alert can be modified through passing the parameters. Commented Mar 7, 2017 at 21:25

1 Answer 1

1

Just need to add the -args switch after your scriptblock and a param() definition insides your script block. A simple version is

$x = bar    
powershell.exe -command {param($x) write-host "foo, $x"} -args $x

Gives the following output

foo, bar

Applying this logic to your code

 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
Sign up to request clarification or add additional context in comments.

1 Comment

[CmdletBinding()] Param( [Parameter(Mandatory=$True)][Int32]$BoxAlert, [Parameter(Mandatory=$True)][Int32]$MailAlert ) PowerShell.exe -WindowStyle Hidden{ param($BoxAlert, $MailAlert) if($timeSpan.Days -ge $BoxAlert) { drawPopupBox $result } if($timeSpan.Days -ge $MailAlert) { sendMail $result; } } -args $BoxAlert,$MailAlert i have changed my code like this and its working now. am passing the value in the variables and the if block is working accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.