Install, wait, then uninstall. And report back with Teams webhook

Hello,
I created a PS script that interacts with the user, installs an application then waits 10 minutes before it uninstalls it then notifies me on teams this was performed thought a webhook.

Using the toolkit it seems a lot cleaner as far as user interaction. I pretty much got everything in there. Except for the post installation part that uses the webhook powershell code. Does anybody know how I might make this webhook part of the deploy script, I might be overthinking it. Thank you in advance.

# Define the webhook URL provided by Microsoft Teams
$webhookUrl = "https://mysite.com"

# Get the computer name 
# Get the logged-in user
$computerName = $env:COMPUTERNAME
$loggedInUser = $env:USERNAME


$message = @{
    "@type" = "MessageCard"
    "@context" = "http://schema.org/extensions"
    "summary" = "Computer Information"
    "themeColor" = "0072C6"
    "title" = "Generic app used"
    "sections" = @(
        @{
            "activityTitle" = "User Name: $loggedInUser"
            "text" = "Computer Name: $computerName"
        }
    )
} 

# Send the message to the Teams webhook
Convert the message to JSON format
$jsonMessage = $message | ConvertTo-Json

try {
     #Send the message to the Teams channel via webhook
     Invoke-RestMethod -Uri $webhookUrl -Method Post -Body $jsonMessage -ContentType 'application/json'
     Write-Host "Message sent successfully to Microsoft Teams channel."
} catch {
    Write-Host "Failed to send message to Microsoft Teams channel: $_"
}

Interesting usage. This should be doable.

I’m assuming this is launched by something like Intune or SCCM.
And you do not use the uninstall section of PSADT because it’s all done in one PowerShell session.

Does the “PSADT Install” run as the User or SYSTEM?
Does the WebHook code need to run as the User or SYSTEM?

I just realized I forgot to modify the name of the thread to a more pertinent title, I solved that wait problem. This script will be run in both intune and cm, I have it all transposed to the psadmin toolkit ps script except for the webhhook part of it . Both script and webhook should be run as system. If i could somehow place it on the post installation part of the psadmin script that would be ideal, I was hoping maybe like a function or something

PSADT has a file for just this purpose called AppDeployToolkitExtensions.ps1. You put your own code in this file in the form of functions that is then easily called from your Deploy-Application.ps1 script. Adding custom functionality to just this file makes it easier when updating the toolkit to a new version.

Toolkit File Structure · PSAppDeployToolkit/PSAppDeployToolkit Wiki (github.com)

AppDeployToolkitExtensions.ps1

This is an optional PowerShell script that can be used to extend the toolkit functionality with custom functions. It is automatically dot-sourced by the AppDeployToolkitMain.ps1 script.

1 Like

Hello !

Let’s say it otherwise to check if my understanding of your issue is OK :

  • PSADT running as system with user interaction
  • Its goal is to install an app then wait 10min (sleep tempo ?) then uninstall it
  • At the end you want to be notifed using your webhook

Is that correct ?

What is wrong with your webhook code ? any error to share ? have you tried to execute it as localsystem with sysinternals PSEXEC.EXE -i -s to check it can work in system context ?

1 Like

I will play around with this and see if I can get it work, I appreciate the feedback

hello, correct that is the gist of it. Good question and maybe thats where the problem lies. That’s the only part of my script that returns a message, with a bad parameter in the message section of the code of the webhook but it still sends the message to my teams channel fine. Even when I upload my script to intune it runs through all the code including the webhook. So I’m assuming there is something in the webhook that needs to be fixed even though i’ve spent some time on it and I’m just not seing what is causing that. if I can’t get it to work on admin tool I might just use my script I just wanted to keep it consistent with all my app deployments using the toolkit.

If PSADT runs as SYSTEM, maybe the webhook want to use SYSTEM’s Teams account that doesn’t exist.

if that’s the case, run the webhook code as the user that is running the app you just installed.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.