Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Powershell Using vRealize Log Insight Rest API

I had an assignment to automate vRLI (VMware vRealize Log Insight) using Powershell as well as Ansible. Here in this article I am showing how to use Microsoft Powershell to automate vRLI configurations. The best part about vRLI is, it provides Rest API which you can easily consume with any scripting languages. You can find the complete Rest API list and guide from below list.

Rest Api guides 
https://yourlogInsightServer/rest-api
Internal guide --> https://yourlogInsightServer/internal/rest-api
Online --> https://vmw-loginsight.github.io/#Getting-started-with-the-Log-Insight-REST-API

Here I have written a PowerShell module to login into vRLI, Get the information and change the configuration. All the tasks are done consuming Rest API only.

  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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
function Login-LogInsightAPI
{
    #requires -version 4
    <#
    .SYNOPSIS
        Log In to Log Insight Server Rest API
    .DESCRIPTION
        The Login-LogInsightAPI function login into vRealize Log insight and provide session details bearer token)for authorization. You need to run login script once, and keep using the existing session in another function cmdlet.
    .PARAMETER Server
        FQDN or IP of vRealize Log Insight server.
    .PARAMETER Username
        Username to login vRealize Log Insight server FQDN or IP.
    .PARAMETER Password
        Password to login vRealize Log Insight server FQDN or IP.
    .PARAMETER RestAPI
        This is Rest API string for vRealize Log Insight, The string after https://loginsightserver:9543/api/v1. Check more information on #"https://$logInsightServer/rest-api#Getting-started-with-the-Log-Insight-REST-API"
    .INPUTS
        string
    .OUTPUTS
        Microsoft.PowerShell.Commands.HtmlWebResponseObject
    .NOTES
    Version:        1.0
    Author:         Kunal Udapi
    Creation Date:  19 July 2020
    Purpose/Change: Login to vRealize Log Insight Rest API using PowerShell
    Useful URLs: http://vcloud-lab.com
    Tested on below versions:
        VMware vRealize Log Insight: 8.0
        Microsoft PowerShell: 5.1
        Operating System: Microsoft Windows 10
    .EXAMPLE
        Login-LogInsight -Server loginsightserver -Username admin -Password P@ssw0rd -RestAPI sessions

        StatusCode        : 200
        StatusDescription : OK
        Content           : {"userId":"ff0205c6-3875-4710-80cc-15100272a8db","sessionId":"EL2TC24NWRgibmzCwI4I1+MYEsP4YwiTlQ0OsDbbdBO/fT/gHMWD5q4Lm7M1BVpWEEZjVLLmI6YLbDyQZg4mWBkm+JPjZASo1y4FB1aotbpnN7bhfmIan8uVEyDBLqOPQOwUxRN0tV...
        RawContent        : HTTP/1.1 200 OK
                            ACCESS-CONTROL-EXPOSE-HEADERS: X-LI-Build
                            X-LI-Build: 16281169
                            Content-Length: 355
                            Content-Type: application/json; charset=utf-8
                            Date: Sun, 19 Jul 2020 05:31:45 GMT

                            {"userId":"...
        Forms             : {}
        Headers           : {[ACCESS-CONTROL-EXPOSE-HEADERS, X-LI-Build], [X-LI-Build, 16281169], [Content-Length, 355], [Content-Type, application/json; charset=utf-8]...}
        Images            : {}
        InputFields       : {}
        Links             : {}
        ParsedHtml        : mshtml.HTMLDocumentClass
        RawContentLength  : 355

        This script shows information as above information.
    #>
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, HelpMessage = 'Type LogInsight FQDN/IP')]
        [string]$Server = 'LogInsight.vcloud-lab.com',
        [Parameter(Mandatory = $true, HelpMessage = 'Type UserName')]
        [string]$Username = 'admin',
        [Parameter(Mandatory = $true, HelpMessage = 'Type Password')]
        [string]$Password = 'P@ssw0rd',
        [Parameter(Mandatory = $true, HelpMessage = 'Type Rest API node')]
        [string]$RestAPI = 'sessions' #Type string after the /api/v1/
    )
    begin
    {
        Approve-SelfSignedCertificate
    }
    process 
    {
        #login Example using cURL
        #curl -k -X POST https://192.168.34.15:9543/api/v1/sessions -d '{"username":"admin","password":"p@ssw0rd","provider":"Local"}' --noproxy '*'
        $uri = "https://$($Server):9543/api/v1/$RestAPI" #$RestAPI is string after /api/v1/
        $body = @{"username"=$Username;"password"=$Password;"provider"="Local"}
        try 
        {
            $session = Invoke-WebRequest -Uri $uri -Method POST -Body $($body | ConvertTo-Json) -ContentType 'application/json' -ErrorAction Stop
            $session
            Write-Host 'Login into Rest API successful' -BackgroundColor DarkGreen
        }
        catch 
        {
            Write-Host $error[0].Exception.Message -BackgroundColor DarkRed
        }
    }
    end {}
}

