Would you like to receive an email when a new Application Request is submitted by an end-user? I’d love to and that’s the reason I created this small PowerShell script to take care of just that. The script is very basic and should be setup to run by a scheduled task on a repeating schedule, e.g. 10 minutes or whatever suits the environment. See this post on how to setup the scheduled task.

How it works

The script gets each submitted Application Request by querying WMI, more exactly the SMS_UserApplicationRequest class. It looks for objects where the CurrentState property is set to 1. For each object found, it tries to match the objects GUID property value from previous requests stored in a text file. If there’s a match, it will not send an email notification. Instead when the GUID can’t be matched to any entry in the text file, an email notification is sent with the user name, application name and comments from the submitted Application Request.

The script

If you’re looking for an alternative for this script, I highly recommend Coretech’s CTAA web application that can be found here:
http://blog.coretech.dk/kea/enabling-email-approvals-for-your-requested-applications-in-configuration-manager-2012/

#Hard-coded variables
$SiteCode = "P01"
$ComputerName = "<NetBIOSName>"
$GUIDFilePath = "C:\Scripts\GUIDList.txt"
$FilePath = "E:\Scripts"

#Get the entries from GUIDList.txt
$GetGUID = Get-Content -Path $GUIDFilePath

#Get all Application Requests with a CurrentState of "1"
$GetAppRequest = Get-WmiObject -Class SMS_UserApplicationRequest -Namespace root/SMS/site_$SiteCode -ComputerName $ComputerName | Where-Object {$_.CurrentState -like "1"} | ForEach-Object {
if ($GetGUID -contains $_.RequestGuid) {
Write-Host "Application request $($_.RequestGuid) already present"
}
else {
$appUser = $_.User
$appName = $_.Application
$appComment = $_.Comments
$Body = @"
Application request: $appName
User: $appUser

Comment: $appComment
"@ #This row can't contain any blank spaces or tabs
#Email configuration
Send-MailMessage -SmtpServer "<smtp_server_FQDN>" -From "sender@domain.com" -To "receiver@domain.com" -Subject "New Application Request has been submitted" -Body $body
#Append the current objects GUID to GUIDList.txt
$_.RequestGuid | Out-File $GUIDFilePath -Append
}
}

#Remove the GUIDList.txt file and re-create it when there's more than 100 entries
$GUIDCount = $GetGUID.Count
if ($GUIDCount -gt 100) {
Get-Item $GUIDFilePath | Remove-Item
New-Item -Path $FilePath -Name GUIDList.txt -ItemType file
}

Implement the script in your environment

In order to implement the script in your environment, you’d need to change a couple of parts in the script. Remember to remove the <> characters when you edit the script. Change the following variables value:

$SiteCode = “<your_Primary_Site_site_code>
$ComputerName = “<NetBIOSName>
$GUIDFilePath = “<path_to_a_text_file_called_eg_GUIDList.txt>
$FilePath = “<path_to_the_parent_folder_of_your_GUIDList.txt_file>

The row below also needs to be modified in order for it to be able to send emails correctly:

Send-MailMessage -SmtpServer “<smtp_server_FQDN>” -From “sender@domain.com” -To “receiver@domain.com” -Subject “New Application Request has been submitted” -Body $body

(1497)

Nickolaj Andersen

Principal Consultant and Enterprise Mobility MVP since 2016. Nickolaj has been in the IT industry for the past 10 years specializing in Enterprise Mobility and Security, Windows devices and deployments including automation. Currently working for TrueSec as a Principal Consultant. Awarded as PowerShell Hero in 2015 by the community for his script and tools contributions. Creator of ConfigMgr Prerequisites Tool, ConfigMgr OSD FrontEnd, ConfigMgr WebService to name a few. Frequent speaker at conferences and user groups.

comments
  • pieman
    Posted at 15:10 July 17, 2013
    pieman
    Reply
    Author

    Nice script, however it doesn’t show which attributes should be changed to work in the users own environment, you state that they should be in red but there not! would also be handy to explain what each attribute to be changed does/is there for.

    • Nickolaj
      Posted at 15:39 July 17, 2013
      Nickolaj
      Reply
      Author

      Hi Pieman,

      You’re totally right, I forgot to remove that part when I migrated all posts from the previous blog. The blog post has now been updated with the corrections that you asked for. Let me know if you still think it’s unclear.

      /Nickolaj

      • pieman
        Posted at 16:48 July 17, 2013
        pieman
        Reply
        Author

        excellent, works a dream, and negates the need to implement another application (MS SCSM or Cortech’s CTAA).
        Many thanks and keep up the good work!

        • Nickolaj
          Posted at 17:08 July 19, 2013
          Nickolaj
          Reply
          Author

          Great and thanks! I’m glad that you got it working. If you are looking into some self service portal, have a look at http://www.eupsco.com.I recently implemented it in production and its really great. The best thing is that its free and customizable.

          /Nickolaj

  • jrmendi
    Posted at 16:55 August 10, 2013
    jrmendi
    Reply
    Author

    The script works great, it emails me all requests; however, items are not appending to the txt file, therefor I am getting duplicate emails each time it runs.

    • Nickolaj
      Posted at 12:56 August 16, 2013
      Nickolaj
      Reply
      Author

      Hi Jrmendi,

      If you try this script, a modified version to only test the basic functionality:

      $SiteCode = “P01”
      $ComputerName = “

  • Jeff Martin
    Posted at 20:20 February 19, 2014
    Jeff Martin
    Reply
    Author

    I customized your script and set it to run via Task Scheduler but the task runs and never stops nor does it appear to be doing anything.

    • Nickolaj
      Posted at 20:40 February 19, 2014
      Nickolaj
      Reply
      Author

      Hi Jeff,

      Interesting, I’ve set it up a few times and it works flawlessly. Could you share a bit more information of you’ve set it up? What’s the action command in the task? Are you able to run the script from a console and get the desired results?

      Regards,
      Nickolaj

  • Armin Pusnik
    Posted at 15:04 May 7, 2014
    Armin Pusnik
    Reply
    Author

    Hi,

    got some troubles with that script. I think I’m just thinking to much ^^ I copied your script and saved it in a *.vbs is that right? When I try to run that script via cscript.exe *.vbs i get a VBScript compilation error.

    Any help would be appreciated.

    Kind regards,
    Armin

    • Nickolaj
      Posted at 15:44 May 7, 2014
      Nickolaj
      Reply
      Author

      Hi Armin,

      This script is a PowerShell script, not a VBScript. Open your favorite text editor or PowerShell ISE and copy the script into it, then save it as a PS1 file (PowerShell ISE will do that automatically).

      Regards,
      Nickolaj

  • Steve L
    Posted at 21:41 June 3, 2014
    Steve L
    Reply
    Author

    Nickolaj, So far this is working great. One question: is there a way to define a variable in order to add the user’s full name to the email message in addition to the account name?

    • Nickolaj
      Posted at 14:20 June 6, 2014
      Nickolaj
      Reply
      Author

      Hi Steve,

      Yes, of course there is. But then the account setup to run the task would also need to have atleast read access to AD. If you want to do this with the Active Directory cmdlets, you’d need to import that module at the top of the script. That module would also need to be ‘installed’ on the server where the script is running (I’d not recommend to run it on a domain controller).

      But instead of using the cmdlets, you could use ADSI. Here’s a sample script that I just tested:

      $User = “nickolaj”
      $ADSearch = New-Object DirectoryServices.DirectorySearcher
      $ADSearch.Filter = “(&(objectCategory=user)(samAccountName=$User))”
      $FullName = $ADSearch.FindOne().Properties.displayname

      The $FullName variable will hold the value of ‘displayname’. Bare in mind, the Properties seems to be case sensitive.

      Regards,
      Nickolaj

  • John
    Posted at 22:05 July 30, 2014
    John
    Reply
    Author

    The link describing how to setup the scheduled task is not working. Will you please provide this info, thanks? I assume I could just run with an SCCM Admin account but I wanted to know for sure.

  • Martin
    Posted at 10:25 October 15, 2014
    Martin
    Reply
    Author

    John;
    I guess the link broke when he moved the blog, here’s a correct link:
    http://www.scconfigmgr.com/2013/03/12/create-a-deployment-summary-html-report-with-powershell/

    • Nickolaj
      Posted at 11:07 October 15, 2014
      Nickolaj
      Reply
      Author

      Hi Martin and John,

      Sorry for not fixing this any sooner, but the post has been amended with the correct link now. And thanks to you Martin for pointing John in the right direction 🙂

      Regards,
      Nickolaj

  • seb
    Posted at 21:34 July 16, 2015
    seb
    Reply
    Author

    that’s beautiful! Elegant and simple solution, thank you so much for sharing your work!
    no issue at all implementing this, awesome!

  • Marwa
    Posted at 14:48 February 22, 2016
    Marwa
    Reply
    Author

    please i need to ask about the GUID file, is that file i create locally or where it is created ?

  • selim atmaca
    Posted at 12:26 May 10, 2016
    selim atmaca
    Reply
    Author

    Hi,
    Before creating a schedule task I wanted to make sure the script works, but it fails.

    GUIDList.txt file is located in C:\emailNotification (on site server)
    I set the variables as follows:
    $SiteCode = “ISU”
    $ComputerName = “”
    $GUIDFilePath = “C:\emailNotification\GUIDList.txt”
    $FilePath = “C:\emailNotification\”
    I also customized my email sender, smptserver, email receiver but I am having the error below when running the script manually.

    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    At C:\emailNotification\emailNotification.ps1:11 char:18
    + $GetAppRequest = Get-WmiObject -Class SMS_UserApplicationRequest -Namespace root …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

  • Remy
    Posted at 10:03 May 24, 2016
    Remy
    Reply
    Author

    I made a working powershell sxcript by combining 2

    This script includes user ad properties check for email, sur and givenname..

    #####################################################################
    ### E-Mail-Notification for Application-Request in ConfigMgr 2012 R2
    ### powered by Andre Picker – http://www.clientmgmt.de
    ### http://twitter.com/clientmgmt
    #####################################################################

    ### import ad module #################################################
    Import-Module activedirectory

    ### Set GUID textfile variables #################################################

    $GUIDFilePath = “D:\_Administration\Scripts\emailnotificationsccmapprequest\GUIDList.txt”
    $FilePath = “D:\_Administration\Scripts\emailnotificationsccmapprequest”

    ### E-Mail Settings #################################################

    $SmtpServer = “smtp.ru.nl”
    $SenderMail = “dte-isc@ru.nl”
    $TargetMail = “dte-isc@ru.nl”
    $Subject = “Application Catalog Request”
    $Message = “You have received a new Application Request from System Center Configuration Manager:`n”
    $Footer = “To process the request go to: \Software Library\Overview\Application Management\Approval Requests.`n`n*** This is an automatically generated email. Please do not reply to this message. ***”

    ### Queryinterval ####################################################

    $interval = $(Get-Date).AddMinutes(-60)

    ### Get SMS.Sitecode #################################################

    $smsproviderloc = “select * from sms_providerlocation”
    $sitecode = Get-WmiObject -Query $smsproviderloc -Namespace “root\sms” -ComputerName localhost
    $sitecode = $sitecode.sitecode

    ### Query ############################################################

    $GetGUID = Get-Content -Path $GUIDFilePath

    Get-WmiObject -Namespace “root\SMS\Site_$sitecode” -Class SMS_UserApplicationRequest | where {$_.CurrentState -match “1” -and [Management.ManagementDateTimeConverter]::ToDateTime($_.LastModifiedDate) -gt $interval} | Foreach-Object {
    if ($GetGUID -contains $_.RequestGuid) {
    Write-Host “Application request $($_.RequestGuid) already present”
    }
    else {
    $User = $_.User
    $Userv2 = $User.Trim(“RU”)
    $Userv3 = $Userv2.Trim(“\”)
    $Application = $_.Application
    $Comments = $_.Comments
    $Date = [Management.ManagementDateTimeConverter]::ToDateTime($_.LastModifiedDate)
    $ADsearch = Get-ADuser -Identity $Userv3 -Properties * | Select-Object GivenName, Surname, EmailAddress
    $Givenname = $ADsearch.GivenName
    $Surname = $ADsearch.Surname
    $MailAddress = $ADsearch.EmailAddress

    Write-host $Userv3 $Givenname

    Send-MailMessage -From $SenderMail -Subject “$Subject from $Givenname $Surname ” -To $TargetMail -Body “$Message`nRU-Account: $userv3`nName: $Givenname $Surname`nEmail: $MailAddress`nApplication: $Application `nDate: $Date `nComments: $Comments `n`n$Footer” -SmtpServer $SmtpServer
    $_.RequestGuid | Out-File $GUIDFilePath -Append
    }
    }

    #Remove the GUIDList.txt file and re-create it when there’s more than 100 entries
    $GUIDCount = $GetGUID.Count
    if ($GUIDCount -gt 100) {
    Get-Item $GUIDFilePath | Remove-Item
    New-Item -Path $FilePath -Name GUIDList.txt -ItemType file

    }

    • Bruce
      Posted at 21:04 January 27, 2017
      Bruce
      Reply
      Author

      Perfect. Thanks!

  • Dmitry
    Posted at 10:59 August 15, 2016
    Dmitry
    Reply
    Author

    I have a problem.
    This article script can not be executed because the mail server authentication is required.
    Proposed in the comments do not nichgeo performs error bunch.

  • Kenneth Jensen
    Posted at 11:14 March 6, 2017
    Kenneth Jensen
    Reply
    Author

    This script worked fine for me (SCCM CB and windows 2016 server).

    One thing: If a request is cancelled by the user, and then requested again, it will be seen as a duplicate request, and therefore not processed.

    I edited the script to handle this, by removing all requests that have denied, cancelled or installed status(2, 3 or 4 in CurrentState).

    The script is here:

    #Hard-coded variables
    $SiteCode = “site”
    $ComputerName = “computername”
    $GUIDFilePath = “somepath\somefile”
    $FilePath = “somepath”

    #Get the entries from GUIDList.txt, and cast to array type.
    $GetGUID = Get-Content -Path $GUIDFilePath
    $GetGUID = [System.Collections.ArrayList]$GetGUID

    #Get ALL Application Requests
    $GetAppRequest = Get-WmiObject -Class SMS_UserApplicationRequest -Namespace root/SMS/site_$SiteCode -ComputerName $ComputerName
    ForEach($Apprequest in $GetAppRequest)
    {
    if ($Apprequest.CurrentState -eq “1”) #Status is 1 = “Requested”:
    {
    if ($GetGUID -contains $Apprequest.RequestGuid)
    {
    Write-Host “Application request $($Apprequest.RequestGuid) already present”. #Do nothing
    }
    else
    {
    #If CurrentState = 1, and ID doesn’t exist in the file then send email, add to the file (array);
    $appUser = $Apprequest.User
    $appName = $Apprequest.Application
    $appComment = $Apprequest.Comments
    $Body = @”
    Application request: $appName
    User: $appUser
    Comment: $appComment
    “@ #This row can’t contain any blank spaces or tabs
    #Email configuration
    Send-MailMessage -SmtpServer “XXXXXXXX” -From “fromaddress” -To “toaddress” -Subject “Somes subject” -Body $body
    #Append the current objects GUID to GUIDList.txt
    $GetGUID.Add($Apprequest.RequestGuid)
    }

    }
    else #If status is everything BUT 1 (requested), and the ID exist in the array, then remove it for possible re-request:
    {
    if ($GetGUID -contains $Apprequest.RequestGuid)
    {
    Write-Host “Status is 2,3 eller 4, og Application request $($Apprequest.RequestGuid) exist in the file – delete the entry.”
    $GetGUID.Remove($Apprequest.RequestGuid)
    }
    }
    }

    #When done, write the array to the file:
    $GetGUID | Out-File $GUIDFilePath -Force

  • Kenneth Jensen
    Posted at 11:16 March 6, 2017
    Kenneth Jensen
    Reply
    Author

    The script/comment I just added.

    Can I ask you to remove the IP to the smtp server – just as a principle 🙂

    Rgs
    Kenneth

    • Nickolaj
      Posted at 13:58 March 6, 2017
      Nickolaj
      Reply
      Author

      Done 😉

    • Bruce
      Posted at 14:45 March 6, 2017
      Bruce
      Reply
      Author

      Hi Kenneth,

      I’ve been using the modifyed version of Nickolaj’s script posted by Remi and I get all email notifications evenusers re-request an application after they cancel or if I cancel in the console. I will double check this again and post.

  • Gary
    Posted at 18:27 April 4, 2017
    Gary
    Reply
    Author

    Before I test this I cannot determine if the GUIDList.txt file is something that must be created manually before you run this or something that gets generated by running the script. It appears that you need to manually create the text file but with what type of entries?

    • Kenneth Jensen
      Posted at 07:20 April 5, 2017
      Kenneth Jensen
      Reply
      Author

      @Gary: The guidlist is generated by the script when it finds an app. request

  • Leave a Reply