-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogtesting.ps1
More file actions
65 lines (63 loc) · 2.12 KB
/
logtesting.ps1
File metadata and controls
65 lines (63 loc) · 2.12 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
$appInsightsKey = "27bc62d8-b4c5-4e00-b7cf-404b82f7affc"
function write-logs
{
param
(
[Parameter(Mandatory)]
[string]$AIKey,
[Parameter(Mandatory,ValueFromPipeline)]
$inputobject,
[switch]$Print
)
$scriptpath = (split-path $SCRIPT:MyInvocation.MyCommand.Path -parent)
$ai = "$scriptpath\Microsoft.ApplicationInsights.dll"
[Reflection.Assembly]::LoadFile($ai) | Out-Null
$telclient = New-Object "Microsoft.ApplicationInsights.TelemetryClient"
$telclient.InstrumentationKey = $AIKey
$type = (Get-Member -InputObject $inputobject).TypeName[0]
if(($type) -like "System.Management.Automation.error*")
{
$TelException = New-Object "Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry"
$TelException.Exception = $_.Exception
$TelClient.TrackException($TelException)
$TelClient.Flush()
if($Print){$inputobject}
}
elseif(($type) -like "System.Management.Automation.info*")
{
$telclient.TrackEvent("[$(([datetime]::now).ToLongTimeString())]" + $inputobject.Messagedata + ", in script:" + $inputobject.Source)
$telclient.Flush()
if($Print){$inputobject}
}
elseif(($type) -like "System.Management.Automation.*")
{
#$inputobject
$telclient.TrackEvent("[$(([datetime]::now).ToLongTimeString())]" + $inputobject.Message + ", in script:" + $inputobject.InvocationInfo.ScriptName)
$telclient.Flush()
if($Print){$inputobject}
}
else
{
$inputobject
}
}
function Write-ToStreams
{
[cmdletbinding()]
Param()
Begin
{
$VerbosePreference = 'Continue'
$DebugPreference = 'Continue'
}
Process
{
Write-Host "This is written to host" -ForegroundColor Green
Write-Output "This is written to Success output"
Write-Error "This is an error"
Write-Warning "This is a warning message"
Write-Verbose "This is verbose output"
Write-Debug "This is a debug message"
}
}
Write-ToStreams *>&1 | ForEach-Object{write-logs -inputobject $_ -AIKey $appInsightsKey -Print}