Author Topic: Command file and launching powershell script  (Read 3177 times)

dlettani

  • Member
  • *
  • Posts: 8
    • View Profile
Command file and launching powershell script
« on: 27 Jun 2017, 21:31:58 »
Hi all,

I've set up about 20 filters with labels and activated command file output with variables active.
The command file launches a batch file with one single line:
powershell -NoProfile -executionpolicy bypass -file C:\Users\David\PDWpowershell\pushover4.ps1 %1 %2 %3 %4 >>testlogbatch.txt

-> sole use of this .bat file is thus to call my Powershell script (passing the variables once more) that calls some API's on the net.  Return is written in testlogbatch.txt for troubleshooting.
In fact I enabled push messaging on my mobile phone for every message that triggers a filter using a free service  ;D
The powershell is only about 100 lines of very simple code to add some more functionality and finally calls the API for creating the push message on smartphone.

This setup works fine and is blazing fast.
There's only one thing that bothers me: whenever two filters are triggered just one after another (with just 1 second delay between both) it seems that the second launch of the same batch file fails because the first command window is still open... Once there is more than 1 second between the messages all is fine.
Anybody a clue on this?
Should I close the command prompt explicitly in my batch file and add a second line that says Exit
Should I use the 'start' command and add this in my first line of the batch file? From what I read I guess this will make sure to start every time an new session once this batc file is called?
START powershell -NoProfile -executionpolicy bypass -file C:\Users\David\PDWpowershell\pushover4.ps1 %1 %2 %3 %4 >>testlogbatch.txt

Thanks!

 

VidarParry

  • Top Member
  • ***
  • Posts: 123
    • View Profile
Re: Command file and launching powershell script
« Reply #1 on: 28 Jun 2017, 07:19:28 »
Hi David,
Looks like you've developed a good system there - I tried developing a similar solution to push the emails to SMS but wasn't successful and gave up! Would you be willing to share?

Re your problem, I think trial and error of your suggestions is the way to go - I can't help specifically on that. I assume you've Googled? You might get some help from stackoverflow - there's still a few DOS experts there!

Cheers
...Steve

dlettani

  • Member
  • *
  • Posts: 8
    • View Profile
Re: Command file and launching powershell script
« Reply #2 on: 28 Jun 2017, 14:41:50 »
Too much to do, too little time....  >:(
I'll give it a try.

Have a look at https://pushover.net/
Download the app, create an account and head over to their website where you set up everything you need.
I started by using the email2push gateway but then wrote the script to directly call their api's and created an application key (eg; a separate channel in Puhover notifications).
Have a go and let me know if you have questions.

I'm now looking into the subscription API, just added a direct navigation Waze link based on the message text that contains the address info...

VidarParry

  • Top Member
  • ***
  • Posts: 123
    • View Profile
Re: Command file and launching powershell script
« Reply #3 on: 28 Jun 2017, 22:03:25 »
Know what you mean about no time!  ;D

Thanks for the info - I'll take a look at Pushover - certainly looks promising.

Cheers
...Steve

dlettani

  • Member
  • *
  • Posts: 8
    • View Profile
Re: Command file and launching powershell script
« Reply #4 on: 29 Jun 2017, 14:26:17 »
The powershell script part that initiates the call for push message:


$uri = "https://api.pushover.net/1/messages.json"
$parameters = @{
  token = "xyz" #your app token, cf pushover.net, name it, upload icon etc this will create separate channel in the app
  user = $user #the userkey/groupkey to whom you want to address the push message
  title = $title #the title
  message = $args[0] #the 'body', here an argument of some other params in my script
 
  }


Enjoy ;-)

$parameters | Invoke-RestMethod -Uri $uri -Method Post

VidarParry

  • Top Member
  • ***
  • Posts: 123
    • View Profile
Re: Command file and launching powershell script
« Reply #5 on: 29 Jun 2017, 15:08:41 »
Thanks mate:-)

Tazz

  • Member
  • *
  • Posts: 2
    • View Profile
Re: Command file and launching powershell script
« Reply #6 on: 29 Jul 2017, 21:02:19 »
Hi dlettani,

I've been using a Python script for a fair while to do similar granular filtration and sending to different groups with Pushover. (PDW doesn't natively support the command file going to .py files, so I have a batch which PDW addresses that then forwards the variables to the Python script.. It's dirty but it works)

I am having an issue however where when I start PDW, the command file is enabled etc - but it just doesn't call the batch file. I have to actually go in to the Filter settings, disable and re-enable the command file option.
Have you had any similar issues?

dlettani

  • Member
  • *
  • Posts: 8
    • View Profile
Re: Command file and launching powershell script
« Reply #7 on: 31 Jul 2017, 13:02:47 »
Hi Tazz,
No, haven't encountered that one yet.. I'm using autologon and autorun utilities to make sure that whenever there was a windows update my computer and PDW starts up again and that worked several times wihtout the issue you mention.
I'm on PDW 3.12


dlettani

  • Member
  • *
  • Posts: 8
    • View Profile
Re: Command file and launching powershell script
« Reply #8 on: 07 Aug 2017, 16:49:27 »
Strange... had to reboot my computer and now experienced the issue you described.. weird.. found any answers in the meanwhile... might be the startup filter file?

Tazz

  • Member
  • *
  • Posts: 2
    • View Profile
Re: Command file and launching powershell script
« Reply #9 on: 23 Aug 2017, 01:17:49 »
I actually stumbled upon a possible fix in the early hours of this morning whilst tinkering around...

Turns out you can run cmd.exe with a flag of /c <command or file>
Typically used for some automation type processes, however can also be used in this example!

SO... In my Filter options, for "command file" - instead of calling the batch file directly, I'm now calling C:\Windows\System32\cmd.exe
Then, in the "command file arguments" section I have /c C:\Users\Tazz\batch.bat %1 "%8" "%7"

IN FACT, I have just discovered I can take a link out of the chain and call my Python script directly as .py's are registered as executables..
SO, my "command file arguments" section now has /c C:\Users\Tazz\pushover.py %1 "%8" "%7"


In your scenario, I imagine your command file could call C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
And your arguments could possibly then be -NoProfile -executionpolicy bypass -file C:\Users\David\PDWpowershell\pushover4.ps1 %1 %2 %3 %4

Or similar... Let me know how you go!
« Last Edit: 23 Aug 2017, 01:28:32 by Tazz »

dlettani

  • Member
  • *
  • Posts: 8
    • View Profile
Re: Command file and launching powershell script
« Reply #10 on: 23 Aug 2017, 10:37:39 »
Ha, genius, thanks!
I'll give it a go, hopefully this will indeed sort the issue we've experienced before.

I love this Pushover implementation!  ;D

dlettani

  • Member
  • *
  • Posts: 8
    • View Profile
Re: Command file and launching powershell script
« Reply #11 on: 24 Aug 2017, 19:36:06 »
Has been implemented. I noticed there is a scheduled update this evening on my pc with reboot so results coming up bit later...

Sub16

  • Member
  • *
  • Posts: 6
    • View Profile
Re: Command file and launching powershell script
« Reply #12 on: 12 Aug 2019, 04:07:18 »
Hi guys i look for a PDW tool that allows you to send your filter pager message to pushover can any one help me thank you.

dlettani

  • Member
  • *
  • Posts: 8
    • View Profile
Re: Command file and launching powershell script
« Reply #13 on: 12 Aug 2019, 08:15:39 »
You'll have to use the output command and then write a (powershell) script to relay that info to Pushover. That's how I did it and that worked flawless...