1- <#
1+ <#
22. SYNOPSIS
33 Post-logon script to display a standard message via an HTA file.
44
1212 Luiz Hamilton Silva - @brazilianscriptguy (adapted & generalized)
1313
1414.VERSION
15- Last Updated: 2025-10-02
15+ Last Updated: 2025-01-23
1616#>
1717
1818param (
1919 [string ]$messagePath = " \\forest-logonserver\NETLOGON\broadcast-logonmessage\Broadcast-UserLogonMessageViaGPO.hta"
2020)
2121
22- # Fail quietly by default (caller environment controlled)
2322$ErrorActionPreference = " SilentlyContinue"
2423
2524# --- Hide the console window ---
@@ -31,62 +30,41 @@ public class Win32 {
3130 [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
3231}
3332"@
34- try {
35- $consolePtr = [Win32 ]::GetConsoleWindow()
36- # 0 = SW_HIDE
37- [Win32 ]::ShowWindow($consolePtr , 0 ) | Out-Null
38- } catch {
39- # If hiding fails, continue but log the condition below (if logging is available)
40- }
41- # ---------------------------------
33+ $consolePtr = [Win32 ]::GetConsoleWindow()
34+ [Win32 ]::ShowWindow($consolePtr , 0 ) | Out-Null
35+ # --------------------------------
4236
43- # Prepare logging
44- $scriptName = [System.IO.Path ]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name )
45- $logDir = " C:\Scripts-LOGS"
37+ # Logging configuration
38+ $scriptName = [System.IO.Path ]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name )
39+ $logDir = " C:\Scripts-LOGS"
4640$logFileName = " ${scriptName} .log"
47- $logPath = Join-Path $logDir $logFileName
41+ $logPath = Join-Path $logDir $logFileName
4842
4943function Log-Message {
5044 param (
51- [Parameter ( Mandatory = $true )][ string ]$Message ,
45+ [string ]$Message ,
5246 [string ]$Severity = " INFO"
5347 )
54- try {
55- if (-not (Test-Path $logDir )) {
56- New-Item - Path $logDir - ItemType Directory - Force | Out-Null
57- }
58- $timestamp = Get-Date - Format " yyyy-MM-dd HH:mm:ss"
59- $entry = " [$timestamp ] [$Severity ] $Message "
60- Add-Content - Path $logPath - Value $entry - ErrorAction Stop
61- } catch {
62- # If logging fails, we cannot do much in a hidden logon script; swallow to avoid blocking logon
63- }
48+ $timestamp = Get-Date - Format " yyyy-MM-dd HH:mm:ss"
49+ Add-Content - Path $logPath - Value " [$timestamp ] [$Severity ] $Message "
6450}
6551
66- # Start
67- Log - Message " Script started. Target HTA: $messagePath "
52+ # Ensure log directory exists
53+ if ( -not ( Test-Path $logDir )) { New-Item - Path $logDir - ItemType Directory - Force | Out-Null }
6854
69- # Validate the HTA path and run it via mshta.exe
70- if (Test-Path $messagePath - PathType Leaf ) {
71- Log- Message " HTA file found at : $messagePath "
55+ # Execute .hta with mshta.exe
56+ if (Test-Path $messagePath ) {
57+ Log- Message " File found: $messagePath "
7258 try {
73- $mshta = Join-Path $env: windir " System32\mshta.exe"
74- if (-not (Test-Path $mshta )) {
75- Log- Message " mshta.exe not found at expected location: $mshta " " ERROR"
76- } else {
77- # Use Start-Process to launch mshta.exe with the HTA file; run hidden
78- $args = " `" $messagePath `" "
79- Start-Process - FilePath $mshta - ArgumentList $args - WindowStyle Hidden - NoNewWindow
80- Log- Message " mshta.exe launched (hidden) for: $messagePath "
81- }
59+ Start-Process - FilePath " $env: windir \System32\mshta.exe" - ArgumentList " `" $messagePath `" " - WindowStyle Hidden
60+ Log- Message " HTA execution triggered."
8261 } catch {
83- Log- Message " Error launching mshta.exe for HTA: $_ " " ERROR"
62+ Log- Message " Error executing HTA: $_ " " ERROR"
8463 }
8564} else {
86- Log- Message " HTA file not found at : $messagePath " " ERROR"
65+ Log- Message " File not found: $messagePath " " ERROR"
8766}
8867
89- Log- Message " Script finished."
9068exit 0
9169
92- # End of script
70+ # --- End of script ---
0 commit comments