Created
November 14, 2018 05:38
-
-
Save glennsarti/3f930b9100c9da56a23b5f460fbbcc95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Script:PuppetConsoleURI = 'https://fill.this.in' # e.g. 'https://peconsole.company.local'; | |
$Script:PuppetConsoleUsername = 'username'; | |
$Script:PuppetConsolePassword = 'password' # Cleartext | |
Function Get-PuppetLoginToken() { | |
$body = @{ | |
'username' = $Script:PuppetConsoleUsername; | |
'password' = $Script:PuppetConsolePassword; | |
'redirect' = '/' | |
} | |
$sv = $null | |
$contentType = 'application/x-www-form-urlencoded' | |
$result = Invoke-WebRequest -Uri "$($Script:PuppetConsoleURI)/auth/login" -ContentType $contentType ` | |
-Method Post -Body $body -SessionVariable sv -SkipCertificateCheck | |
$body = $null | |
$token = $null | |
$result.Headers.'Set-Cookie' | % { | |
if ($_.SubString(0,8) -eq 'pl_ssti=') { $token = ($_.SubString(8) -split ';',2)[0]} | |
} | |
$sv.Headers.Add('X-Authentication', $Token + '|no_keepalive') | |
$script:PuppetRequestSession = $sv | |
} | |
Function Script:Invoke-PuppetRequest($URI, $Method = 'GET', $Body = $null) { | |
$iwrArgs = @{ | |
'URI' = "$($Script:PuppetConsoleURI)$URI"; | |
'Method' = $Method; | |
'WebSession' = $script:PuppetRequestSession; | |
'UseBasicParsing' = $true; | |
} | |
if ($Body -ne $null) { $iwrArgs.Add('Body', $Body) } | |
$oldPreference = $progressPreference | |
$progressPreference = 'SilentlyContinue' | |
$result = Invoke-WebRequest @iwrArgs -SkipCertificateCheck | |
$progressPreference = $oldPreference | |
Return $result.Content | |
} | |
Function Get-AllPuppetNodes() | |
{ | |
$obj = New-Object -TypeName System.Collections.ArrayList | |
# Only fetch up to 2000 nodes | |
$Maximum = 2000 | |
# Get 100 nodes at a time | |
$OffsetBlock = 100 | |
$offset = 0 | |
$returnedObjects = 0 | |
Write-Progress -Activity 'Loading Nodes' -PercentComplete 0 | |
$body = @{ | |
'filter' = (@{} | ConvertTo-Json -Depth 5); | |
'limit' = $OffsetBlock; | |
'offset' = $offset; | |
} | |
do { | |
$body.offset = $offset; | |
$result = (Invoke-PuppetRequest -URI '/api/cm/nodes' -Body $body | ConvertFrom-JSON) | |
$totalNodes = $result.meta.total | |
if ($totalNodes -le 1) { $totalNodes = $Maximum } | |
Write-Progress -Activity 'Loading Nodes' -PercentComplete ($offset/$totalNodes * 100) | |
$returnedObjects = 0 | |
$result.'cm/nodes' | % { | |
Write-Output $_ | |
$returnedObjects = $returnedObjects + 1 | |
} | |
$offset = $offset + $OffsetBlock | |
} while (($returnedObjects -eq $OffsetBlock) -and ($obj.Count -le $Maximum)) | |
Write-Progress -Activity 'Loading Nodes' -Completed | |
} | |
# Useful stuff is here ----v | |
Get-PuppetLoginToken | |
Get-AllPuppetNodes | ? { ($_.Status -eq 'unreported') -or ($_.Status -eq 'failure') -or ($_.ReportedAt -lt (Get-Date).AddDays(-5)) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment