Tag: PowerShell

Azure FunctionsVisual Studio

Azurite emulator cannot be started

After installing Visual Studio 2022 and working with Azure Functions I noticed that a new storage emulator is being used called Azurite.

Azurite is an open source Azure Storage API compatible server (emulator). Based on Node.js, Azurite provides cross platform experiences for customers wanting to try Azure Storage easily in a local environment. Azurite simulates most of the commands supported by Azure Storage with minimal dependencies.

https://github.com/Azure/Azurite

This seemed to replace the old Azure Storage Emulator you would run previously when doing local development. I quickly came across an issue where the Azurite emulator cannot be started because port 10000 is already in use. This is also applied to ports 10001 and 10002 which it uses. Here are the contents of the Service Dependencies from the Visual Studio 2022 Output pane:

Ensuring Azure Functions Core Tools are up to date. This may take a few minutes...
Azure Functions Core Tools are up to date.
DotNetCore31-FunctionApp: Azurite emulator cannot be started because port 10000 is already in use. Another instance of the Azurite emulator or Azure Storage emulator might be already running on your machine.
DotNetCore31-FunctionApp: We detected that Azure Storage emulator is running on your machine. The Azure Storage emulator is now deprecated. Microsoft recommends that you use the Azurite emulator for local development with Azure Storage. Follow the directions in the link 'https://go.microsoft.com/fwlink/?LinkID=2167087' to install and run Azurite emulator.
Unable to start dependency 'functions.storage1'.
Ensuring Azure Functions Core Tools are up to date. This may take a few minutes...
Azure Functions Core Tools are up to date.
Ensuring Azure Functions Core Tools are up to date. This may take a few minutes...
Azure Functions Core Tools are up to date.
DotNetCore31-FunctionApp: Azurite emulator cannot be started because port 10000 is already in use. Another instance of the Azurite emulator or Azure Storage emulator might be already running on your machine.
DotNetCore31-FunctionApp: We detected that Azure Storage emulator is running on your machine. The Azure Storage emulator is now deprecated. Microsoft recommends that you use the Azurite emulator for local development with Azure Storage. Follow the directions in the link 'https://go.microsoft.com/fwlink/?LinkID=2167087' to install and run Azurite emulator.
Unable to start dependency 'storage1'.
DotNetCore31-FunctionApp: Azurite emulator cannot be started because port 10000 is already in use. Another instance of the Azurite emulator or Azure Storage emulator might be already running on your machine.
DotNetCore31-FunctionApp: We detected that Azure Storage emulator is running on your machine. The Azure Storage emulator is now deprecated. Microsoft recommends that you use the Azurite emulator for local development with Azure Storage. Follow the directions in the link 'https://go.microsoft.com/fwlink/?LinkID=2167087' to install and run Azurite emulator.
Unable to start dependency 'storage1'.

Let’s drop into Windows Terminal and take a look at what process is using that port:

Get-Process -Id (Get-NetTCPConnection -LocalPort 10002).OwningProcess

After stopping the Node process and re-running Azurite (I restarted Visual Studio) we can see everything starts up as expected:

Ensuring Azure Functions Core Tools are up to date. This may take a few minutes...
Azure Functions Core Tools are up to date.
DotNetCore31-FunctionApp: azurite.cmd --location "C:\Users\ccampbell\AppData\Local\Temp\Azurite" --debug "C:\Users\ccampbell\AppData\Local\Temp\Azurite\debug.log"
DotNetCore31-FunctionApp: Azurite Blob service is starting at http://127.0.0.1:10000
DotNetCore31-FunctionApp: Azurite Blob service is successfully listening at http://127.0.0.1:10000
DotNetCore31-FunctionApp: Azurite Queue service is starting at http://127.0.0.1:10001
DotNetCore31-FunctionApp: Azurite Queue service is successfully listening at http://127.0.0.1:10001
DotNetCore31-FunctionApp: Azurite Table service is starting at http://127.0.0.1:10002
DotNetCore31-FunctionApp: Azurite Table service is successfully listening at http://127.0.0.1:10002