function Get-LogInsightAPI
{
    #requires -version 4
    <#
    .SYNOPSIS
        Get information from Log Insight Server Rest API using verb Get.
    .DESCRIPTION
        The Get-LogInsightAPI function login into vRealize Log insight and gets the information requested.
    .PARAMETER Server
        FQDN or IP of vRealize Log Insight server.
    .PARAMETER Session
        To get this information you will need to run Login-LogInsightAPI. You feed Microsoft.PowerShell.Commands.HtmlWebResponseObject information here.
    .PARAMETER RestAPI
        This is Rest API string for vRealize Log Insight, The string after https://loginsightserver:9543/api/v1. Check more information on #"https://$logInsightServer/rest-api#Getting-started-with-the-Log-Insight-REST-API"
    .INPUTS
        string
        Microsoft.PowerShell.Commands.HtmlWebResponseObject
    .OUTPUTS
        Microsoft.PowerShell.Commands.HtmlWebResponseObject
    .NOTES
    Version:        1.0
    Author:         Kunal Udapi
    Creation Date:  19 July 2020
    Purpose/Change: Get the configuration/Information from vRealize Log Insight Rest API using PowerShell
    Useful URLs: http://vcloud-lab.com
    Tested on below versions:
        VMware vRealize Log Insight: 8.0
        Microsoft PowerShell: 5.1
        Operating System: Microsoft Windows 10
    .EXAMPLE
        $session = Login-LogInsight -Server loginsightserver -Username admin -Password P@ssw0rd -RestAPI sessions
        Get-LogInsightAPI -Server loginsightserver -Session $session -RestAPI 'time/config'

        Content           : {}
        StatusCode        : 200
        StatusDescription : OK
        RawContentStream  : Microsoft.PowerShell.Commands.WebResponseContentMemoryStream
        RawContentLength  : 0
        RawContent        : HTTP/1.1 200 OK
                            X-LI-Build: 16281169
                            ACCESS-CONTROL-EXPOSE-HEADERS: X-LI-Build
                            Content-Length: 0
                            Date: Sun, 19 Jul 2020 05:44:37 GMT


        BaseResponse      : System.Net.HttpWebResponse
        Headers           : {[X-LI-Build, 16281169], [ACCESS-CONTROL-EXPOSE-HEADERS, X-LI-Build], [Content-Length, 0], [Date, Sun, 19 Jul 2020 05:44:37 GMT]}

        Run the Login-LogInsight script and use the information as Session parameter, This function script shows information for NTP Server configuration.
    #>
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, HelpMessage = 'Type LogInsight FQDN/IP')]
        [string]$Server = 'LogInsight.vcloud-lab.com',        
        [Parameter(Mandatory = $true, HelpMessage = 'Type LogInsight FQDN/IP')]
        [Microsoft.PowerShell.Commands.HtmlWebResponseObject]$Session,
        [Parameter(Mandatory = $true, HelpMessage = 'Type Rest API node')]
        [string]$RestAPI = 'licenses' #Type string after the /api/v1/
    )
    begin
    {
        Approve-SelfSignedCertificate
    }
    process 
    {
        try 
        {
            $sessionID = $Session.Content | ConvertFrom-Json | Select-Object -ExpandProperty sessionId
            $headers = @{'Authorization' = "Bearer $sessionID"}
            $getInfo = Invoke-WebRequest -Uri "https://$($server):9543/api/v1/$RestAPI" -Method Get -Headers $headers -ContentType 'application/json' -ErrorAction Stop
            $getInfo
            Write-Host "Getting $RestAPI Information" -BackgroundColor DarkGreen
        }
        catch 
        {
            Write-Host $error[0].Exception.Message -BackgroundColor DarkRed
        }

    }
    end {}
}

function Put-LogInsightAPI
{
    #requires -version 4
    <#
    .SYNOPSIS
        Modify information on the Log Insight Server Rest API using verb Put.
    .DESCRIPTION
        The Put-LogInsightAPI function login into vRealize Log insight and gets the information requested.
    .PARAMETER Server
        FQDN or IP of vRealize Log Insight server.
    .PARAMETER Session
        To get this information you will need to run Login-LogInsightAPI. It requires Microsoft.PowerShell.Commands.HtmlWebResponseObject .net object information.
    .PARAMETER Body
        In this parameter you need to provide information in Hashtable form, this information you can get from https://vmw-loginsight.github.io/#Getting-started-with-the-Log-Insight-REST-API.
    .PARAMETER RestAPI
        This is Rest API string for vRealize Log Insight, The string after https://loginsightserver:9543/api/v1. Check more information on #"https://$logInsightServer/rest-api#Getting-started-with-the-Log-Insight-REST-API"
    .INPUTS
        string
        System.Collections.Hashtable
        Microsoft.PowerShell.Commands.HtmlWebResponseObject
    .OUTPUTS
        Microsoft.PowerShell.Commands.HtmlWebResponseObject
    .NOTES
    Version:        1.0
    Author:         Kunal Udapi
    Creation Date:  19 July 2020
    Purpose/Change: Change (put) the configuration/Information on vRealize Log Insight Rest API using PowerShell
    Useful URLs: http://vcloud-lab.com
    Tested on below versions:
        VMware vRealize Log Insight: 8.0
        Microsoft PowerShell: 5.1
        Operating System: Microsoft Windows 10
    .EXAMPLE
        $session = Login-LogInsight -Server loginsightserver -Username admin -Password P@ssw0rd -RestAPI sessions
        $body = @{timeReference="NTP_SERVER"; ntpServers=@('172.30.10.5', '172.30.10.6')}
        Put-LogInsightAPI -Server loginsightserver -Session $session -RestAPI 'time/config' -Body $body

        Name                           Value
        ----                           -----
        ntpServers                     {172.30.10.5, 172.30.10.6}
        timeReference                  NTP_SERVER

        Content           : {}
        StatusCode        : 200
        StatusDescription : OK
        RawContentStream  : Microsoft.PowerShell.Commands.WebResponseContentMemoryStream
        RawContentLength  : 0
        RawContent        : HTTP/1.1 200 OK
                            X-LI-Build: 16281169
                            ACCESS-CONTROL-EXPOSE-HEADERS: X-LI-Build
                            Content-Length: 0
                            Date: Sun, 19 Jul 2020 07:57:10 GMT


        BaseResponse      : System.Net.HttpWebResponse
        Headers           : {[X-LI-Build, 16281169], [ACCESS-CONTROL-EXPOSE-HEADERS, X-LI-Build], [Content-Length, 0], [Date, Sun, 19 Jul 2020 07:57:10 GMT]}

        Use Login-LogInsight script and use the information as Session parameter, This function script use PUT verb to configure NTP Servers on vRLI.
    #>    
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, HelpMessage = 'Type LogInsight FQDN/IP')]
        [string]$Server = 'LogInsight.vcloud-lab.com',
        [Parameter(Mandatory = $true, HelpMessage = 'Type LogInsight FQDN/IP')]
        [Microsoft.PowerShell.Commands.HtmlWebResponseObject]$Session,
        [Parameter(Mandatory = $true, HelpMessage = 'Type LogInsight FQDN/IP')]
        [System.Collections.Hashtable]$Body = @{timeReference="NTP_SERVER"; ntpServers=@('172.30.10.1', '172.30.10.2')},
        [Parameter(Mandatory = $true, HelpMessage = 'Type Rest API node')]
        [string]$RestAPI = 'licenses' #Type string after the /api/v1/
    )
    begin
    {
        Approve-SelfSignedCertificate
    }
    process 
    {
        try 
        {
            $Body
            $sessionID = $Session.Content | ConvertFrom-Json | Select-Object -ExpandProperty sessionId
            $headers = @{'Authorization' = "Bearer $sessionID"}
            $putInfo = Invoke-WebRequest -Uri "https://$($server):9543/api/v1/$RestAPI" -Method Put -Headers $headers -ContentType 'application/json' -Body $($Body | ConvertTo-Json) -ErrorAction Stop
            $putInfo
            Write-Host "Configured $RestAPI" -BackgroundColor DarkGreen
        }
        catch 
        {
            Write-Host $error[0].Exception.Message -BackgroundColor DarkRed
        }

    }
    end {}
}

