param([string]$action)

$action = $args[0]

Try {
    if ($action -ne "ENABLE") {
        $action = "DISABLE"
    }

    Write-Host -ForegroundColor yellow "`n--- $action WINDOWS AUTOMATIC UPDATE SERVICES ---`n”
   

    #
    #   Windows Update Medic Service
    #

    Write-Host "$action Windows Update Medic Service..."
    if (Get-Service WaasMedicSvc -ErrorAction SilentlyContinue) {
        $service = Get-Service -Name WaasMedicSvc
        if ($service.Status -eq 'Running') {
            Stop-Service -Name WaasMedicSvc -Force
        }
    }

    $startValue = Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\WaasMedicSvc -Name Start
    if ($action -eq "ENABLE" ) {
        if ($startValue -ne 3) {
            Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\WaasMedicSvc -Name Start -Value 3
        }
        # Start-Service -Name WaasMedicSvc 
    }
    if ($action -eq "DISABLE" -and $startValue -ne 4) {
        Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\WaasMedicSvc -Name Start -Value 4
    }


    #
    #   Windows Automatic Update Service
    #

    Write-Host "$action Windows Automatic Update Service..." 
    if (Get-Service wuauserv -ErrorAction SilentlyContinue) {
        $service = Get-Service -Name wuauserv
        if ($service.Status -eq 'Running') {
            Stop-Service -Name wuauserv -Force
        }
    }

    $startValue = Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\wuauserv -Name Start
    if ($action -eq "ENABLE") {
        if ($startValue -ne 3) {
            Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\wuauserv -Name Start -Value 3
        }
        Start-Service -Name wuauserv 
    }
    if ($action -eq "DISABLE" -and $startValue -ne 4) {
        Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\wuauserv -Name Start -Value 4
    }




    #
    #   Update Orchestrator Service
    #

    Write-Host "$action Update Orchestrator Service..." 
    if (Get-Service UsoSvc -ErrorAction SilentlyContinue) {
        $service = Get-Service -Name UsoSvc
        if ($service.Status -eq 'Running') {
            Stop-Service -Name UsoSvc -Force
        }
    }

    $startValue = Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\UsoSvc -Name Start
    if ($action -eq "ENABLE") {
        if ($startValue -ne 3) {
            Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\UsoSvc -Name Start -Value 2
        }
        Start-Service -Name UsoSvc
    }
    if ($action -eq "DISABLE" -and $startValue -ne 4) {
        Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\UsoSvc -Name Start -Value 4
    }

    Write-Host -ForegroundColor yellow "`n--- $action COMPLETE ---`n"


}
Catch {
    throw $_.Exception.Message
}