In this article, we are going to discuss consuming REST API inside PowerShell and performing CRUD operations through PowerShell Script.
Agenda
- Implementation of .NET Core 6 Web API
- Create PowerShell Script and consume API to perform CRUD operation
Prerequisites
- Visual Studio 2022
- .NET Core SDK 6
- SQL Server
- PowerShell
Implementation of .NET Core 6 Web API
Here we implement a product offer application for demo purposes which is managed by the admin and performed CRUD operations
Project Structure

Step 1
Create a new .NET Core Web API

Step 2
Configure a new project

Step 3
Provide additional information

Step 4
Create an Offer class inside the Entities folder
namespace PowerShellDemo.Entities
{
public class Offer
{
public int Id { get; set; }
public string ProductName { get; set; }
public string OfferDescription { get; set; }
}
}
Step 5
Next, add DbContextClass inside the Data folder which provides a data context object to perform data-related all SQL operations
using Microsoft.EntityFrameworkCore;
using PowerShellDemo.Entities;
namespace PowerShellDemo.Data
{
public class DbContextClass : DbContext
{
protected readonly IConfiguration Configuration;
public DbContextClass(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
}
public DbSet<Offer> Offer { get; set; }
}
}
Step 6
Create IProductOfferService and ProductOfferService files inside the Repositories
IProductOfferService
using PowerShellDemo.Entities;
namespace PowerShellDemo.Repositories
{
public interface IProductOfferService
{
public Task<List<Offer>> GetOfferListAsync();
public Task<Offer> GetOfferByIdAsync(int Id);
public Task<Offer> AddOfferAsync(Offer offer);
public Task<Offer> UpdateOfferAsync(Offer offer);
public Task<bool> DeleteOfferAsync(int Id);
}
}
ProductOfferService
using Microsoft.EntityFrameworkCore;
using PowerShellDemo.Data;
using PowerShellDemo.Entities;
namespace PowerShellDemo.Repositories
{
public class ProductOfferService : IProductOfferService
{
private readonly DbContextClass _dbContext;
public ProductOfferService(DbContextClass dbContext)
{
_dbContext = dbContext;
}
public async Task<List<Offer>> GetOfferListAsync()
{
return await _dbContext.Offer.ToListAsync();
}
public async Task<Offer> GetOfferByIdAsync(int Id)
{
return await _dbContext.Offer.Where(x => x.Id == Id).FirstOrDefaultAsync();
}
public async Task<Offer> AddOfferAsync(Offer offer)
{
var result = _dbContext.Offer.Add(offer);
await _dbContext.SaveChangesAsync();
return result.Entity;
}
public async Task<Offer> UpdateOfferAsync(Offer offer)
{
var result = _dbContext.Offer.Update(offer);
await _dbContext.SaveChangesAsync();
return result.Entity;
}
public async Task<bool> DeleteOfferAsync(int Id)
{
var filteredData = _dbContext.Offer.Where(x => x.Id == Id).FirstOrDefault();
var result = _dbContext.Remove(filteredData);
await _dbContext.SaveChangesAsync();
return result != null ? true : false;
}
}
}
Step 7
Next, create a new product to offer a controller
using Microsoft.AspNetCore.Mvc;
using PowerShellDemo.Entities;
using PowerShellDemo.Repositories;
namespace PowerShellDemo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ProductOfferController : ControllerBase
{
private readonly IProductOfferService productOfferService;
public ProductOfferController(IProductOfferService productOfferService)
{
this.productOfferService = productOfferService;
}
[HttpGet("getofferlist")]
public async Task<List<Offer>> GetOfferListAsync()
{
try
{
var response = await productOfferService.GetOfferListAsync();
return response;
}
catch
{
}
return null;
}
[HttpGet("getofferbyid")]
public async Task<Offer> GetOfferByIdAsync(int Id)
{
try
{
var response = await productOfferService.GetOfferByIdAsync(Id);
return response;
}
catch
{
}
return null;
}
[HttpPost("addoffer")]
public async Task<Offer> AddOfferAsync(Offer offer)
{
try
{
var response = await productOfferService.AddOfferAsync(offer);
return response;
}
catch
{
}
return null;
}
[HttpPut("updateoffer")]
public async Task<Offer> UpdateOfferAsync(Offer offer)
{
try
{
var response = await productOfferService.UpdateOfferAsync(offer);
return response;
}
catch
{
}
return null;
}
[HttpDelete("deleteoffer")]
public async Task<bool> DeleteOfferAsync(int Id)
{
try
{
var response = await productOfferService.DeleteOfferAsync(Id);
return response;
}
catch
{
}
return false;
}
}
}
Step 8
Later on, register a few services in the DI container inside the Program class
using PowerShellDemo.Data;
using PowerShellDemo.Repositories;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IProductOfferService, ProductOfferService>();
builder.Services.AddDbContext<DbContextClass>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();





Join the conversation! Your thoughts help the community grow.