PowerShell

Nice to Know – Running RestPS as a Service

Overview

We have been using RestPS for a while, it is a very nice solution, it is basically a PowerShell script that when running act as a Restful API. It gives us the ability to use RestPS as a part of deploying computers in MDT/ConfigMgr or basically any solution, without writing a WebServices for each and every situation. The RestPS solution has the ability to execute powershell scripts remotely using Invoke-RestMethod from the client side, it means that we can do the heavy lifting  on the server side. Check out RestPS here https://github.com/jpsider/RestPS

The super nice thing with RestPS

Here is a great example, you are deploying devices, you would like to check if the model is approved in your list, or you want to check if the computer name is already in use, or maybe you need to upload information from the client that moves a computer from one OU to another OU. Running such script on the Computer it self is bad, from many reasons, security is one. Handing it over to a server is better, the “normal” way is to start Visual Studio and start typing, or buy software. RestPes can run a script on the server when called from the client using Invoke-RestMethod, that makes this very easy and possible for many customers that don’t have the ability to write code

The Challenge

Easy, RestPS is a PowerShell script that start the weblistner, but it is a script, therefore it is tricky to restart the server, you need to logon after reboot and start the script, I don’t want do do that.

The solution

Easy as well, just flip the script to a Windows Service solves the problem

Installing RestPS

You need to run the latest version of NuGet and PowerShell get before you can install RestPS

Install the Nuget Providor, PowerShellGet and update all modules

Start an elevated PowerShell prompt and execute the following:

# Install NuGet Provider
Install-PackageProvider -Name NuGet -Force -Verbose

# Install PowerShellGet
Install-Module -Name PowerShellGet -Force -SkipPublisherCheck -Verbose

# Update all modules
Update-Module -Force –Verbose

# Close PowerShell prompt
Exit

Configure RestPS

This blog post is not about doing fancy stuff with RestPS, that is something we can do later, so we will just use the default configuration and verify that it works.

Install the Module

# Get the Module
Find-Module -Name RestPS | Install-Module -Verbose -SkipPublisherCheck –Force

Intitial Configuration of RestPS

The basic step is to import the module and invoke the initial sample scrips folder

# Import Module
Import-Module RestPS -Verbose -Force

# Initual Configuration
Invoke-DeployRestPS -LocalDir ‘C:\RestPS’ –Verbose

Start the WebListner

Using another PowerShell command windows, execute the following:

# Start the RestPS listner
$RestPSparams = @{
            RoutesFilePath = ‘C:\RestPS\endpoints\RestPSRoutes.json’
            Port = ‘8080’
         }
Start-RestPSListener @RestPSparams

clip_image001
The RestPS is running.

Test RestPS

Using another PowerShell prompt, execute the following

# Verify that RestPS works
$RestMethodParams = @{
            Uri = ‘
http://localhost:8080/process?name=powershell’
             Method = ‘Get’
            UseBasicParsing = $true
        }
Invoke-RestMethod @RestMethodParams

clip_image002
Results from running the Invoke-RestMethod.

Turning RestPS into a Service

Install Choclaty and NSSM

From PowerShell, execute the following:

# Install Choclaty
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1′))

Install NSSM

Make sure you do this when all other PowerShell Windows are closed and not from ISE!

# Install NSSM
choco install nssm

Create a folder for the Service

Not needed, but it will be easier, the script and the logs will end up here, as well as the scripts that starts it. Create C:\RestPSService and in the folder create a PowerShell start script that looks like this with the name StartRestPS.ps1

Start-Transcript -Path C:\RestPSService\Start.log
Import-Module RESTPS -Verbose -Force
Start-RestPSListener -Port 8080 -LogLevel ALL -Logfile C:\RestPSService\RestPS.log -Verbose
Stop-Transcript -Path C:\RestPSService\Start.log

Test the script

Test the script by running it from a prompt

image
Testing the script, looks fine.

Close the PowerShell prompt, we now know it is working.

Make RestPS a Service using NSSM

Pretty straightforward, run the following commands

# Make RestPS a Service
$NSSMPath = (Get-Command “C:\ProgramData\chocolatey\bin\nssm.exe”).Source
$PoShPath = (Get-Command powershell).Source

$NewServiceName = “RestPS”
$PoShScriptPath = “C:\RestPSService\StartRestPS.ps1”
$args = ‘-ExecutionPolicy Bypass -NoProfile -File “{0}”‘ -f $PoShScriptPath

& $NSSMPath install $NewServiceName $PoShPath $args
& $NSSMPath status $NewServiceName

# Change the name of the Services
& $NSSMPath set $NewServiceName description “RestFul API Services”

# Check the Services
Get-Service -Name RestPS

# Start the Services
Get-Service -Name RestPS | Start-Service

image
Here is our little Service.

That’s it, let us try it out

Execute the following:

# Verify that RestPS works
$RestMethodParams = @{
            Uri = ‘
http://localhost:8080/process?name=powershell’
              Method = ‘Get’
            UseBasicParsing = $true
        }
Invoke-RestMethod @RestMethodParams

That’s it,

Now you know how to flip it to a services, but you need more to make use of the services, and here are the links:

You can download all commands from my GitRepo here https://github.com/DeploymentBunny/Files/tree/master/Tools/RestPSFiles

Happy OSD

/mike

Categories: PowerShell, Uncategorized

Tagged as:

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.