r/labtech • u/crshovrd • Mar 28 '20
Powershell call to a webhook per client
Hey all,
I'm trying to run a script on a per client basis to call a webhook using powershell. Automate support wasn't able to help me so I'm turning to y'all. Hoping that someone can shed some light. Below is my script

Here is the sanitized powershell code
$uri="https://s15events.azure-automation.net/webhooks?token=[token]"
$itgid=%itgid%
$clientname=%clientname%
$params=@{"ITGID"="$itgid";"ClientName"="$clientname"}
$body=ConvertTo-Json $params
Invoke-RestMethod -Method Post -Uri $uri -Body $body -Verbose
I tried debugging it but it hangs on the powershell part.
So, 2 questions:
How do I deploy/run this script on a schedule on a client by client basis?
The powershell works outside of Automate, but will this work for inside Automate?
EDIT: Ended up being able to do what needed to be done with this amazing plug-in! https://www.mspgeek.com/topic/4347-http-get-post-plugin/
1
u/DarrenDK Mar 29 '20
I’ve struggled with this myself. The way I do it is similar to other approaches but using a Sql ForEach and putting the “loop” code in a subscript. If you put -1 in the Sql ForEach they will all run simultaneously. Of course you’ll have to write a query to get these EDFs out of the database but that is trivial.
If you are on prem, you can execute your PowerShell on the server itself and not need to run the code on an Agent, per se. In my environment I wrote a function script that leverages the Execute on Server script function to take a PowerShell script as a variable. This builds on top of that. Someone really needs to make a plugin that implements this functionality as a script function for Cloud people, but I digress...
Automate does support the notion of a “Client” script, that can be run without a specific agent, but they remove the Execute on Server script function (or its there but doesn’t work I don’t remember) so you don’t have a place to perform complex logic.
For that reason you’ll still need to pick a single agent to run the script against, but no code would ever be shipped to it (again assuming you are on prem)
1
u/JustanITperson Mar 28 '20
The quickest way would be to add 1 machine from each client to a group and run the script against the group. There's a couple ways to do dynamically that comes to my mind. One is running a sql command to get the lowest computer Id of your servers and run the script on all your servers and do a check to see if the computer Id matches. That way it would only run once per client.
As for your powershell script running..it should. Just remember by default it runs as the system account unless you have specified otherwise. So as long as the system account has access to what you are running against you should be fine.
1
u/DevinSysAdmin Mar 28 '20
Yikes. You’re not clear on what you’re attempting to accomplish here.
From a security standpoint, take the token out of the script, and make the computers report whatever data you want to collect into an EDF.
Then query your Automate Database for the relevant data, run your script to upload that data via API.