Skip to content

Instantly share code, notes, and snippets.

@rohit-lakhanpal
Created November 3, 2016 23:57
Show Gist options
  • Save rohit-lakhanpal/7556526e8e1e5d17e9d62fbd1c2f428d to your computer and use it in GitHub Desktop.
Save rohit-lakhanpal/7556526e8e1e5d17e9d62fbd1c2f428d to your computer and use it in GitHub Desktop.
This PowerShell script pretty prints JSON values from a REDIS cache.
# Define redis host & auth credentials
$redisHost = "redis.local"
$redisAuth = "redispwd"
# Define per site variables stored in redis
$sites = @("server-01","server-01")
# Define function to Pretty Print Json (and replace escaped json string)
function CleanJson($data)
{
return $data -replace '\"','"' | ConvertFrom-Json | ConvertTo-Json
}
# Echo startup time
Write-Output "`n`n`n> Begin value check at $(Get-Date)"
foreach ($site in $sites) {
# Echo output for each site
Write-Output "> Checking $site"
# Check value for sitename_key1 & sitename_key2
$cmd1 = "& 'C:\Program Files\Redis\redis-cli.exe' -h $redisHost -a $redisAuth get $site" + "_key1"
$cmd2 = "& 'C:\Program Files\Redis\redis-cli.exe' -h $redisHost -a $redisAuth get $site" + "_key2"
$otp1=Invoke-Expression $cmd1
$otp2=Invoke-Expression $cmd2
# Print key values
Write-Output $site"_key1: $otp1"
# As key2 value is a JSON payload call CleanJson to unescape & pretty print
Write-Output $site"_key2: $(CleanJson($otp2))"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment