forked from realmjoin/RealmJoin.RunbookHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logging.ps1
57 lines (48 loc) · 1.43 KB
/
Logging.ps1
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
function Write-RjRbLog {
[CmdletBinding()]
param (
[string] $Message,
[object] $Data,
[switch] $NoDebugOnly
)
if ($NoDebugOnly -and ($DebugPreference -ne [Management.Automation.ActionPreference]::SilentlyContinue)) {
return
}
Write-RjRbLogImpl @PSBoundParameters
}
function Write-RjRbDebug {
[CmdletBinding()]
param (
[string] $Message,
[object] $Data
)
if ($DebugPreference -eq [Management.Automation.ActionPreference]::SilentlyContinue) {
return
}
Write-RjRbLogImpl @PSBoundParameters
}
function Write-RjRbLogImpl {
[CmdletBinding()]
param (
[string] $Message,
[object] $Data,
[switch] $NoDebugOnly # dummy only
)
if ($null -ne $Data) {
if ($Message) {
$Message += ": "
}
if ($Data -is [Hashtable] -and $Data.Count -eq 1) {
$Message += "$($Data.Keys[0]): $($Data.Values[0] | ConvertTo-Json -Depth 20)"
}
else {
$Message += ($Data | ConvertTo-Json -Depth 20)
}
}
$callingFunction = [string]$(Get-PSCallStack)[2].FunctionName
if ($callingFunction -ine "<ScriptBlock>") {
$Message = "$callingFunction`: $Message"
}
# use -Verbose to even write message to verbose stream in case preference is off
Write-Verbose -Verbose -Message $Message
}