ActivateAll.psm1

function Get-Answer
{
    [CmdletBinding()]
    [OutputType([boolean])]
    param
    (
        [Parameter(Mandatory = $true)]
        [string]$question,
        [boolean]$answer
    )
    
    $YN = Read-Host -Prompt "$question`n(Y)es, ENTER - No"
    if ($YN -eq "Y")
    {
        [boolean]$answer = $true
    }
    else
    {
        [boolean]$answer = $false
    }
    [boolean]$answer
}
function Test-Admin
{
    [CmdletBinding()]
    [OutputType([boolean])]
    param ()
    
    return [boolean]([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

<#
        .SYNOPSIS
            Install Office (R) Tool module
         
        .DESCRIPTION
            Installs Office (R) Tool module and prompt to install Office (R) Tool
         
        .PARAMETER Force
            CAUTIOUS! This will skip if antivirus realtime protection is enabled !
         
        .EXAMPLE
            PS C:\> Install-OfficeRTool
            PS C:\> Install-OfficeRTool - Force # Install OfficeRTool module without antivirus check
         
        .NOTES
            By CHXOFT ©2025.
    #>

function Install-OfficeRTool
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        [switch]$Force
    )
    
    if (!(Test-Admin -ErrorAction SilentlyContinue))
    { return Write-Error -Message "Need to open Powershell with Admin privileges." -Category PermissionDenied }
    
    if (!(Get-Module -Name "AntivirusProductsDetailedStatus" -ListAvailable -ErrorAction SilentlyContinue))
    { Install-Module -Name "AntivirusProductsDetailedStatus" -Force }
    
    $RTL = Get-Module -Name OfficeRTool -ListAvailable -ErrorAction SilentlyContinue
    if (!$RTL)
    {
        if (Get-Answer -question "Install Office ® Tool ?")
        {
            Install-Module -Name OfficeRTool -Force
            if (Get-RealTimeProtection)
            {
                Write-Output "`nAntivirus Realtime proteciton emabled.`nDisable antivirus first to install Office (R) Tool."
                return Start-Process -FilePath "windowsdefender://Threatsettings"
            }
            else { return Install-RTool }
        }
        else { return Write-Output -InputObject "`nSkipping Office ® Tool installation." }
    }
    elseif ($RTL.Version.Build -lt 2)
    {
        if (Get-RealTimeProtection)
        {
            Write-Output "`nAntivirus Realtime proteciton emabled.`nDisable antivirus first to update Office (R) Tool."
            Start-Process -FilePath "windowsdefender://Threatsettings"
        }
        else
        {
            Write-Output -InputObject "`nUpdating Office ® Tool module."
            Update-Module -Name "OfficeRTool" -Force
            Write-Output -InputObject "`nInstalling Office ® Tool."
            Install-RTool -IgnoreAntivirus
        }
    }
    
    if ($Force)
    { return Install-RTool -IgnoreAntivirus }
    elseif (Get-Answer -question "Activate Office ?")
    {
        Write-Output -InputObject "`nActivating Office permanently."
        & ([ScriptBlock]::Create((Invoke-RestMethod -Uri $uri))) /Ohook
    }
}

<#
        .SYNOPSIS
            Install dot NET 3.5 feature
         
        .DESCRIPTION
            A detailed description of the Install-dotNET3.5 function.
         
        .PARAMETER Force
            Skip check if dotNET 3.5 is already installed.
         
        .EXAMPLE
            PS C:\> Install-dotNET3.5
         
        .NOTES
            By CHXOFT ©2025.
    #>

function Install-dotNET3.5
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        [switch]$Force
    )
    
    if (!(Test-Admin -ErrorAction SilentlyContinue))
    {
        return Write-Error -Message "Need to open Powershell with Admin privileges." -Category PermissionDenied
    }
    
    if ($pscmdlet.ShouldProcess("Target", "Operation"))
    {
        $dotNetName = "Windows feature .NET 3.5"
        $dotNet = (Get-WindowsOptionalFeature -FeatureName "NetFx3" -Online -ErrorAction SilentlyContinue).State
        
        if ($Force)
        {
            Write-Output "Installing optional $dotNetName package."
            Enable-WindowsOptionalFeature -Online -FeatureName "NetFx3" -All
        }
        elseif (![boolean]$dotNet)
        {
            Write-Output "Installing optional $dotNetName package."
            Enable-WindowsOptionalFeature -Online -FeatureName "NetFx3" -All
        }
        else
        { Write-Output "Optional $dotNetName already enabled." }
        
        if (![boolean]$dotNet)
        {
            Write-Error -Category NotEnabled -Message "$dotNetName failed to install, trying other installation method." -ErrorAction Continue
            $choco = Get-Command -Name "choco.exe" -ErrorAction SilentlyContinue
            
            if (![boolean]$choco)
            {
                [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
                return & ([ScriptBlock]::Create((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')))
            }
            else
            { Start-Process -FilePath "choco.exe" -ArgumentList "install dotnet3.5 -f -y" -NoNewWindow }
            
            if (![boolean]$choco)
            { Start-Process -FilePath "DISM" -ArgumentList "/Online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:D:\sources\sxs" -NoNewWindow }
        }
    }
}

<#
    .SYNOPSIS
        Start online MAS script
     
    .DESCRIPTION
        Opens online Microsoft Activation Scripts.
     
    .PARAMETER Win
        Activate Windows with HWID permanently.
     
    .PARAMETER Office
        Activate Office.
     
    .PARAMETER alt
        Alternative url for online script.
     
    .PARAMETER all
        Activate Windows and Office.
     
    .PARAMETER Force
        Start MAS even the one or more is already running.
     
    .PARAMETER WinHWID
        Start Windows HWID activation.
     
    .PARAMETER OfficeOhook
        Start Office Ohook activation script.
     
    .EXAMPLE
        PS C:\> Start-MAS
        PS C:\> Start-MAS -all
     
    .NOTES
        By CHXOFT ©2025.
#>

function Start-MAS
{
    [CmdletBinding(ConfirmImpact = 'Medium',
                   SupportsShouldProcess = $true)]
    [OutputType([CmdletBinding])]
    param
    (
        [switch]$Win,
        [switch]$Office,
        [switch]$alt,
        [switch]$all,
        [switch]$Force
    )
    
    [string]$Arguments = $null
    [string]$ArgHWID = "/HWID"
    [string]$ArgOFFICE = "/Ohook"
    [string]$Job = 'MAS'
    [string]$name = 'Microsoft Activation Scripts'
    [uri]$uri = 'https://get.activated.win'
    [uri]$uriAlt = 'https://massgrave.dev/get'
    [scriptblock]$ScriptBlock = ([ScriptBlock]::Create((Invoke-RestMethod -Uri $uri)))
    $MASjob = Get-Job -Name $Job -ErrorAction SilentlyContinue
    
    if ($Force)
    { Remove-Job -Name $Job -Force -ErrorAction SilentlyContinue }
    
    if (!$MASjob -or !$MASjob.State.Contains("Running"))
    {
        Remove-Job -Name $Job -Force -ErrorAction SilentlyContinue
        if ($Win -and $Office -or $all)
        { $Arguments = "$ArgHWID $ArgOFFICE" }
        elseif ($Win)
        { $Arguments = "$ArgHWID" }
        elseif ($Office)
        { $Arguments = "$ArgOFFICE" }
        if ($alt)
        { $uri = $uriAlt }
        if ($pscmdlet.ShouldProcess("target", "action"))
        {
            [string]$info = "Starting online $name from $uri"
            if ($Arguments)
            { $info += "with switch $Arguments" }
            Write-Output -InputObject $info -NoEnumerate
            Start-Job -Name $Job -ScriptBlock $ScriptBlock -ArgumentList $Arguments -RunAs32 | Out-Null
        }
    }
    else
    { Write-Warning -Message "$name is already running !" }
}