Sample/LogExceptions.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
<#
 # Demonstration of the Exeption Logging capabilities of Application Insights
#>


#Import the module
Import-Module .\PSAppInsightsFix.psd1 -Force 
$key = "b437832d-a6b3-4bb4-b237-51308509747d"


#init a client and send basic non-PII information for correlation
#this includes identifiers hashed(username) and hashed(machine name)
$Client = New-AIClient -Key $key -Verbose -Fiddler -Initializer Dependency






#Create an Exception and error to log
    $exception = new-object System.Management.Automation.ApplicationFailedException
    try  
    {  
     $fileContent = Get-Content -Path "C:\Does.not.exists.txt" -ErrorAction Stop  
    }  
    catch  
    {  
     $MyError = $_     
        $MyExeption = $_.Exception

        Send-AIException -Error $MyError
        Send-AIException -Exception $MyExeption
    }