TechEd Australia 2013 - PowerShell for ConfigMgr 2012 SP1 - Demo 5

 Demo 5: App Approvals

Script for System Tray notification, pop-up form and Approve/Deny an Application Approval Request

Add-Type -AssemblyName System.Windows.Forms
Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
Set-Location PRI:\
$ApprovalRequests = Get-CMApprovalRequest | Where{$_.CurrentState -eq 1}
ForEach ($Approval in $ApprovalRequests)
{
function Popup-Form
{
param ($form)

$form.ShowDialog()
}
$RequestUser = $Approval.User.TrimStart("CONTOSO\")
$RequestApp = $Approval.Application

$container = New-Object System.ComponentModel.Container
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon($container)
$notifyIcon.Icon = New-Object System.Drawing.Icon("C:\Scripts\Demo 5\tick.ico")
$notifyIcon.Text = "New App Approval Request Available"
$notifyIcon.Visible = $true
$notifyIcon.BalloonTipText = "New App Approval Request Available"

$formImage = [System.Drawing.Image]::FromFile("C:\Scripts\Demo 5\background.jpg")
$form = New-Object System.Windows.Forms.Form
$form.Add_Shown({$form.Activate()})
$form.Size = New-Object System.Drawing.Point(325,200)
$form.StartPosition = "Manual"
$form.Text = "New App Approval Request"
$form.BackgroundImage = $formImage
$screenBounds = [system.Windows.Forms.Screen]::Getworkingarea(0)
$form.Location = New-Object System.Drawing.Point( (($screenBounds.right) - ($form.Width)),(($screenBounds.Bottom) - ($form.height)) )
$form.FormBorderStyle = "fixedDialog"

$label = New-Object System.Windows.Forms.Label
$label.Text = "$RequestUser has requested the $RequestApp application"
$label.Location = New-Object System.Drawing.Point(10,20)
$label.MaximumSize = New-Object System.Drawing.Size(300,100)
$label.Font = New-Object System.Drawing.Font("Segoe UI",11,[system.drawing.fontstyle]::regular)
$label.Autosize = $true
$label.BackColor = "Transparent"
$form.Controls.Add($label)

$buttonApprove = New-Object System.Windows.Forms.Button
$buttonApprove.Text = "Approve"
$buttonApprove.Size = New-Object System.Drawing.Size(120,50)
$buttonApprove.Location = New-Object System.Drawing.Point(35,110)

$buttonApprove.Add_Click(
{
# Approve Request
Set-Location PRI:\
Approve-CMApprovalRequest -Id $Approval.CI_UniqueID -Comment "Approved via PowerShell Form"
$form.close()
$notifyIcon.Visible = $false
New-Event ClickComplete
})
$form.Controls.Add($buttonApprove)

$buttonDeny = New-Object System.Windows.Forms.Button
$buttonDeny.Text = "Deny"
$buttonDeny.Size = New-Object System.Drawing.Size(120,50)
$buttonDeny.Location = New-Object System.Drawing.Point(165,110)

$buttonDeny.Add_Click(
{
# Deny Request
Set-Location PRI:\
Deny-CMApprovalRequest -Id $Approval.CI_UniqueID -Comment "Denied via PowerShell Form"
$form.close()
$notifyIcon.Visible = $false
New-Event ClickComplete
})
$form.Controls.Add($buttonDeny)

$notifyIcon.ShowBalloonTip(3)
Register-ObjectEvent -InputObject $notifyIcon -EventName BalloontipClicked -Action {Popup-Form -form $form} | Out-Null
Register-ObjectEvent -InputObject $notifyIcon -EventName MouseClick -Action {Popup-Form -form $form} | Out-Null
Wait-Event -SourceIdentifier ClickComplete | Out-Null
Remove-Event -SourceIdentifier ClickComplete | Out-Null
}

VBScript to launch the PowerShell script silently

command = "%SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe -NoLogo -WindowStyle Hidden -File C:\Scripts\Sched\Demo5_Complete.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,2

 

Files required to run the script

background.jpg (rename to background.jpg)

tick.ico (rename to tick.ico)