Set-Win32ScheduledTasks.ps1

<#PSScriptInfo
.GUID
    d572020a-7583-4867-a845-bb9737b1ecd9
.VERSION
    1.0.0.1
.AUTHOR
    Michael Haken
.COMPANYNAME
    BAMCIS
.COPYRIGHT
    (c) 2016 BAMCIS. All rights reserved.
.TAGS
    WMI ScheduledTasks TaskScheduler
.LICENSEURI
    https://www.bamcisnetworks.net/license
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
    Added compatibility for Nano server.
#>


<#
    .SYNOPSIS
        Creates the Win32_ScheduledTask WMI class on the local computer or a remote computer.
 
    .DESCRIPTION
        The cmdlet creates a custom WMI class for enumerating scheduled task information. It creates a temporary mof file on the SystemDrive and calls mofcomp.exe to add the WMI class.
 
    .PARAMETER ComputerName
        The computer to create the custom WMI classes on. This defaults to [System.String]::Empty, which will execute on the local host. If the target is a remote computer, Invoke-Command is used to execute the underlying function.
 
    .PARAMETER TempFilePath
        Where the temporary mof file is stored, this defaults to %SYSTEMDRIVE%\ScheduledTasks.mof.
 
    .PARAMETER Credential
        The credential to use to execute the script.
 
        If credentials are specified and the computer is the localhost, WinRM is used locally to execute the commands.
 
    .EXAMPLE
        Set-Win32ScheduledTasks
 
        Creates the custom WMI class on the local computer.
 
    .EXAMPLE
        Set-Win32ScheduledTasks -ComputerName server1.contoso.com -Credential (Get-Credential)
 
        Creates the custom WMI class on server1.contoso.com.
 
    .INPUTS
        None
 
    .OUTPUTS
        None
 
    .NOTES
        AUTHOR: Michael Haken
        LAST UPDATE: 1/3/2017
#>


Param
(
    [Parameter(Position = 0)]
    [System.String]$ComputerName = [System.String]::Empty,
    [Parameter(Position= 1)]
    [System.String]$TempFilePath = "$env:SYSTEMDRIVE\ScheduledTasks.mof",
    [Parameter(Position = 2)]
    [System.Management.Automation.PSCredential]
    [System.Management.Automation.Credential()]
    $Credential = [System.Management.Automation.PSCredential]::Empty
)

Function Set-WMIClass 
{
    Begin
    {
        if (([System.Security.Principal.WindowsPrincipal][System.Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
        {
            $MOFFile = "win32_scheduledtasks.mof"
            $WMIClass = "win32_ScheduledTasks"
    
            $Contents = @"
#pragma namespace("\\\\.\\root\\cimv2")
#PRAGMA AUTORECOVER
 
[dynamic, provider("RegProv"),
ProviderClsid("{fe9af5c0-d3b6-11ce-a5b6-00aa00680c3f}"),ClassContext("local|HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Schedule\\TaskCache\\Tasks")]
class $WMIClass
{
    [key] string KeyName;
    [read, propertycontext("Actions")] uint8 Actions[];
    [read, propertycontext("Author")] string Author;
    [read, propertycontext("Data")] string Data;
    [read, propertycontext("Date")] string Date;
    [read, propertycontext("Description")] string Description;
    [read, propertycontext("DynamicInfo")] uint8 DynamicInfo[];
    [read, propertycontext("Hash")] uint8 Hash[];
    [read, propertycontext("Path")] string Path;
    [read, propertycontext("Schema")] uint32 Schema;
    [read, propertycontext("SecurityDescriptor")] string SecurityDescriptor;
    [read, propertycontext("Source")] string Source;
    [read, propertycontext("Triggers")] uint8 Triggers[];
    [read, propertycontext("URI")] string URI;
    [read, propertycontext("Version")] string Version;
 
};
"@

        }
        else
        {
            Write-Warning "Script must be run with administrator privileges."
            Exit
        }
    }

    Process
    {
        Set-Content -Path $TempFilePath -Value $Contents 

        $ScheduledTask = Get-CimInstance -ClassName $WMIClass -Namespace "root/cimv2" -ErrorAction SilentlyContinue

        $InstallType = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name InstallationType | Select-Object -ExpandProperty InstallationType

        if ($ScheduledTask -ne $null)
        {
            if ($InstallType -eq "Nano Server") 
            {
                Start-Process -FilePath "$($env:SystemRoot)\system32\wbem\wmic.exe" -ArgumentList @("class $WMIClass delete") -Wait | Out-Null
            }
            else 
            {
                Start-Process -FilePath "$($env:SystemRoot)\system32\wbem\wmic.exe" -ArgumentList @("class $WMIClass delete") -WindowStyle Hidden -Wait | Out-Null
            }
        }

        if ($InstallType -eq "Nano Server") 
        {
            Start-Process -FilePath ($env:SystemRoot + "\system32\wbem\mofcomp.exe") -ArgumentList @($TempFilePath) -Wait | Out-Null
        }
        else
        {
            Start-Process -FilePath ($env:SystemRoot + "\system32\wbem\mofcomp.exe") -ArgumentList @($TempFilePath) -WindowStyle Hidden -Wait | Out-Null
        }

        $Counter = 0

        while ($Counter -lt 30) 
        {
            try 
            {
                Remove-Item -Path $TempFilePath -ErrorAction Stop -Force | Out-Null
                break
            }
            catch [Exception] 
            {
                $Counter++

                if ($Counter -ge 30) 
                {
                    Write-Warning "Timeout waiting to delete the temporary mof file, delete manually."
                    break
                }

                Start-Sleep -Seconds 1
            }
        }

        $ScheduledTask = Get-CimInstance -ClassName $WMIClass -Namespace "root/cimv2" -ErrorAction SilentlyContinue

        if ($ScheduledTask -ne $null)
        {
            Write-Host "Creating the WMI class was successful." -ForegroundColor Green
        }
        else
        {
            Write-Host "There was an error creating the class." -ForegroundColor Red
        }
    }
    
    End {

    }   
}

[bool]$Local = [System.String]::IsNullOrEmpty($ComputerName) -or `
    $ComputerName -eq "." -or `
    $ComputerName.ToLower() -eq "localhost" -or `
    $ComputerName.ToLower() -eq $ENV:COMPUTERNAME.ToLower() -or `
    $ComputerName -eq "127.0.0.1"

if ($Credential -eq $null)
{
    $Credential = [System.Management.Automation.PSCredential]::Empty
}

if ($Local -and $Credential -eq [PSCredential]::Empty)
{
    Set-WMIClass
}
else 
{
    Invoke-Command -ComputerName $ComputerName -ScriptBlock ${function:Set-WMIClass} -Credential $Credential
}