Skip to content

Instantly share code, notes, and snippets.

@MyITGuy
Created December 27, 2024 21:37
Show Gist options
  • Save MyITGuy/84d902e82162f6b5a8c0d7e4ba618c74 to your computer and use it in GitHub Desktop.
Save MyITGuy/84d902e82162f6b5a8c0d7e4ba618c74 to your computer and use it in GitHub Desktop.
function Get-SplunkDownload {
[CmdletBinding()]
param(
[string[]]$Product
,
[string[]]$XProduct
,
[string[]]$Architecture
,
[string[]]$XArchitecture
,
[string[]]$Platform
,
[string[]]$XPlatform
)
#region Filtering
filter FilterByPlatform {
$Object = $_
$ErrorActionPreference = 'SilentlyContinue'
$thisPlatform = ($Object.getAttribute('data-platform') | Out-String).Trim()
$ErrorActionPreference = 'Continue'
Write-Verbose "thisPlatform: $($thisPlatform)"
if (!$thisPlatform) {
Write-Warning "Property not found. [Platform] ($($Object))"
return
}
if (!$Platform) {
$Object
}
elseif ( $thisPlatform -in $Platform) {
$Object
}
}
filter FilterByXPlatform {
$Object = $_
$ErrorActionPreference = 'SilentlyContinue'
$thisXPlatform = $Object.XPlatform
$ErrorActionPreference = 'Continue'
Write-Verbose "thisXPlatform: $($thisXPlatform)"
if (!$thisXPlatform) {
Write-Warning "Property not found. [XPlatform] ($($Object))"
return
}
if (!$XPlatform) {
$Object
}
elseif ( $Object.XPlatform -in $XPlatform) {
$Object
}
}
filter FilterByProduct {
$Object = $_
$ErrorActionPreference = 'SilentlyContinue'
$thisProduct = ($Object.getAttribute('data-product') | Out-String).Trim()
$ErrorActionPreference = 'Continue'
Write-Verbose "thisProduct: $($thisProduct)"
if (!$thisProduct) {
Write-Warning "Property not found. [Product] ($($Object))"
return
}
if (!$Product) {
$Object
}
elseif ( $thisProduct -in $Product) {
$Object
}
}
filter FilterByXProduct {
$Object = $_
$ErrorActionPreference = 'SilentlyContinue'
$thisXProduct = $Object.XProduct
$ErrorActionPreference = 'Continue'
Write-Verbose "thisXProduct: $($thisXProduct)"
if (!$thisXProduct) {
Write-Warning "Property not found. [XProduct] ($($Object))"
return
}
if (!$XProduct) {
$Object
}
elseif ( $thisXProduct -in $XProduct) {
$Object
}
}
filter FilterByArchitecture {
$Object = $_
$ErrorActionPreference = 'SilentlyContinue'
$thisArchitecture = ($Object.getAttribute('data-arch') | Out-String).Trim()
$ErrorActionPreference = 'Continue'
Write-Verbose "thisArchitecture: $($thisArchitecture)"
if (!$thisArchitecture) {
Write-Warning "Property not found. [Architecture] ($($Object))"
return
}
if (!$Architecture) {
$Object
}
elseif ( $thisArchitecture -in $Architecture) {
$Object
}
}
filter FilterByXArchitecture {
$Object = $_
$ErrorActionPreference = 'SilentlyContinue'
$thisXArchitecture = $Object.XArchitecture
$ErrorActionPreference = 'Continue'
Write-Verbose "thisXArchitecture: $($thisXArchitecture)"
if (!$thisXArchitecture) {
Write-Warning "Property not found. [XArchitecture] ($($Object))"
return
}
if (!$XArchitecture) {
$Object
}
elseif ( $thisXArchitecture -in $XArchitecture) {
$Object
}
}
#endregion Filtering
[System.Uri]$BaseURI = 'https://www.splunk.com/en_us/download'
$ProductFamilies = @('Splunk Enterprise', 'Universal Forwarder')
$ProductFamiliesAssumedLinks = $ProductFamilies | ForEach-Object { $_.Split(' ').ToLower() -join '-' }
$OutputObject = @()
foreach ($ProductFamilyAssumedLink In $ProductFamiliesAssumedLinks) {
[System.Uri]$Uri = "${BaseURI}/${ProductFamilyAssumedLink}.html"
Write-Verbose "Uri: ${Uri}"
$WebRequest = Invoke-WebRequest -Uri $Uri
$DownloadLinks = $WebRequest.ParsedHtml.getElementsByTagName('a') | Where-Object className -eq 'splunk-btn sp-btn-solid sp-btn-pink' | FilterByProduct | FilterByPlatform | FilterByArchitecture
foreach ($DownloadLink In $DownloadLinks) {
$Properties = [ordered]@{
Product = ($DownloadLink.getAttribute('data-product') | Out-String).Trim()
Version = ($DownloadLink.getAttribute('data-version') | Out-String).Trim()
Architecture = ($DownloadLink.getAttribute('data-arch') | Out-String).Trim()
Platform = ($DownloadLink.getAttribute('data-platform') | Out-String).Trim()
OPlatform = ($DownloadLink.getAttribute('data-oplatform') | Out-String).Trim()
FileName = ($DownloadLink.getAttribute('data-filename') | Out-String).Trim()
Link = ($DownloadLink.getAttribute('data-link') | Out-String).Trim()
SHA512 = ($DownloadLink.getAttribute('data-sha512') | Out-String).Trim()
MD5 = ($DownloadLink.getAttribute('data-md5') | Out-String).Trim()
WGet = ($DownloadLink.getAttribute('data-wget') | Out-String).Trim()
XProduct = $null
XVersion = $null
XHash = $null
XPlatform = $null
XArchitecture = $null
XFileExtension = $null
}
$FileNameArray = $Properties.FileName.split('-|.')
$Properties.XProduct = $FileNameArray[0]
$Properties.XVersion = '{0}.{1}.{2}' -f $FileNameArray[1], $FileNameArray[2], $FileNameArray[3]
$Properties.XHash = $FileNameArray[4]
if ($Properties.FileName -match "^(?'XProduct'\w+)-(?'XVersion'\d+\.\d+\.\d+)-(?'XHash'[a-zA-Z0-9]+)\.(?'XArchitecture'\w+)(?'XFileExtension'.\w+)$") {
# splunkforwarder-X.X.X-6b4ebe426ca6.aarch64.rpm
$Properties.XPlatform = $Properties.Platform
$Properties.XArchitecture = $matches.XArchitecture
$Properties.XFileExtension = $matches.XFileExtension
}
elseif ($Properties.FileName -match "^(?'XProduct'\w+)-(?'XVersion'\d+\.\d+\.\d+)-(?'XHash'[a-zA-Z0-9]+)-(?'XPlatform'\w+)\-(?'XArchitecture'\w+)(?'XFileExtension'.\w+)$") {
# splunkforwarder-X.X.X-6b4ebe426ca6-freebsd13-amd64.txz
$Properties.XPlatform = $matches.XPlatform
$Properties.XArchitecture = $matches.XArchitecture
$Properties.XFileExtension = $matches.XFileExtension
}
elseif ($Properties.FileName -match "^(?'XProduct'\w+)-(?'XVersion'\d+\.\d+\.\d+)-(?'XHash'[a-zA-Z0-9]+)-(?'XPlatform'\w+)\-(?'XArchitecture'\w+)(?'XFileExtension'.*)$") {
# splunkforwarder-X.X.X-6b4ebe426ca6-freebsd13-amd64.txz.Z
$Properties.XPlatform = $matches.XPlatform
$Properties.XArchitecture = $matches.XArchitecture
$Properties.XFileExtension = $matches.XFileExtension
}
$OutputObject += [PSCustomObject]$Properties
}
}
$OutputObject | FilterByXProduct | FilterByXPlatform | FilterByXArchitecture
}
$SplunkDownloadSplat = @{
Product = 'universalforwarder'
Platform = 'windows'
Architecture = 'x86_64'
XProduct = 'splunkforwarder'
XPlatform = 'windows'
XArchitecture = 'x64'
# Verbose = $true
}
$SplunkDownload = Get-SplunkDownload @SplunkDownloadSplat | Where-Object XFileExtension -eq '.msi' | Select-Object -First 1
$MyPSScriptRoot = $PSScriptRoot
if (!$MyPSScriptRoot) { $MyPSScriptRoot = $PWD.Path }
$StartProcessSplat = @{
FilePath = "$($MyPSScriptRoot)\wget.exe"
ArgumentList = @(
$SplunkDownload.WGet.TrimStart('wget').Trim()
'--no-check-certificate'
)
}
Start-Process @StartProcessSplat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment