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, System Administrator / Server Operation Engineer interact with IT infrastructure programmatically.
Currently many tech support companies need web application to manage L1 (Level 1) server support activity through automation or programmatically, DevOps concept appropriately fits in this scenario.
In most cases, DevOps practice involves following two roles:
- System Administrator
- Automation Developer
Each DevOps application has 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 application.
For example:
1. Get Current Status of 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 below sample examples.
3.Execute script on another machine to get service status [This would only execute – if you have Administrator rights on destination server OR you need to pass credential in script]
$command = Invoke-Command -ComputerName YOURCOMPUTERNAME -ScriptBlock {Get-Service | where {$_.Name –eq 'YourServiceName'}}
$command.Status
4.Get Child items from destination machine based on specify location
$command = Invoke-Command -Computername YOURCOMPUTERNAME -ScriptBlock {Get-ChildItem "C:\Program Files”}
$command
Note: Above script is just as 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 above script on remote server. But for that, you need to configure some prerequisites on remote server. We will explain it in our next blog.
To execute above script from portal , you need to store above PowerShell file to your development server on specific location (i.e. C:\PSPackage\PS\ServiceOperation.ps1). [If you have very less commands to execute then you can add those directly in Command pipeline of c#]
You would require to add “System.Management.Automation;” namespace to execute 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();
}
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 based workflow for user creation
- Exchange and Office 365 automation
- SharePoint On premise to SharePoint online migration activity
- Manage Azure Operation
- Integrate Server performance parameter with testing
- Manage repository on SVN
- Get Current IIS HTTP Request
PowerShell Automation Scripting reduces L1 tech support cost. 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 call on remote server in our next blog.

Mihir Parekh

Latest posts by Mihir Parekh (see all)
- Guide to PowerShell Automation Scripting for Successful DevOps - August 14, 2018
- 3 Steps to Integrate SharePoint with Amazon S3 to create Enterprise File Storage Solution - October 17, 2016
Do u offer DevOps services? if yes then let me know, i have couple of opportunities. thanks,
Yes we provide DevOps services. The above blog article to an extent highlights our capabilities on DevOps. We would be glad to work with you. Kindly email us details on your requirements on presales@devitpl.com. We will get back to you at the earliest possible.