-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSend-GoogleChat.ps1
More file actions
97 lines (85 loc) · 4.51 KB
/
Copy pathSend-GoogleChat.ps1
File metadata and controls
97 lines (85 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
param(
# Parameters are now optional, logic below will decide which to use
[string]$LogFilePath,
[string]$Message,
[string]$Uri = 'https://chat.googleapis.com/v1/spaces/AAAACC1obHI/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=_laQ0zzNJDo60PMKckZY4z-IYz0nLIj4TjJI0qONNyY'
)
# --- Debugging Start ---
# Define log file path FIRST
$scriptErrorLogDir = Join-Path $env:TEMP 'UpdateLoxone'
# $LogFilePath is left exactly as the caller passed it - INCLUDING empty.
#
# It used to be overwritten unconditionally with an absolute path to one developer's machine
# and one 2025-04-14 log file, so the parameter was dead and the script only ever read that
# file - on any other machine, nothing at all.
#
# Defaulting it to a path under TEMP does not fix that, it re-creates it: the dispatch below
# takes the log-file branch whenever $LogFilePath is non-empty, so ANY default makes
# -Message unreachable and turns `-Message "hello"` into "Error: Log file not found".
# Emptiness is what selects the -Message branch, so emptiness has to survive.
# Ensure directory exists
if (-not (Test-Path $scriptErrorLogDir)) { New-Item -Path $scriptErrorLogDir -ItemType Directory -Force | Out-Null }
# Log received parameters and set name
$debugInfo = @"
Timestamp: $(Get-Date)
PSBoundParameters: $($PSBoundParameters | Out-String)
ParameterSetName: $($PSCmdlet.ParameterSetName)
---
"@
# Add-Content -Path $scriptErrorLogPath -Value $debugInfo # Keep debug logging commented out for now
# --- Debugging End ---
# Define log file for errors from this script itself (path defined above)
#$scriptErrorLogDir = "C:\temp"
#$LogFilePath = Join-Path $scriptErrorLogDir "toast_chat_error.log"
try {
$messageToSend = ''
# Determine message based on provided parameters
if (-not [string]::IsNullOrWhiteSpace($LogFilePath)) {
# LogFilePath was provided, try to use it
if (Test-Path -Path $LogFilePath -PathType Leaf) {
try {
# Read all bytes from the log file
$fileBytes = [System.IO.File]::ReadAllBytes($LogFilePath)
# Compress the bytes using GZip
$memoryStream = [System.IO.MemoryStream]::new()
$gzipStream = [System.IO.Compression.GZipStream]::new($memoryStream, [System.IO.Compression.CompressionMode]::Compress)
$gzipStream.Write($fileBytes, 0, $fileBytes.Length)
$gzipStream.Close() # Important: Close the GZipStream to flush data
$compressedBytes = $memoryStream.ToArray()
$memoryStream.Close()
# Convert compressed bytes to Base64 string
$base64String = [System.Convert]::ToBase64String($compressedBytes)
$messageToSend = "GZIP+Base64:" + $base64String # Prefix to indicate format
} catch {
$messageToSend = "Error reading/compressing log file '$LogFilePath': $($_.Exception.Message)"
}
} else {
$messageToSend = "Error: Log file not found at '$LogFilePath'"
}
} elseif (-not [string]::IsNullOrWhiteSpace($Message)) {
# LogFilePath was NOT provided, but Message was
$messageToSend = $Message
} else {
# Neither parameter was provided with a value
$messageToSend = "Error: No LogFilePath or Message provided to Send-GoogleChat.ps1"
}
# Ensure message isn't empty
if ([string]::IsNullOrWhiteSpace($messageToSend)) {
$messageToSend = "(Empty log file or message)"
}
$body = @{
text = $messageToSend
} | ConvertTo-Json -Compress
# Use -UseBasicParsing to avoid potential IE engine dependency issues
Invoke-RestMethod -Uri $Uri -Method Post -Body $body -ContentType 'application/json; charset=UTF-8' -UseBasicParsing -ErrorAction Stop
# Optional success logging
# if (-not (Test-Path $scriptErrorLogDir)) { New-Item -Path $scriptErrorLogDir -ItemType Directory -Force | Out-Null }
# Add-Content -Path $scriptErrorLogPath -Value "Successfully sent message at $(Get-Date)"
} catch {
# Log the error for debugging purposes
$errorMessage = "Error sending to Google Chat at $(Get-Date): $($_.Exception.ToString())"
# Ensure the directory exists before writing the log
if (-not (Test-Path $scriptErrorLogDir)) { New-Item -Path $scriptErrorLogDir -ItemType Directory -Force | Out-Null }
Add-Content -Path $scriptErrorLogPath -Value $errorMessage
# throw $_ # Optional: re-throw if needed
}