Skip to content

Instantly share code, notes, and snippets.

@DerfOh
Created October 8, 2022 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DerfOh/0de8f892490727d9e7cce8fa411dd7a2 to your computer and use it in GitHub Desktop.
Save DerfOh/0de8f892490727d9e7cce8fa411dd7a2 to your computer and use it in GitHub Desktop.
installs windows prometheus exporter
$version = "0.20.0"
$AgentURL = "https://github.com/prometheus-community/windows_exporter/releases/download/v${version}/windows_exporter-${version}-amd64.exe"
$AbsoluteCurrPath = $(Get-Location).Path
$ExporterDirectoryToCreate = "C:\Program Files\windows_exporter\"
$AbsolutePathExe = "${ExporterDirectoryToCreate}\windows_exporter.exe"
$EnabledCollectors = "cpu,cs,logical_disk,memory,net,os,service,system,tcp,remote_fx,logon,process"
$ServiceName = "windows_exporter"
# create exporter directory
if (-not (Test-Path -LiteralPath $ExporterDirectoryToCreate)) {
try {
New-Item -Path $ExporterDirectoryToCreate -ItemType Directory -ErrorAction Stop | Out-Null #-Force
} catch {
Write-Error -Message "Unable to create directory '$ExporterDirectoryToCreate'. Error was: $_" -ErrorAction Stop
}
"Successfully created directory '$ExporterDirectoryToCreate'."
} else {
"Directory already exists"
}
# download specify msi to temp directory
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest $AgentURL -OutFile $AbsolutePathExe
# remove the previous MSI install if it exists
Uninstall-Package -Name windows_exporter
# open firewall rules
New-NetFirewallRule -Name "windows_exporter" -DisplayName "windows_exporter port 9182" -Direction inbound -Profile Any -Action Allow -LocalPort 9182 -Protocol TCP
$rule = "windows_exporter port 9182"
Get-NetFirewallRule -DisplayName $rule | ft -Property Name, DisplayName, @{Name='Protocol';Expression={($PSItem | Get-NetFirewallPortFilter).Protocol}}, @{Name='LocalPort';Expression={($PSItem | Get-NetFirewallPortFilter).LocalPort}}, @{Name='RemotePort';Expression={($PSItem | Get-NetFirewallPortFilter).RemotePort}}, @{Name='RemoteAddress';Expression={($PSItem | Get-NetFirewallAddressFilter).RemoteAddress}}, Enabled, Profile, Direction, Action
# Check Status of Service
$Service = Get-Service -Name "$ServiceName"
if($Service.Status -eq "running"){
Write-Host "$ServiceName is running, stopping..."
Stop-Service -Name $ServiceName
Write-Host "Removing $ServiceName"
sc.exe delete $ServiceName
} else {
Write-Host "$ServiceName status is: $Service.Status"
Write-Host "Removing $ServiceName"
sc.exe delete $ServiceName
}
# enable the service
New-Service -name "$ServiceName" `
-BinaryPathName "${AbsolutePathExe} --collectors.enabled `"${EnabledCollectors}`" --collector.service.services-where `"Name LIKE 'windows_%' OR Name LIKE 'WMI%'`"" `
-StartupType Automatic `
-DisplayName "$ServiceName"
# start the service
Start-Service -Name $ServiceName
# Check Status of Service
if($Service.Status -eq "running"){
Write-Host "$ServiceName is running"
} else {
Write-Host "$ServiceName status is: $Service.Status"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment