Manage Windows service using PowerShell

Windows service can be managed using few PowerShell cmdlets.


Udemy course: Migrate Windows service to Azure

Install Windows service into operating system:

New-Service -BinaryPathName .\PathTo\MyWinService.exe -Name MyWinService `
            -DisplayName "My Windows Service" -StartupType Automatic

List installed Windows services using wildcard:

Get-Service *my*

Get Windows service by name:

Get-Service MyWinService

Start Windows service:

Start-Service MyWinService

or:

Get-Service MyWinService | Start-Service

Run Service Management Console to check in UI whether Windows service is installed and running:

services.msc

Stop Windows service:

Stop-Service MyWinService

or:

Get-Service MyWinService | Stop-Service

Uninstall Windows service from operating system in PowerShell 5.0 or previous:

(Get-WmiObject -Class Win32_Service -Filter "Name='MyWinService'").Delete()

Uninstall Windows service from operating system in PowerShell 6.0 or later:

Remove-Service MyWinService

If you are interested in Windows services and how to migrate them to Azure, take my Udemy course Migrate Windows service to Azure.

Deploy Azure WebJob using PowerShell

There are many posts on thema of Azure WebJob deployment using PowerShell. But I try to find the most simple solution possible and it is using the combination of PowerShell and Azure CLI.


Udemy course: Migrate Windows service to Azure

Following PowerShell script deploys Azure WebJob:

param
(
    [string] $buildOutput = $(throw "Directory with build output is required"),
    [string] $resourceGroupName = $(throw "Resource group name is required"),
    [string] $webAppName = $(throw "Web app name is required"),
    [string] $webJobName = $(throw "Web job name is required"),
    [string] $webJobType = "triggered"
)

$currentDir = (Get-Item .).FullName
$tempDir = "$currentDir\Temp"
$webJobDir = "$tempDir\App_Data\jobs\$webJobType\$webJobName"

New-Item $webJobDir -ItemType Directory
Copy-Item "$buildOutput\*" -Destination $webJobDir -Recurse
Compress-Archive -Path "$tempDir\*" -DestinationPath ".\$webJobName.zip"
Remove-Item $tempDir -Recurse -Force

az webapp deployment source config-zip -g $resourceGroupName -n $webAppName --src "$webJobName.zip"

PowerShell script has following parameters:

  • $buildOutput – Azure WebJob build output, in my case it is bin\Release\net472 folder
  • $resourceGroupName – Azure resource group name
  • $webAppName – Azure Web App name running on Azure App Service
  • $webJobName – Azure WebJob name
  • $webJobType – Azure WebJob type (triggered/continuous)

PowerShell creates temp directory and copy build output to the temp directory. The key part here is to use defined directory structure App_Data\jobs\$webJobType\$webJobName to deploy Azure WebJob to valid directory in Azure WebApp. Then create ZIP archive and remove temp directory. Then deploys Azure WebJob using Azure CLI command az webapp deployment to Azure.

If you are interested in Azure WebJobs and how to use them for Windows service migration to Azure, take my Udemy course Migrate Windows service to Azure where you learn more about Azure WebJob implementation, deployment, configuration and monitoring.

Migrate Windows service to Azure

My third Udemy course https://www.udemy.com/course/migrate-windows-service-to-azure covers the thema of Windows service migration to Azure. Taking this course you learn:

  • Cloud service models
  • Benefits of migration to cloud
  • Alternatives for migration
  • Code changes
  • Preparing infrastructure (Azure Portal, Azure PowerShell, Azure CLI, ARM templates)
  • Deployment (Azure Portal, PowerShell)
  • Configuration (.config transformations, Azure Portal)
  • Logging and monitoring (Azure Application Insights)
  • Other options (Azure Functions, Azure Automation, Azure SQL Database Jobs, Hangfire)

Git for busy TFS users

I have 10+ years of experience with TFS and now I have an project which uses Git as source control system. I decided to wrote some notes which can help another experienced TFS users to start with Git. Key differences:

  • Git is decentralized, while TFS has one central repository on TFS server. As for the Git there is one remote repository and then local repositories on developer machines. Due to this, the sync of commits between the repositories is needed.
  • Git has commits, while in TFS we have changesets.
  • Git branches are lightweight, while TFS branches allocates much more disk space.
  • TFS get latest version = Git fetch + pull
  • TFS check-in = Git commit + push

Next I will show some of the basic commands needed for the daily work. We start with cloning remote reposity using command:

git clone <repository_url>

Check the status and display current active branch:

git status

Create new branch:

git checkout -b <branch_name>

Track new file:

git add <file_name>

Commit to local repository:

git commit -m "My commit message"

Push to remote repository:

git push

Display list of last 5 commits:

git log -5

Display list of branches:

git branch

Merge from <other_brach> to current active branch:

git merge <other_branch>

Download commits from remote repository to local repository:

git fetch origin <branch_name>

Apply commits to local repository:

git pull

In this short post I used only command line commands, but this illustrates basic Git usage which can be also done with help of UI.

Run T-SQL query parallel using PowerShell

T-SQL query can be run parallel using combination of PowerShell Workflows and Invoke-Sqlcmd cmdlet as show in following code:

param
(
    [string] $query = $(throw "Query is required"),
    [string] $database = $(throw "Database name is required"),
    [int] $parallelCount = 10
)

workflow Invoke-SqlcmdParallel
{
    param
    (
        $query,
        $parallelCount,
        $database
    )

    $numbers = [System.Linq.Enumerable]::Range(1, $parallelCount)

    foreach -parallel ($number in $numbers)
    {
        Invoke-Sqlcmd -Database $database -Query $query
    }
}

Invoke-SqlcmdParallel -query $query -parallelCount $parallelCount -database $database

Using this script you can easily simulate concurrency.

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


Udemy course: Improve your productivity with PowerShell