Elastic.Console.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 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 |
# Register-ArgumentCompleter needs minimum v3.0 Set-StrictMode -Version 3.0 #load types used by the module if (-not ([System.Management.Automation.PSTypeName]'Elastic.ElasticsearchRequestBody').Type) { # A type to accept only string or hashtable as input. # This allows $Body in Invoke-Elasticsearch to *not* bind to ElasticsearchRequest Add-Type -LiteralPath (Join-Path $PSScriptRoot -ChildPath "ElasticsearchRequestBody.cs") } if (-not ([System.Management.Automation.PSTypeName]'Elastic.ElasticVersion').Type) { # A type to represent an Elasticsearch version Add-Type -LiteralPath (Join-Path $PSScriptRoot -ChildPath "ElasticVersion.cs") } if (-not ([System.Management.Automation.PSTypeName]'Elastic.ServerCertificateValidation').Type) { # A type for skipping Certificate validation. There is a bug in some versions of PowerShell # where using a script block does not work, but using a class does. Add-Type -LiteralPath (Join-Path $PSScriptRoot -ChildPath "ServerCertificateValidation.cs") } # module scope variables $Script:completerComponents = $null $Script:version = $null $Script:methods = @("GET", "PUT", "POST", "DELETE", "HEAD") $forwardSlashChar = @('/') # Rely on the OS default for valid SSL/TLS protocols [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::SystemDefault # Disable nagling and expect 100 continue. Only valid for PowerShell versions # where ServicePointManager is used i.e. PowerShell not based on .NET Core and NetStandard [System.Net.ServicePointManager]::Expect100Continue = $false [System.Net.ServicePointManager]::UseNagleAlgorithm = $false <# .Synopsis Tests if there is anything in the pipeline #> function Test-Any() { Begin { $any = $false } Process { $any = $true } End { $any } } <# .Synopsis Gets the version of Elasticsearch with which to work .Description Gets the version of Elasticsearch with which to work. Use the -Installed parameter to list Elasticsearch versions for which specs are installed. .Parameter ListAvailable Lists the Elasticsearch versions for which specs have been downloaded, in descending version order. A version with downloaded specs does not require downloading files when using Set-ElasticsearchVersion to power tab completion. .Example PS> Set-ElasticsearchVersion 6.2.0 PS> Get-ElasticsearchVersion Sets the Elasticsearch version to 6.2.0, then retrieves the set version .Example PS> Get-ElasticsearchVersion -ListAvailable Lists the Elasticsearch versions for which specs have been downloaded .Example PS> Get-ElasticsearchVersion -ListAvailable | Select-Object -First 1 | Set-ElasticsearchVersion Lists the Elasticsearch versions for which specs have been downloaded, selecting the latest version downloaded, and setting this as the version of Elasticsearch to work with #> function Get-ElasticsearchVersion { [CmdletBinding()] param( [switch] [Parameter()] $ListAvailable ) if ($ListAvailable) { $specsDir = (Join-Path $PSScriptRoot -ChildPath "specs") Get-ChildItem $specsDir -Directory -Name | ForEach-Object { $version = $null [Elastic.ElasticVersion]::TryParse($_, [ref]$version) | Out-Null $version } | Where-Object { $null -ne $_ } | Sort-Object -Descending } else { $Script:Version } } <# .Synopsis Sets the version of Elasticsearch with which to work. .Description Downloads the REST API specs for a specific version of Elasticsearch, using the API paths in the specification to power tab completion on paths .Parameter Version The Elasticsearch version .Parameter Force Forces the REST API specs to be downloaded, overwriting any existing specs for the version, and regenerating the file to power tab completion on paths .Example PS> Set-ElasticsearchVersion 6.2.0 Sets the version of Elasticsearch to 6.2.0 .Example PS> Set-ElasticsearchVersion -Version 7.0.0-beta1 -Force Sets the version of Elasticsearch to 7.0.0-beta1, overriding any existing downloaded files for the version .Example PS> Set-ElasticsearchVersion 6.2 Sets the version of Elasticsearch to 6.2.0. An omitted patch version part will have the value 0 by default .Example PS> Set-ElasticsearchVersion 6 Sets the version of Elasticsearch to 6.0.0. Omitted minor and patch version parts will have the value 0 by default #> function Set-ElasticsearchVersion { [CmdletBinding()] param( [Elastic.ElasticVersion] [Parameter(Mandatory = $true, ValueFromPipeline=$true)] $Version, [Parameter()] [switch] $Force ) Begin { } Process { if ($Version -eq $Script:version -and -not $Force) { return } $Script:Version = $Version $Script:completerComponents = $null $specsDir = (Join-Path $PSScriptRoot -ChildPath "specs") if (-not (Test-Path $specsDir)) { New-Item -Path $specsDir -ItemType Directory | Out-Null } $versionDir = (Join-Path $specsDir -ChildPath ($Version.ToString())) if (-not (Test-Path $versionDir)) { New-Item -Path $versionDir -ItemType Directory | Out-Null } $autocompleteFileName = "autocomplete.json" $autocompleteFile = (Join-Path $versionDir -ChildPath $autocompleteFileName) # Get the REST API specs from GitHub for the Elasticsearch version if (-not (Test-Path $autocompleteFile) -or $Force) { $contentsApi = "https://api.github.com/repos/elastic/elasticsearch/contents" $specUrls = @("$contentsApi/rest-api-spec/src/main/resources/rest-api-spec/api?ref=v$Version") # Get the REST API specs for Elastic Stack Feature/X-Pack endpoints too, when available if ($Version -ge "6.3.0") { $specUrls += "$contentsApi/x-pack/plugin/src/test/resources/rest-api-spec/api?ref=v$Version" } $downloadurls = $specUrls | Foreach-Object { Invoke-RestMethod $_ | Where-Object { $_.name.EndsWith(".json") } | Foreach-Object { $_.download_url } } # TODO: optimize by downloading in parallel for ($i = 0; $i -lt $downloadurls.count; $i++) { $downloadurl = $downloadurls[$i] $file = Split-Path $downloadurl -Leaf $outfile = Join-Path $versionDir -ChildPath $file Write-Progress -Activity "Downloading REST API specs for Elasticsearch $Version" -Status "Downloading $file" ` -PercentComplete ($i / $downloadurls.Count * 100) (New-Object System.Net.WebClient).DownloadFile($downloadurl, $outfile) } $excludeSpecs = @($autocompleteFileName, "_common.json") $specs = Get-ChildItem $versionDir -File -Filter *.json | Where-Object { $excludeSpecs -notcontains $_.Name } | ForEach-Object { $_.FullName } $apiCompleters = @() $pathCompleters = New-Object System.Collections.ArrayList foreach($spec in $specs) { $json = Get-Content $spec -Raw | ConvertFrom-Json $api = $json.PsObject.Properties | Select-Object -First 1 # skip specs where the first key/value isn't an object if (-not ($api.Value -is [string])) { $name = $api.Name if ($api.Value.PsObject.Properties.Name -contains "methods") { # old REST api spec format that lists methods at the top level $methods = $api.Value.methods $url = $api.Value.url foreach($path in $url.paths) { $apiCompleter = @{ name = $name path = $path parts = $path.Split($forwardSlashChar, [System.StringSplitOptions]::RemoveEmptyEntries) methods = $methods } $apiCompleters += $apiCompleter for ($i = 0; $i -lt $apiCompleter.parts.Length; $i++) { if (($pathCompleters.Count - 1) -lt $i) { [void]$pathCompleters.Add($(New-Object System.Collections.Generic.HashSet[string])) } [void]$pathCompleters[$i].Add($apiCompleter.parts[$i]) } } } else { # newer REST api spec format that lists methods against paths $url = $api.Value.url foreach($path in $url.paths) { $apiCompleter = @{ name = $name path = $path.path parts = $path.path.Split($forwardSlashChar, [System.StringSplitOptions]::RemoveEmptyEntries) methods = $path.methods } $apiCompleters += $apiCompleter for ($i = 0; $i -lt $apiCompleter.parts.Length; $i++) { if (($pathCompleters.Count - 1) -lt $i) { [void]$pathCompleters.Add($(New-Object System.Collections.Generic.HashSet[string])) } [void]$pathCompleters[$i].Add($apiCompleter.parts[$i]) } } } } } # $pathCompleters must be an array rather than an ArrayList. With the latter, # No autocompletion with urlCompleter after downloading REST specs otherwise $Script:completerComponents = @{ apiCompleters = $apiCompleters pathCompleters = $pathCompleters.ToArray() } ConvertTo-Json -InputObject $Script:completerComponents -Compress -Depth 3 | Set-Content $autocompleteFile } if ($null -eq $Script:completerComponents) { $Script:completerComponents = Get-Content $autocompleteFile -Raw | ConvertFrom-Json } $uriCompleter = { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) $uriAndQuery = $wordToComplete.Trim('"').Split('?') $wordToComplete = $uriAndQuery[0] if ($uriAndQuery.Length -gt 1) { $query = $uriAndQuery[1] } else { $query = $null } # if the Uri contains an authority, this should be prepended to any autocompletion results. # if it doesn't, the Uri should be normalized to start with forward slash to match paths in REST API specs $parsedUri = $null $authority = $null $validUri = [Uri]::TryCreate($wordToComplete, [UriKind]::RelativeOrAbsolute, [ref]$parsedUri) if ($validUri -and $parsedUri.IsAbsoluteUri) { $authority = $parsedUri.GetLeftPart([UriPartial]::Authority) $toComplete = [System.Web.HttpUtility]::UrlDecode($parsedUri.AbsolutePath) } else { if ($wordToComplete.StartsWith("/")) { $toComplete = $wordToComplete } else { $toComplete = "/" + $wordToComplete } } # Filter suggested APIs by passed Method $method = $fakeBoundParameters.ContainsKey("Method") if ($method) { $apis = ($Script:completerComponents).apiCompleters | Where-Object { $_.methods -contains "$($fakeBoundParameters.Method)" } } else { $apis = ($Script:completerComponents).apiCompleters } $parts = $toComplete.Split($forwardSlashChar, [System.StringSplitOptions]::RemoveEmptyEntries) # only filter when a value has been provided i.e. anything but empty string if ($parts.Length -gt 0) { # calculate if there are any paths that would be a like match for the last part of the passed path. # if there are, any token matches should be excluded later on $partsLikeness = New-Object bool[] -ArgumentList $parts.Length $likes = [string[]]($parts | ForEach-Object { "$_*" }) for ($i = 0; $i -lt $parts.Length; $i++) { $part = $parts[$i] if (($Script:completerComponents).pathCompleters.Length -ge $parts.Length) { $partsLikeness[$i] = ($Script:completerComponents).pathCompleters[$i] | Where-Object { $_ -like $likes[$i] } | Test-Any } } $len = $parts.Length - 1 $apis = $apis | Where-Object { # Exclude APIs with parts shorter than the one we're gonna match if ($_.parts.Length -lt $parts.Length) { return $false } # for all parts of the path except the last, the part either needs # to be an exact match or a token, like {index} for ($i = 0; $i -lt $len; $i++) { $part = $parts[$i] if ($partsLikeness[$i] -and $part -ne $_.parts[$i]) { return $false } elseif ($part -ne $_.parts[$i] -and -not $_.parts[$i].StartsWith("{")) { return $false } } # for the last part of the path, the path part needs to be like the passed last part # and if there aren't any matches, can also be a token if ($partsLikeness[$len]) { return $_.parts[$len] -like $likes[$len] } else { return $_.parts[$len] -like $likes[$len] -or $_.parts[$len].StartsWith("{") } } } # completion suggestions $apis | ForEach-Object { $path = New-Object string[] -ArgumentList $_.parts.Length for ($i = 0; $i -lt $_.parts.Length; $i++) { if ($i -eq ($_.parts.Length - 1)) { if ($_.parts[$i] -eq "{index}") { # TODO: Get the indices names and return as multiple values for this API $path[$i] = $_.parts[$i] } else { $path[$i] = $_.parts[$i] } } elseif ($i -lt $parts.Length -and $_.parts[$i].StartsWith("{")) { $path[$i] = $parts[$i] } else { $path[$i] = $_.parts[$i] } } $path = "/" + ($path -join "/") if ($authority) { $builder = New-Object System.UriBuilder -ArgumentList $authority $builder.Path = $path if ($query) { $builder.Query = $query } # don't include default ports, if present if (($builder.Scheme -eq "http" -and $builder.Port -eq 80) -or ` ($builder.Scheme -eq "https" -and $builder.Port -eq 443)) { $builder.Port = -1 } $completionText = [System.Web.HttpUtility]::UrlDecode($builder.ToString()) } else { if ($query) { $completionText = $path + "?" + $query } else { $completionText = $path } } # Always quote completion results as token URL parts like {index} represent script blocks when left unquoted New-Object System.Management.Automation.CompletionResult -ArgumentList "`"$completionText`"", $_.path, "ParameterValue", $_.name } # pass back the original value as a completion suggestion too, if it has a value if ($wordToComplete) { New-Object System.Management.Automation.CompletionResult -ArgumentList "`"$wordToComplete`"", $wordToComplete, "ParameterValue", $wordToComplete } } Register-ArgumentCompleter -CommandName Invoke-Elasticsearch -ParameterName Uri -ScriptBlock $uriCompleter } } # auto completion for HTTP methods Register-ArgumentCompleter -CommandName Invoke-Elasticsearch -ParameterName Method -ScriptBlock { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) if ($null -eq $Script:completerComponents.apiCompleters -or $fakeBoundParameters.ContainsKey("Uri") -eq $false) { # If there's no Uri then all methods are valid $Script:methods | ForEach-Object { New-Object System.Management.Automation.CompletionResult -ArgumentList $_, $_, "ParameterValue", $_ } } else { # get just the path out of the Uri $uri = $fakeBoundParameters.Uri.Trim('"').Split('?')[0] $parsedUri = $null if ([Uri]::TryCreate($uri, [UriKind]::RelativeOrAbsolute, [ref]$parsedUri) -and $parsedUri.IsAbsoluteUri) { $toComplete = [System.Web.HttpUtility]::UrlDecode($parsedUri.AbsolutePath) } else { if ($uri.StartsWith("/")) { $toComplete = $uri } else { $toComplete = "/" + $uri } } $parts = $toComplete.Split($forwardSlashChar, [System.StringSplitOptions]::RemoveEmptyEntries) $apis = $Script:completerComponents.apiCompleters # only filter when a value has been provided i.e. anything but empty string if ($parts.Length -gt 0) { # calculate if there are any paths that would be a like match for the last part of the passed path. # if there are, any token matches should be excluded later on $partsLikeness = New-Object bool[] -ArgumentList $parts.Length $likes = [string[]]($parts | ForEach-Object { "$_*" }) for ($i = 0; $i -lt $parts.Length; $i++) { $part = $parts[$i] if ($Script:completerComponents.pathCompleters.Length -ge $parts.Length) { $partsLikeness[$i] = $Script:completerComponents.pathCompleters[$i] | Where-Object { $_ -like $likes[$i] } | Test-Any } } $len = $parts.Length - 1 $apis = $apis | Where-Object { # Exclude APIs with parts shorter than the one we're gonna match if ($_.parts.Length -lt $parts.Length) { return $false } # for all parts of the path except the last, the part either needs # to be an exact match or a token for ($i = 0; $i -lt $len; $i++) { $part = $parts[$i] if ($partsLikeness[$i] -and $part -ne $_.parts[$i]) { return $false } elseif ($part -ne $_.parts[$i] -and -not $_.parts[$i].StartsWith("{")) { return $false } } # for the last part of the path, the path part needs to be like the passed last part # and if there aren't any matches, can also be a token if ($partsLikeness[$len]) { return $_.parts[$len] -like $likes[$len] } else { return $_.parts[$len] -like $likes[$len] -or $_.parts[$len].StartsWith("{") } } } if (-not $apis) { # If there's no match then all methods are valid $Script:methods | ForEach-Object { New-Object System.Management.Automation.CompletionResult -ArgumentList $_, $_, "ParameterValue", $_ } } else { $apis | ForEach-Object { -split $_.methods } | Sort-Object -Unique | ForEach-Object { New-Object System.Management.Automation.CompletionResult -ArgumentList $_, $_, "ParameterValue", $_ } } } } <# .Synopsis Converts a Kibana console request to a request that can be piped to Invoke-Elasticsearch .Description Converts a Kibana console request to a request that can be piped to Invoke-Elasticsearch. The converted request is a custom PowerShell object with request properties that can be piped to Invoke-Elasticsearch .Example PS> @' PUT /my_locations { "mappings": { "properties": { "pin": { "properties": { "location": { "type": "geo_point" } } } } } } PUT /my_locations/_doc/1 { "pin" : { "location" : { "lat" : 40.12, "lon" : -71.34 } } } '@ | ConvertFrom-KibanaConsole | Invoke-Elasticsearch Converts two Kibana console requests to requests that can be piped to Elasticsearch to execute .Example PS> 'GET /_cat/indices' | ckc | es Converts a Kibana console GET request to the _cat/indices endpoint and pipes the resulting request to Elasticsearch to execute .Link https://www.elastic.co/guide/en/kibana/current/console-kibana.html #> function ConvertFrom-KibanaConsole { [CmdletBinding()] param ( [string] [Parameter(Mandatory=$true, ValueFromPipeline=$true)] $Request ) Begin { $requests = @() } Process { # A command may be multiple API requests, so split by empty lines or lines starting with verbs $requests += [Regex]::Split($Request, "\r?\n\r?\n|\r?\n(?=HEAD|GET|PUT|POST|DELETE)") } End { $emptyChars = @(" ") $newLines = [string[]]@("`n", "`r`n", [Environment]::NewLine) $requests | ForEach-Object { $consoleParts = $_.Split($newLines, 2, [StringSplitOptions]::RemoveEmptyEntries) $methodUriParts = $consoleParts[0].Split($emptyChars, 2, [StringSplitOptions]::None) if ($methodUriParts.Length -ne 2) { throw "'$($consoleParts[0])' is not a valid Kibana Console command of the form '<METHOD> <PATH>'" } $method = $methodUriParts[0] $uri = $methodUriParts[1] if ($consoleParts.Length -gt 1) { $body = $consoleParts[1] } else { $body = $null } return [PSCustomObject]@{ PSTypeName = "ElasticsearchRequest" Method = $method Uri = $uri Body = $body } } } } <# .Synopsis Executes a REST API request against Elasticsearch .Description Provides a simpler experience for executing REST API requests against Elasticsearch. Use Set-ElasticsearchVersion <version> to download the REST API specs for a specific version of Elasticsearch, to power tab completion of available endpoints and methods. A request that does not specify a method - Without a body will be a GET request - With a body will be a POST request In addition, a GET request with a body will be sent as a POST request, since PowerShell does not allow sending a GET request with a body. .Example es _cat/indices Sends a request to Elasticsearch to list the indices in the cluster. .Example PS> es twitter/_doc/1 -Pretty -Method PUT -Body @' { "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch" } '@ Sends a request to Elasticsearch to create a document with id 1 in the twitter index. The document is sent as a JSON string literal .Example PS> es posts/_search?pretty -u elastic:changeme -H @{ 'X-Opaque-Id' = 'track_this_call' } -ResponseVariable response -d @{ query = @{ match = @{ user = "kimchy" } } } PS> $statusCode = $response.StatusCode PS> $responseHeaders = $response.Headers Sends a request to Elasticsearch to search all indices, passing an X-Opaque-Id header to track the call using the tasks API. The search query is passed as a Hashtable. The underlying PowerShell response is captured with the response variable, allowing the status code and response headers to be inspected. .Example PS> es posts/_bulk -ContentType application/x-ndjson -d C:\data.json Sends a bulk request to Elasticsearch with the newline delimited application/x-ndjson content type. The body of the request is read from the file C:\data.json .Example PS> gc C:\data.json | es posts/_bulk -ContentType application/x-ndjson Sends a bulk request to Elasticsearch with the newline delimited application/x-ndjson content type. The body of the request is piped to the command. .Parameter Method The HTTP method to use. For requests with a body, will default to 'POST' and without, 'GET'. .Parameter Uri The URI to make the request against. A relative URI path will make a request using the base URI 'http://localhost:9200' .Parameter User The username for Authentication. the password may also be specified here using the format 'username:password' .Parameter Password The password for Authentication. if username is specified but password is not, an interactive prompt will be displayed to provide the password. .Parameter Body The request body. May be a JSON string literal, a Hashtable, or a path to a file containing JSON .Parameter ContentType The Content-Type HTTP header. By default, uses 'application/json' .Parameter Headers A Hashtable of the HTTP headers to send .Parameter Pretty Pretty print (indent) the JSON response. Alternatively, may be supplied as a query string parameter on the Uri with '?pretty' or '?pretty=true' .Parameter SkipCertificateCheck By default, Server certificates are verified when making requests against Elasticsearch secured by SSL/TLS. Verification can be skipped by specifying SkipCertificateCheck. Can be useful when working with self-signed certificates, for example. .Parameter ResponseVariable The name of a variable to which the response will be assigned, with global scope. The response can be inspected for response headers, status code, etc. .Parameter Bytes Return the response as a byte array. The response will be returned as a string by default, which may not be desired for content types like CBOR. .Inputs The request body. May be a JSON string literal, a Hashtable, or a path to a file containing JSON .Outputs The response body as a string #> function Invoke-Elasticsearch { [CmdletBinding()] [OutputType([string])] param ( [string] # Use ValidateScript to work in conjunction with Register-ArgumentCompleter for Method [ValidateScript({ if ($_ -in $Script:methods) { $true } else { throw "'$_' must be one of $($Script:methods -join ", ")" } })] [Parameter(ValueFromPipelineByPropertyName=$true)] [Alias("X")] $Method, [string] [Parameter(Mandatory=$true,Position=0,ValueFromPipelineByPropertyName=$true)] $Uri, [string] [Alias("u")] $User, [SecureString] $Password, [Alias("d")] [Alias("data")] [Elastic.ElasticsearchRequestBody] [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] $Body, [string] $ContentType = "application/json", [Alias("H")] $Headers = @{}, [switch] $Pretty, [Alias("k")] [Alias("insecure")] [switch] $SkipCertificateCheck, [Alias("response")] [string] $ResponseVariable, [switch] $Bytes ) Begin { } Process { if (-not $Method) { if ($Body) { $Method = "POST" } else { $Method = "GET" } } elseif ($Body -and $Method -eq "GET") { # Invoke-WebRequest does not allow sending a body with GET, so force POST $Method = "POST" } if ($Uri) { $parsedUri = $null if ([Uri]::TryCreate($Uri, [UriKind]::RelativeOrAbsolute, [ref]$parsedUri) -and -not $parsedUri.IsAbsoluteUri) { $parsedUri = New-Object System.Uri "http://localhost:9200/$($parsedUri.OriginalString.TrimStart('/'))" } } else { $parsedUri = New-Object System.Uri "http://localhost:9200/" } # ParseQueryString does not respect keys without values, so test .Query directly if ($Pretty -and (-not $parsedUri.Query -or $parsedUri.Query -match "[?|&]pretty(?=\=true)" -eq $false)) { $queryString = [System.Web.HttpUtility]::ParseQueryString($parsedUri.Query) if (-not $queryString) { $queryString = New-Object System.Collections.Specialized.NameValueCollection } $queryString.Set("pretty","true") $uriBuilder = New-Object System.UriBuilder $parsedUri $uriBuilder.Query = $queryString.ToString(); $parsedUri = $uriBuilder.Uri } if ($User) { $userParts = $User.Split(':', 2) if ($userParts.Length -eq 2) { $User = $userParts[0] $Password = $userParts[1] | ConvertTo-SecureString -AsPlainText -Force } while (-not $Password -or $Password.Length -eq 0) { $Password = Read-Host -AsSecureString "Enter password for $($User):" } $credential = New-Object System.Management.Automation.PSCredential ($User, $Password) } else { $credential = $null } $requestParameters = @{ Uri = $parsedUri ContentType = $ContentType Headers = $Headers Credential = $credential Method = $Method UseBasicParsing = $true } # Allow PowerShell Core to send credentials over unencrypted connection. Possibly expose as param? if ((Get-Command Invoke-WebRequest).Parameters.ContainsKey("AllowUnencryptedAuthentication")) { $requestParameters.AllowUnencryptedAuthentication = $true } if ($Body) { if ($Body.Input -is [string]) { if (Test-Path $Body.Input -PathType Leaf) { $requestParameters.InFile = $Body.Input } else { $requestParameters.Body = $Body.Input } } else { $requestParameters.Body = $Body.Input | ConvertTo-Json } } if ($SkipCertificateCheck) { if ((Get-Command Invoke-WebRequest).Parameters.ContainsKey("SkipCertificateCheck")) { # PowerShell Core $requestParameters.SkipCertificateCheck = $true } else { # PowerShell [System.Net.ServicePointManager]::ServerCertificateValidationCallback = [Elastic.ServerCertificateValidation]::AllowAll() } } try { $response = Invoke-WebRequest @requestParameters if ($ResponseVariable) { Set-Variable -Name $ResponseVariable -Value $response -Scope Global } if ($response.Content.GetType().FullName -eq "System.Net.Http.HttpContent") { #PowerShell Core if ($Bytes) { return $response.Content.ReadAsByteArrayAsync().Result; } else { return $response.Content.ReadAsStringAsync().Result; } } else { #PowerShell if ($response.Content -is [string]) { if ($Bytes) { return [Text.Encoding]::UTF8.GetBytes($response.Content); } return $response.Content; } else { if ($Bytes) { return $response.Content } return [Text.Encoding]::UTF8.GetString($response.Content); } } } catch { if ($_.Exception | Get-Member Response) { # Powershell $response = $_.Exception.Response } else { # PowerShell Core $response = $null } if ($ResponseVariable) { Set-Variable -Name $ResponseVariable -Value $response -Scope Global } if ($response) { if ($response | Get-Member GetResponseStream) { # PowerShell $responseStream = $response.GetResponseStream() $reader = New-Object System.IO.StreamReader($responseStream) $reader.BaseStream.Position = 0 $reader.DiscardBufferedData() return $reader.ReadToEnd() } else { # PowerShell Core if ($_.ErrorDetails -and $_.ErrorDetails.Message) { return $_.ErrorDetails.Message; } else { throw $_.Exception } } } else { throw $_.Exception } } finally { if ($SkipCertificateCheck -and (Get-Command Invoke-WebRequest).Parameters.ContainsKey("SkipCertificateCheck") -eq $false) { [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null } } } End { } } function Get-ElasticsearchIndex { [OutputType([string[]])] param( [string] [Parameter(Position=0,ValueFromPipelineByPropertyName=$true)] $Uri, [string] [Alias("u")] [Parameter(ValueFromPipelineByPropertyName=$true)] $User, [SecureString] [Parameter(ValueFromPipelineByPropertyName=$true)] $Password ) $parsedUri = $null $authority = $null if ([Uri]::TryCreate($Uri, [UriKind]::RelativeOrAbsolute, [ref]$parsedUri) -and $parsedUri.IsAbsoluteUri) { $authority = $parsedUri.GetLeftPart([UriPartial]::Authority) } if ($authority) { $builder = New-Object System.UriBuilder -ArgumentList $authority $builder.Path = "_cat/indices" $builder.Query = "?h=index" if (($builder.Scheme -eq "http" -and $builder.Port -eq 80) -or ` ($builder.Scheme -eq "https" -and $builder.Port -eq 443)) { $builder.Port = -1 } $catIndicesUri = $builder.ToString() } else { $catIndicesUri = "_cat/indices?h=index" } $parameters = @{ User = $User Password = $Password Method = "GET" Uri = $catIndicesUri } try { $indices = Invoke-Elasticsearch @parameters return $indices.Split([Environment]::NewLine, [StringSplitOptions]::RemoveEmptyEntries) } catch { Write-Warning "Unable to retrieve indices for $($parameters.Keys.ForEach({"$_ $($parameters.$_)"}) -join ','). $_.Exception" return @() } } # Set Elasticsearch version to the one being installed Set-ElasticsearchVersion -Version "7.9.0" Set-Alias -Name es -Value Invoke-Elasticsearch -Description "Sends a request to Elasticsearch" Set-Alias -Name ckc -Value ConvertFrom-KibanaConsole -Description "Converts a Kibana Console request to a request that can be passed to Invoke-Elasticsearch" Set-Alias -Name hash -Value ConvertFrom-Json -Description "Converts JSON into a dictionary/hashmap" # SIG # Begin signature block # MIIOHAYJKoZIhvcNAQcCoIIODTCCDgkCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUihgLC5N0icSNutAZ96tvfBev # 3h2gggtSMIIFajCCBFKgAwIBAgIRALVV9nHrVFUKcgNbgqjR2vAwDQYJKoZIhvcN # AQELBQAwfTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF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