Analyse Redis cache usage with PowerShell

At the load tests I need to analyse communication between web servers and Redis Cache. My target was to have a statistics with most accessed cache keys. To get required stasticts I run MONITOR command to collect data from Redis cache usage to file using redis-cli command:

.\redis-cli.exe -h <redis_host> -a <redis_access_key> monitor > monitor.log


Udemy course: Improve your productivity with PowerShell

Then I run load test to create traffic on web servers. After the load test I stop MONITOR command using CTRL+C. Statistics is created from usage of GET command. Raw monitor.log file is processed by following PowerShell command:

Get-Content .\monitor.log `
 | Select-String " ""GET"" " `
 | ForEach-Object { $_.ToString().Split(@(" ""GET"" "), [System.StringSplitOptions]::None)[1] } `
 | Group-Object `
 | Sort-Object -Property Count -Descending `
 | Format-Table -Property Count,Name

Result of PowerShell command is summary statistics with most accessed cache keys:

Count Name
----- ----
 2570 "OrderService_OrderService_Orders_STATE"
 2570 "OrderService_OrderService_Orders"
  261 "Test_a2/product/detail/1"
  225 "Test_a2/product/detail/1HQNnoneV+n+FCDE"
  211 "Test_a2/product/category/1"
  211 "ProductService_ProductService_Product_1_STATE"
  202 "Test_a2/product/detail/1"
  196 "ProductService_ProductService_Product_1"   

If you are interested in PowerShell automation, take my Udemy course Improve your productivity with PowerShell.

Restore Microsoft SQL Server database using PowerShell

In previous post I wrote about the backup of Microsoft SQL Server database using PowerShell. Opposed operation to backup is restore, which can be executed using following PowerShell script:

param
(
    [string] $backupFile = $(throw "Backup file is required"),
    [string] $databaseName = $(throw "Database name is required")
)

$server = "."

Import-Module SQLPS

Invoke-Sqlcmd -ServerInstance $server -Query "ALTER DATABASE $databaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
Invoke-Sqlcmd -ServerInstance $server -Query "RESTORE DATABASE $databaseName FROM DISK='$backupFile' WITH REPLACE"
Invoke-Sqlcmd -ServerInstance $server -Query "ALTER DATABASE $databaseName SET MULTI_USER WITH ROLLBACK IMMEDIATE"


Udemy course: Improve your productivity with PowerShell

At the first step database is set to single user mode. Next the restore of database is executed. After restore the database is set back to multi user mode.

If you are interested in PowerShell automation, take my Udemy course Improve your productivity with PowerShell.

Backup Microsoft SQL Server database using PowerShell

During development we need often create backup of Microsoft SQL Server database. Performing this task using SSMS UI dialog is slow and time consuming activity. This task can be automatized by following PowerShell script:

param
(
    [string] $databaseName = $(throw "Database name is required"),
    [string] $backupToDir = ".",
    [bool] $shrinkLogFile = $true
)

Import-Module SQLPS

if ($backupToDir -eq ".")
{
    $backupToDir = (Get-Item .).FullName + "\Backups\"

    if (-not (Test-Path $backupToDir))
    {
        New-Item -Path $backupToDir -ItemType Directory | Out-Null
    }
}

if ($shrinkLogFile)
{
    $shrinkCommand = "DBCC SHRINKFILE (" + $databaseName + "_log, 0)"
    Invoke-Sqlcmd -ServerInstance . -Database $databaseName -Query $shrinkCommand | Out-Null
}

$now = (Get-date).ToString("yyyyMMddHHmmss")
$backupCommand = "BACKUP DATABASE " + $databaseName + " TO DISK='" + $backupToDir + $databaseName + "_" + $now + ".bak' WITH COMPRESSION"

Invoke-Sqlcmd -ServerInstance . -Query $backupCommand


Udemy course: Improve your productivity with PowerShell

The script shrinks database log file and backup is created with compression to decrease the size of backup file.

If you are interested in PowerShell automation, take my Udemy course Improve your productivity with PowerShell.

Analyse log files using PowerShell

PowerShell is great tool for analysing log files. Using few PowerShell cmdlets pipelined together we can find errors:

Get-Content *.log | Select-String error | Sort-Object | Get-Unique | Out-File error.log -Width 1000 -Encoding UTF8


Udemy course: Improve your productivity with PowerShell

Purpose of cmdlets is following:

This simple, but powerfull one line code snippet can help find specific items in log files fast and effectively.

If you are interested in PowerShell automation, take my Udemy course Improve your productivity with PowerShell.