#requires -Version 5.1
[CmdletBinding()]
param(
    [ValidateSet('Baseline', 'Application', 'Both')]
    [string]$Scenario = 'Both',

    [ValidateSet('Introduce', 'Verify')]
    [string]$Action = 'Introduce'
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

$registryPath = 'HKLM:\SOFTWARE\AzureWorkshopz\MachineConfiguration'
$markerPath = 'C:\AzureWorkshopz\MachineConfiguration\desired-state.txt'

function Write-WorkshopEvent {
    param([int]$EventId, [string]$Message)
    try {
        if (-not [System.Diagnostics.EventLog]::SourceExists('AzureWorkshopzMachineConfiguration')) {
            New-EventLog -LogName Application -Source 'AzureWorkshopzMachineConfiguration'
        }
        Write-EventLog -LogName Application -Source 'AzureWorkshopzMachineConfiguration' -EntryType Information -EventId $EventId -Message $Message
    }
    catch { }
}

if ($Action -eq 'Introduce') {
    New-Item -Path $registryPath -Force | Out-Null
    Set-ItemProperty -Path $registryPath -Name BaselineVersion -Value 'DRIFTED' -Type String
    Remove-Item -LiteralPath $markerPath -Force -ErrorAction SilentlyContinue
    Set-Service -Name EventLog -StartupType Manual

    if ($Scenario -in @('Application', 'Both')) {
        Remove-WindowsFeature -Name Web-Server -Remove:$false -Restart:$false | Out-Null
        Remove-Item -LiteralPath 'C:\inetpub\wwwroot\index.html' -Force -ErrorAction SilentlyContinue
    }

    Write-WorkshopEvent -EventId 4101 -Message "Workshop drift introduced safely on $env:COMPUTERNAME at $([DateTime]::UtcNow.ToString('o'))."
    [pscustomobject]@{
        Hostname        = $env:COMPUTERNAME
        DriftIntroduced = $true
        RegistryValue   = (Get-ItemPropertyValue -Path $registryPath -Name BaselineVersion)
        MarkerPresent   = Test-Path -LiteralPath $markerPath
        EventLogStartup = (Get-CimInstance Win32_Service -Filter "Name='EventLog'").StartMode
        IisInstalled    = [bool](Get-WindowsFeature Web-Server).Installed
    }
    return
}

$isApplication = $Scenario -in @('Application', 'Both')
$registryValue = Get-ItemPropertyValue -Path $registryPath -Name BaselineVersion -ErrorAction SilentlyContinue
$markerPresent = Test-Path -LiteralPath $markerPath
$eventLog = Get-CimInstance Win32_Service -Filter "Name='EventLog'"
$iisInstalled = [bool](Get-WindowsFeature Web-Server).Installed
$compliant = $registryValue -eq '1.1.0' -and $markerPresent -and $eventLog.State -eq 'Running' -and $eventLog.StartMode -eq 'Auto'
if ($isApplication) { $compliant = $compliant -and $iisInstalled }
if ($compliant) {
    Write-WorkshopEvent -EventId 4102 -Message "Workshop desired state verified on $env:COMPUTERNAME at $([DateTime]::UtcNow.ToString('o'))."
}
[pscustomobject]@{
    Hostname        = $env:COMPUTERNAME
    Compliant       = $compliant
    RegistryValue   = $registryValue
    MarkerPresent   = $markerPresent
    EventLogState   = $eventLog.State
    EventLogStartup = $eventLog.StartMode
    IisInstalled    = $iisInstalled
}
