Amit Verma
Kilo Patron
Kilo Patron

Introduction to PowerShell

PowerShell is a scripting language and command-line shell designed by Microsoft. It is built on the .NET framework and is primarily used for task automation and configuration management. PowerShell enables IT professionals and developers to control and automate the administration of Windows and other systems.

Key Features of PowerShell

  1. Cmdlets: PowerShell commands, referred to as cmdlets, are unique .NET classes designed to execute specific tasks. Examples include Get-Process, Set-Item, and Get-Help.
  2. Pipelines: PowerShell allows the output of one cmdlet to be passed as input to another, enabling complex operations to be performed in a streamlined manner.
  3. Scripting: PowerShell scripts are text files with a .ps1 extension. These scripts can automate repetitive tasks, manage system configurations, and perform batch processing.
  4. Remote Management: PowerShell supports remote management, allowing administrators to execute commands on remote systems using PowerShell Remoting.
  5. Object-Oriented: Unlike traditional command-line interfaces, PowerShell outputs objects rather than plain text, providing more flexibility in handling and manipulating data.

Let's Build a PowerShell Script

For this blog series, we will work on a use case -  Check Service Status and Restart if Stopped. Let us build a PowerShell script for this use case and integrate it with a ServiceNow Flow Action. To build the PowerShell script, we will make use of PowerShell Integrated Scripting Environment - PowerShell ISE  (An IDE for PowerShell Script Development).

 

To open PowerShell ISE, click on Windows Key -> Search for PowerShell ISE -> Click on PowerShell ISE. Alternatively, you can press Windows+r key together. In the run dialog box, type powershell_ise and press enter key. The PowerShell ISE will open.

AmitVerma_0-1742791245856.pngAmitVerma_1-1742791315797.png

 

Let's get started by building our PowerShell script in the Script Pane (The white area where we will type our cmdlets). As our requirement is to check if a Service is stopped and and restart it if yes, we will start our script by checking the current status of the script. We will make use of Get-Service cmdlet to get the particular service and make use of Pipeline operator with Select-Object cmdlet to get the status of the service and store the output in a variable $serviceStatus. As the Service name will vary dynamically, we will make use of a PowerShell variable $serviceName to store the Service Name.

AmitVerma_2-1742791767843.png

 

Next, we will make use of an if condition to check if service status is stopped. If Yes, we will make use of Restart-Service cmdlet to restart it else no action will be taken. Finally, we will check and return the service status so that we can take further decisions. Our final PowerShell Script will look as below:

AmitVerma_3-1742792496682.png
## Get the Current Status of the Service ##
$serviceStatus = (Get-Service $serviceName|Select-Object Status).Status

## Check if the service is stopped ##
if($serviceStatus -eq "Stopped"){
   Restart-Service -Name $serviceName
}

## Return current status of the service ##
return (Get-Service $serviceName|Select-Object Status).Status

Integrating PowerShell Script with Flow Designer Action

We will now integrate this PowerShell script with an Action by creating a new action. Navigate to Flow Designer -> Click New-> Select Action. We will name this action as Restart Stopped Service and click on Build Action.

AmitVerma_4-1742792874140.png

 

Once the action is being build, we will create an input variable to capture Service Name. Under the input tab, click on Create Input button.

AmitVerma_5-1742792960218.png

Create an Input as shown below. Notice that the Variable Name is auto-populated when you type the Variable Label. We will use this variable name in our PowerShell Script in the next steps.

AmitVerma_6-1742793007358.png

Next, we will make use PowerShell step to run our script. Click on + icon to add a new step and search for PowerShell. Add PowerShell step to your action by clicking on it.

AmitVerma_7-1742793165077.png

 

We will configure our PowerShell Step now to run the PowerShell script. We will make use of Inline Connection and make use of Mid Server IP to populate Host field (Our Script will be running on Mid Server only for this demo). We will also change the Script Type to inline script and paste our PowerShell script in the Command section.

AmitVerma_0-1742815628693.png

 

AmitVerma_1-1742815664665.png

 

Now, our PowerShell script expects a parameter called $serviceName so to pass the Service Name dynamically, we will create an input variable with the same name as our variable and map our action variable to the input variable value so that the Action Input value could be passed to the PowerShell script.

AmitVerma_2-1742815835304.png

 

Now, to collect the output being returned from our PowerShell script, we will create Action output variable. Head to the Outputs section and click on Create Output button.

AmitVerma_3-1742815933596.png

 

Create an Output variable with Label as ServiceStatus and click on Exit Edit Mode.

 

AmitVerma_4-1742816005129.png

Now, we will map the Output from our PowerShell step to this action output variable so that the output being returned from the PowerShell step could be collected and used further via this action. Drag and Drop the PowerShell Step Output data pill to ServiceStatus variable.

AmitVerma_5-1742816097029.png

 

Now, we are ready to test our action. Click on Save to save the action. Now, click on Test button to begin the testing. For this blog, we will make use of Print Spooler service on our mid server for testing. Let's give spooler as a ServiceName and click on Run Test.

AmitVerma_6-1742816245553.png

 Once we open the flow context, we can see our service status being returned in the ServiceName Action variable.

AmitVerma_7-1742816359768.png

As our action is working as expected, we can go ahead and publish it so that it is available to be mapped in a flow. Hit the publish button to publish the action.

AmitVerma_8-1742816519881.png

With this, we have come to the end of this blog. In the next blog, we will make use of this action in a flow which we will use to Orchestrate this Service Restart Process.

 

If you find this blog helpful, please like and bookmark it for future reference. Also, you can visit my earlier blogs in this series :

Infrastructure Automation Odyssey using ServiceNow - Part 1 

Infrastructure Automation Odyssey using ServiceNow - Part 2