Skip to content

Instantly share code, notes, and snippets.

@secdev02
Last active April 23, 2025 15:16
Show Gist options
  • Save secdev02/97d11939cd51d1bfdad7dfc8ca23e588 to your computer and use it in GitHub Desktop.
Save secdev02/97d11939cd51d1bfdad7dfc8ca23e588 to your computer and use it in GitHub Desktop.
Windows Event 7045 Test
# Create a directory to store our files
$workingDir = "C:\ServiceTest"
if (!(Test-Path $workingDir)) {
New-Item -ItemType Directory -Path $workingDir
}
# Create the C# service code
$serviceCode = @'
using System;
using System.ServiceProcess;
using System.Diagnostics;
using System.Threading;
namespace MinimalService
{
public class MinimalService : ServiceBase
{
private EventLog eventLog;
private Thread serviceThread;
private bool running = false;
public MinimalService()
{
this.ServiceName = "MinimalService";
this.CanStop = true;
this.CanPauseAndContinue = false;
this.AutoLog = true;
// Set up event logging
if (!EventLog.SourceExists(this.ServiceName))
{
EventLog.CreateEventSource(this.ServiceName, "Application");
}
eventLog = new EventLog();
eventLog.Source = this.ServiceName;
eventLog.Log = "Application";
}
protected override void OnStart(string[] args)
{
eventLog.WriteEntry("Minimal Service Starting...");
running = true;
serviceThread = new Thread(new ThreadStart(ServiceWorker));
serviceThread.Start();
}
protected override void OnStop()
{
eventLog.WriteEntry("Minimal Service Stopping...");
running = false;
if (serviceThread != null)
{
serviceThread.Join(3000); // Wait for the thread to finish
}
}
private void ServiceWorker()
{
while (running)
{
eventLog.WriteEntry("Minimal Service is running", EventLogEntryType.Information);
Thread.Sleep(10000); // Sleep for 10 seconds
}
}
public static void Main()
{
ServiceBase.Run(new MinimalService());
}
}
}
'@
# Save the service code to a file
$serviceCodePath = Join-Path -Path $workingDir -ChildPath "MinimalService.cs"
Set-Content -Path $serviceCodePath -Value $serviceCode
# Compile the service using .NET Framework
$references = "System.dll", "System.ServiceProcess.dll"
$outputPath = Join-Path -Path $workingDir -ChildPath "MinimalService.exe"
Add-Type -Path $serviceCodePath -OutputAssembly $outputPath -OutputType WindowsApplication -ReferencedAssemblies $references -Debug:$false
# Create the service
$serviceName = "MinimalService"
$exePath = $outputPath
Write-Host "Creating service..." -ForegroundColor Yellow
sc.exe create $serviceName binPath= $exePath DisplayName= "Minimal Test Service" start= auto
# Start the service
Write-Host "Starting service..." -ForegroundColor Green
Start-Service -Name $serviceName
Start-Sleep -Seconds 5
# Check the service status
Get-Service -Name $serviceName
# Stop the service
Write-Host "Stopping service..." -ForegroundColor Red
Stop-Service -Name $serviceName -Force
Start-Sleep -Seconds 3
# Delete the service
Write-Host "Deleting service..." -ForegroundColor Magenta
sc.exe delete $serviceName
Write-Host "Process completed!" -ForegroundColor Cyan
# Create a directory to store our files
$workingDir = "C:\ServiceTest"
if (!(Test-Path $workingDir)) {
New-Item -ItemType Directory -Path $workingDir
}
# Create the C# service code
$serviceCode = @'
using System;
using System.ServiceProcess;
using System.Diagnostics;
using System.Threading;
namespace MinimalService
{
public class MinimalService : ServiceBase
{
private EventLog eventLog;
private Thread serviceThread;
private bool running = false;
public MinimalService()
{
this.ServiceName = "MinimalService";
this.CanStop = true;
this.CanPauseAndContinue = false;
this.AutoLog = true;
// Set up event logging
if (!EventLog.SourceExists(this.ServiceName))
{
EventLog.CreateEventSource(this.ServiceName, "Application");
}
eventLog = new EventLog();
eventLog.Source = this.ServiceName;
eventLog.Log = "Application";
}
protected override void OnStart(string[] args)
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
eventLog.WriteEntry($"[{timestamp}] Minimal Service Starting...");
running = true;
serviceThread = new Thread(new ThreadStart(ServiceWorker));
serviceThread.Start();
}
protected override void OnStop()
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
eventLog.WriteEntry($"[{timestamp}] Minimal Service Stopping...");
running = false;
if (serviceThread != null)
{
serviceThread.Join(3000); // Wait for the thread to finish
}
}
private void ServiceWorker()
{
while (running)
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
eventLog.WriteEntry($"[{timestamp}] Minimal Service is running", EventLogEntryType.Information);
// Also write to console for visibility during debugging
Console.WriteLine($"[{timestamp}] Minimal Service is running");
Thread.Sleep(10000); // Sleep for 10 seconds
}
}
public static void Main()
{
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine($"[{timestamp}] Minimal Service main method started");
ServiceBase.Run(new MinimalService());
}
}
}
'@
# Save the service code to a file
$serviceCodePath = Join-Path -Path $workingDir -ChildPath "MinimalService.cs"
Set-Content -Path $serviceCodePath -Value $serviceCode
# Compile the service using .NET Framework
$references = "System.dll", "System.ServiceProcess.dll"
$outputPath = Join-Path -Path $workingDir -ChildPath "MinimalService.exe"
Add-Type -Path $serviceCodePath -OutputAssembly $outputPath -OutputType WindowsApplication -ReferencedAssemblies $references -Debug:$false
# Create the service
$serviceName = "MinimalService"
$exePath = $outputPath
Write-Host "Creating service..." -ForegroundColor Yellow
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "[$timestamp] Creating service $serviceName" -ForegroundColor Yellow
sc.exe create $serviceName binPath= $exePath DisplayName= "Minimal Test Service" start= auto
# Start the service
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "[$timestamp] Starting service..." -ForegroundColor Green
Start-Service -Name $serviceName
Start-Sleep -Seconds 5
# Check the service status
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "[$timestamp] Service status:" -ForegroundColor Cyan
Get-Service -Name $serviceName
# Stop the service
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "[$timestamp] Stopping service..." -ForegroundColor Red
Stop-Service -Name $serviceName -Force
Start-Sleep -Seconds 3
# Delete the service
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "[$timestamp] Deleting service..." -ForegroundColor Magenta
sc.exe delete $serviceName
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "[$timestamp] Process completed!" -ForegroundColor Cyan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment