Guide to PowerShell Automation Scripting for Successful DevOps

Guide to PowerShell Automation Scripting for Successful DevOps

DevOps word itself describes its definition: Dev stands for Development and Ops stands for Operations. When Dev and Ops are merged in one single term where the software development engineer and System Administrator / Server Operation Engineer work across the entire application life cycle, it forms DevOps. So in this concept, the System Administrator / Server Operation Engineer interacts with IT infrastructure programmatically.

PowerShell Automation Scripting

Currently, many tech support companies need web applications to manage L1 (Level 1) server support activity through automation or programmatically, the DevOps concept appropriately fits in this scenario.

In most cases, DevOps practice involves the following two roles:

  • System Administrator
  • Automation Developer

Each DevOps application has a different use. As we are providing 24*7*365 days technical support services, I have taken one small example to demonstrate how IT infrastructure support can be managed through web applications.

For example:

1. Get the Current Status of the Service

$Service = Get-Service | where {$_.Name –eq 'YourServiceName'}
$Service.Status

2. Start Service only if stopped

$Service = Get-Service | where {$_.Name –eq 'YourServiceName'}
if($Service.Status -eq "Stopped")
{
Start-Service 'YourServiceName'
}

You can also execute scripting on another machine. Please check the sample examples below.

3. Execute script on another machine to get service status [This would only execute – if you have Administrator rights on the destination server OR you need to pass credentials in script]

$command = Invoke-Command -ComputerName YOURCOMPUTERNAME -ScriptBlock {Get-Service | where {$_.Name –eq 'YourServiceName'}}
$command.Status

4. Get Child items from the destination machine based on specific location

$command = Invoke-Command -Computername YOURCOMPUTERNAME -ScriptBlock {Get-ChildItem "C:\Program Files”}
$command

Note: The above script is just an example. You can do much more with PowerShell Automation Scripting. Using PowerShell Automation Scripting, you can manage many operations of Microsoft Exchange, Microsoft SQL Server, Microsoft SharePoint, Microsoft O365, and many more. Also, you can execute the above script on a remote server. But for that, you need to configure some prerequisites on a remote server. We will explain it in our next blog.

To execute the above script from the portal, you need to store the above PowerShell file on your development server in a specific location (i.e. C:\PSPackage\PS\ServiceOperation.ps1). [If you have very less commands to execute then you can add those directly in the Command pipeline of c#]

You would be required to add the “System.Management.Automation;” namespace to execute the below code.

Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
try
{
string sourceFile = @" C:\PSPackage\PS\SQLAgent.ps1";
Command cmd = new Command(sourceFile);
pipeline.Commands.Add(cmd);
Collection results = pipeline.Invoke();
foreach (PSObject obj in results)
{
//You can add your logic with received PS Object
}
}
catch (System.Management.Automation.RuntimeException ex)
{
//Manage Error Log
}
catch (Exception ex)
{
//Manage Error Log
}
finally
{
pipeline.Dispose();
runspace.Close();
}

The above example is just for reference.  Remote Support Executive / Managed IT Services Executive can identify many real-time use cases and Automation Developer can check feasibility & develop those in automation platform/web platform.

For example:

  • Active Directory Web UI and Approval workflow for user creation
  • Exchange and Office 365 automation
  • SharePoint On-premise to SharePoint online migration activity
  • Manage Azure Operation
  • Integrate Server performance parameters with testing
  • Manage repository on SVN
  • Get Current IIS HTTP Request

PowerShell Automation Scripting reduces L1 tech support costs. Also, automation code executes based on predefined code statements instead of human action.

Similarly, automation call configuration and Azure automation setup can be configured. We will explain how to configure automation calls on remote servers in our next blog.