
Option Explicit

Dim fso, shell, psFile, ps, cmd, file

Set fso = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")

psFile = fso.BuildPath( _
    shell.ExpandEnvironmentStrings("%TEMP%"), _
    "custom_notification.ps1")

ps = ""

' Load WPF
ps = ps & "Add-Type -AssemblyName PresentationFramework" & vbCrLf
ps = ps & "Add-Type -AssemblyName PresentationCore" & vbCrLf

' Create popup layout
ps = ps & "$xaml = @'" & vbCrLf

ps = ps & "<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " & _
    "WindowStyle='None' AllowsTransparency='True' Background='Transparent' " & _
    "Width='455' Height='225' WindowStartupLocation='CenterScreen' " & _
    "Topmost='True'>" & vbCrLf

ps = ps & "<Border Background='#292929' BorderBrush='#555555' " & _
    "BorderThickness='1' CornerRadius='7'>" & vbCrLf

ps = ps & "<Grid>" & vbCrLf

ps = ps & "<Grid.RowDefinitions>" & vbCrLf
ps = ps & "<RowDefinition Height='32'/>" & vbCrLf
ps = ps & "<RowDefinition Height='*'/>" & vbCrLf
ps = ps & "<RowDefinition Height='60'/>" & vbCrLf
ps = ps & "</Grid.RowDefinitions>" & vbCrLf

' Blue title bar
ps = ps & "<Border Background='#0879BD' CornerRadius='6,6,0,0'>" & vbCrLf
ps = ps & "<TextBlock Text='Windows Security' Foreground='White' " & _
    "FontSize='12' VerticalAlignment='Center' Margin='12,0'/>" & vbCrLf
ps = ps & "</Border>" & vbCrLf

' Main message
ps = ps & "<StackPanel Grid.Row='1' Margin='20,20,20,0'>" & vbCrLf
ps = ps & "<TextBlock Text='Security Update Available' FontSize='17' " & _
    "FontWeight='Bold' Foreground='White'/>" & vbCrLf
ps = ps & "<TextBlock Text='Connect your phone via Phone Link for security notifications.' " & _
    "FontSize='12' Foreground='#DDDDDD' " & _
    "Margin='0,15,0,0'/>" & vbCrLf
ps = ps & "</StackPanel>" & vbCrLf

' Button layout
ps = ps & "<Grid Grid.Row='2' Margin='16,8,16,15'>" & vbCrLf
ps = ps & "<Grid.ColumnDefinitions>" & vbCrLf
ps = ps & "<ColumnDefinition Width='*'/>" & vbCrLf
ps = ps & "<ColumnDefinition Width='8'/>" & vbCrLf
ps = ps & "<ColumnDefinition Width='*'/>" & vbCrLf
ps = ps & "</Grid.ColumnDefinitions>" & vbCrLf

' OK button
ps = ps & "<Button Name='OKButton' Grid.Column='0' Content='OK' " & _
    "Background='#42B9F5' Foreground='Black' " & _
    "BorderBrush='#99DDFF' Cursor='Hand'/>" & vbCrLf

' Close button
ps = ps & "<Button Name='CloseButton' Grid.Column='2' Content='Close' " & _
    "Background='#383838' Foreground='White' " & _
    "BorderBrush='#444444' Cursor='Hand'/>" & vbCrLf

ps = ps & "</Grid></Grid></Border></Window>" & vbCrLf

ps = ps & "'@" & vbCrLf

' Popup behavior
ps = ps & "try {" & vbCrLf

ps = ps & "  $window = [Windows.Markup.XamlReader]::Parse($xaml)" & vbCrLf

' Allow Escape key to close popup
ps = ps & "  $window.Add_PreviewKeyDown({" & vbCrLf
ps = ps & "    param($sender, $e)" & vbCrLf
ps = ps & "    if ($e.Key -eq 'Escape') {" & vbCrLf
ps = ps & "      $window.Close()" & vbCrLf
ps = ps & "    }" & vbCrLf
ps = ps & "  })" & vbCrLf

' Play notification sound when shown
ps = ps & "  $window.Add_ContentRendered({" & vbCrLf
ps = ps & "    [System.Media.SystemSounds]::Asterisk.Play()" & vbCrLf
ps = ps & "  })" & vbCrLf

' OK opens Phone Link
ps = ps & "  $window.FindName('OKButton').Add_Click({" & vbCrLf
ps = ps & "    try {" & vbCrLf
ps = ps & "      Start-Process 'ms-phone:' -ErrorAction Stop" & vbCrLf
ps = ps & "      $window.Close()" & vbCrLf
ps = ps & "    } catch {" & vbCrLf
ps = ps & "      [System.Windows.MessageBox]::Show('Unable to open Phone Link. Please check that it is installed.')" & vbCrLf
ps = ps & "    }" & vbCrLf
ps = ps & "  })" & vbCrLf

' Close dismisses popup
' Close button plays a sound but does not close popup
ps = ps & "  $window.FindName('CloseButton').Add_Click({" & vbCrLf
ps = ps & "    [System.Media.SystemSounds]::Hand.Play()" & vbCrLf
ps = ps & "  })" & vbCrLf


ps = ps & "  $null = $window.ShowDialog()" & vbCrLf

ps = ps & "} catch {" & vbCrLf
ps = ps & "  Add-Type -AssemblyName System.Windows.Forms" & vbCrLf
ps = ps & "  [System.Windows.Forms.MessageBox]::Show($_.Exception.Message, 'Popup Error')" & vbCrLf
ps = ps & "}" & vbCrLf

' Write temporary PowerShell file
Set file = fso.CreateTextFile(psFile, True, False)
file.Write ps
file.Close

' Run without displaying a terminal
cmd = "powershell.exe -NoProfile -STA -WindowStyle Hidden -File """ & psFile & """"

shell.Run cmd, 0, True

' Clean up temporary file
If fso.FileExists(psFile) Then
    fso.DeleteFile psFile, True
End If

Set shell = Nothing
Set fso = Nothing
