PSvLIMessage.psm1

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
98
99
100
101
102
103
104
105
106
<#
    .SYNOPSIS
    Push Messages to VMware vRealize Log Insight.
   
    .DESCRIPTION
    Push Messages to VMware vRealize Log Insight.
 
    .EXAMPLE
    Push-vLIMessage -vLIServer "loginsight.lan.local" -vLIAgentID "12862842-5A6D-679C-0E38-0E2BE888BB28" -Text "My Test"
     
    .EXAMPLE
    Push-vLIMessage -vLIServer "loginsight.lan.local" -vLIAgentID "12862842-5A6D-679C-0E38-0E2BE888BB28" -Text "My Test" -Hostname MyTEST -FieldName myTest -FieldContent myTest
     
    .PARAMETER vLIServer
    Specify the vLI FQDN
 
    .PARAMETER vLIAgentID
    Specify the vLI Agent ID
 
    .PARAMETER Text
    Specify the Event Text
 
    .PARAMETER Hostname
    Specify the Hostanme displayed in vLI
 
    .PARAMETER FieldName
    Specify the a Optional Field Name for vLI
     
    .PARAMETER FieldContent
    Specify the a Optional FieldContent for the Field in -FieldName for vLI
    If FielName is missing and FieldContent is given, it will be ignored
     
 #Requires PS -Version 4.0
 #>

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

function Push-vLIMessage {

    [cmdletbinding()]
    param (
        [parameter(Mandatory=$true, Position=0)]
            [string]$vLIServer,
        [parameter(Mandatory=$true, Position=1)]
            [string]$vLIAgentID,
        [parameter(Mandatory=$true, Position=2)]
            [string]$Text,
        [parameter(Mandatory=$false, Position=3)]
            [string]$Hostname = $env:computername,
        [parameter(Mandatory=$false, Position=4)]
            [string]$FieldName,
        [parameter(Mandatory=$false, Position=5)]
            [string]$FieldContent = ""
    )
    Process {
        $Field_vLI = [ordered]@{
                        name = "PS_vLIMessage"
                        content = "true"
                        }
        $Field_HostName = [ordered]@{
                        name = "hostname"
                        content = $Hostname
                        }
                    
        $Fields = @($Field_vLI, $Field_HostName)
        
        if ($FieldName) {
            $Field_Custom = [ordered]@{
                    name = $FieldName
                    content = $FieldContent
                    }
            $Fields += @($Field_Custom)
            }
            
        $Restcall = @{
                    messages =    ([Object[]]([ordered]@{
                            text = ($Text)
                            fields = ([Object[]]$Fields)
                            }))
                    } | convertto-json -Depth 4
    
        $Resturl = ("http://" + $vLIServer + ":9000/api/v1/messages/ingest/" + $vLIAgentID)
        try
        {
            $Response = Invoke-RestMethod $Resturl -Method Post -Body $Restcall -ContentType 'application/json' -ErrorAction stop
            Write-Information "REST Call to Log Insight server successfull"
            Write-Verbose $Response
        }
        catch
        {
            Write-Error "REST Call failed to Log Insight server"
            Write-Verbose $error[0]
            Write-Verbose $Resturl
        }
    }
}