Download this module from github.com/kunaludapi or you can download module LogInsightAPI here.

You can use the modules using below examples, To start using this module unzip the zip file, do not change the folder or files names in it. Import the module with Import-Module. (You can safely ignore the WARNING: The names of some imported commands from the module 'LogInsightAPI' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb)

Next list the cmdlets available in the module with Get-Command. I am storing the Log Insight server information in variable as I will be keep using it again and again. All the functions are based on cmdlet Invoke-WebRequest to consume Rest API.

Import-Module .\LogInsightAPI
Get-Command -Module LogInsightAPI

#Details about vRealize Log Insight Server
$server = '192.168.34.15'

#Login into vRealize Log Insight Server using Rest API
$session = Login-LogInsightAPI -Server $server -Username admin -Password Computer@1 -RestAPI sessions

First is login into vRLI. with Login-LogInsightAPI. Provide parameters server, username (admin), password  and RestAPI as sessions. All the references taken from given guide urls. After successful login it shows the messages accordingly. The login information is stored on session variable, this contains the bearer token (login information) required to perform the tasks or fetch information, which I will use later.

Microsoft Powershell Rest API Import-Module vrealize log insight Rest API Get-Command Powershell function invoke-restmethod invoke-request body header Select-object hashtable.png

The command Get-LogInsightAPI helps to get the configurations from vRLI Rest API, for example I am fetching configured NTP Servers details with 'time/config' Rest API (Document: https://vmw-loginsight.github.io/#time_config).

#Use Get verb to get NTP Server settings from vRealize Log Insight Server Configuration
$logInsightNtpServer = Get-LogInsightAPI -Server $server -Session $session -RestAPI 'time/config'
$logInsightNtpServer.Content | ConvertFrom-Json | Select-Object -ExpandProperty ntpConfig

To modify the list of NTP servers information on vRLI use Put-LogInsightAPI. I am using verb method PUT. To modify NTP servers make changes to body hashtable and use the same session variable, It has login information with bearer token.

#Use Put verb to change settings on vRealize Log Insight Server using Rest API
$body = @{timeReference="NTP_SERVER"; ntpServers=@('192.168.34.11', '1921.68.34.254')}
$logInsightNtpServer = Put-LogInsightAPI -Server $server -Session $session -RestAPI 'time/config' -Body $body
$logInsightNtpServer[0]

Although it shows the new configuration in Put cmdlet  but still you can verify the same running function cmdlet Get-LogInsightAPI again. I will be showing Log Insight configuration using ansible in another article.

Microsoft Powershell Get-LogInsightAPI Put-LogInsightAPI consume rest api session body header ntserver configuration ConvertFrom-Json Select-Object ExpandProperty Log Insight server.png

Useful Articles
POWERCLI AND VSPHERE WEB CLIENT: JOIN ESXI INTO ACTIVE DIRECTORY DOMAIN CONTROLLER
Resolved: Esxi Join domain failed - Error in Active Directory Operations
Join domain ESXi to an Active Directory OU : Powercli

Esxi reset root password, reset windows administrator passwordReset forgotten ESXi root password on Domain joined Esxi using vSphere web client and Powercli
Reset ESXi root password using Host Profiles on vCenter server: VMWare vSphere Web client
Resolved: Reset Esxi forgotten root password using hiren bootCD step by step

Go Back

Comment

Blog Search

Page Views

12056100

Follow me on Blogarama