This was not a great experience on the first day I started to use Visual Studio 2022 with Azure Functions as I had to go off and figure out why the Azure emulator could not be started instead of just working on my application. You can go and change the default ports ft you like which is mentioned in the documentation. For more information on Azurite check out the docs on their GitHub repository.

I hope this helps with anyone new to the Azurite emulator in Visual Studio 2022.

Enjoy!

References

https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azurite?tabs=visual-studio

https://github.com/Azure/Azurite

Development

Creating a Windows Service Using PowerShell

Last week I was looking at using PowerShell for creating and starting a Windows Service.

Usually I have a batch program for installing and uninstalling my services. As you can see, my Batch script is very basic, but works:

image

This type of script has worked for me for years but I’ve always wanted to learn PowerShell and figured this is a good time to do so and why not migrate to using PowerShell for installing my Windows Services.

After reading through the PowerShell documentation, Core Cmdlets and playing around with the PowerShell ISE tool (part of PowerShell 3), I found I was still lacking the knowledge to do this properly and since I was also learning PowerShell, I decided to see if anyone else had troubles with this or had insight and behold they did!

One of the best articles I came across nailed it perfectly with decent information on interacting with Windows Services from PowerShell and then a detailed walkthrough and example on exactly how to do it.

Referenced article: Create a Windows Service using PowerShell

Thanks, you saved me a lot of time.

Here is what I created for PowerShell scripts

After creating the Install.ps1 and Uninstall.ps1 files, to install your service, just right click on  Install.ps1 file and click on Run with PowerShell:

image

This will check if your service is already installed and if so, stop and uninstall your service. Then it will proceed with installing and started your service.

You can then confirm your service is installed and running by running the following Cmdlet as shown below:

Get-Service –Name “YourServiceName”

image

And voila, you can see that your service is installed and running. Pretty simple.

I will say that the scripts are far more complex than their original batch scripts, but as you will find, you have much more control and there is a lot you can do in PowerShell. It more than makes up for the extra effort.

Give it a try if you haven’t already started to use PowerShell. You may find you like it and that it can help in ways you never thought possible.

Below are each of the scripts.

Enjoy!

 

Install.ps1

$serviceName = "WorkManagerDemoService"
$serviceDisplayName = "WorkManager Demo Service"
$serviceDescription = "WorkManager Demo Service"
$serviceExecutable = "WindowsService1.exe"

# verify if the service already exists, and if yes remove it first
if (Get-Service $serviceName -ErrorAction SilentlyContinue)
{
    "service already installed, stopping…"
    # using WMI to remove Windows service because PowerShell does not have CmdLet for this
    $serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name=’$serviceName’"
    $serviceToRemove | Stop-Service
    $serviceToRemove.delete()
    "service removed"
}
else
{
    # just do nothing
    "service does not exist"
}

"installing service…"

# detect current execution directory
$directoryPath = Split-Path $MyInvocation.MyCommand.Path

# creating credentials which can be used to run my windows service
#$secpasswd = ConvertTo-SecureString "MyPa$$word" -AsPlainText -Force
#$mycreds = New-Object System.Management.Automation.PSCredential (".\MyUserName", $secpasswd)
# OR
#$myCredentials = Get-Credential

$binaryPath = $directoryPath + "\" + $serviceExecutable

# creating widnows service using all provided parameters, -displayName $serviceName, -credential $myCredentials
New-Service -name $serviceName -displayName $serviceDisplayName -binaryPathName $binaryPath -startupType Automatic -Description $serviceDescription

Start-Service -Name $serviceName

Get-Service $serviceName

"installation completed"

Pause

 

Uninstall.ps1

$serviceName = "WorkManagerDemoService"

# verify if the service already exists, and if yes remove it
if (Get-Service $serviceName -ErrorAction SilentlyContinue)
{
    "service already installed, stopping…"
    # using WMI to remove Windows service because PowerShell does not have CmdLet for this
    $serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name=’$serviceName’"
    $serviceToRemove | Stop-Service
    $serviceToRemove.delete()
    "service removed"
}
else
{
    # just do nothing
    "service does not exist"
}

Pause

Development

Calculating Bitmasks in PowerShell

Excellent post on calculating bitmasks in PowerShell…

http://www.jasonstrate.com/2013/02/calculate-bitmasks-in-powershell/