Simple ActiveSetup

Topics: Archive - Toolkit Extensions
Oct 20, 2014 at 4:52 AM
I needed a simple Active Setup, just to add a single registry key. Seems to be the most common task I run into when dealing with Active Setup. The other scripts offer much more functionality but I wanted to keep it simple.

It merely add two functions, one to add an ActiveSetup entry and one to remove. It does support multiple entries, but you will need to number them by hand.
function Add-ActiveSetup([int]$Number, [string]$Command) {

# This function creates an ActiveSetup entry in the registry. This can be used to rerun an application per user
# or add HKCU registry keys.
#
# Usage is simply Add-ActiveSetup -Number # -Command <command>
#
# $Number is utilized to run multiple commands (and remove individual commands at uninstall)
# $Command is the command you wish to run.
#
# Example: Add-ActiveSetup -Number 1 -Command 'reg add "HKCU\Software\Applications\Settings" /v "ProductKey" /d "159SAGFASGFASDFAS2KCYY6D7Z2" /t REG_SZ /f'

    write-log "Creating ActiveSetup entry number: $number"
    write-log "Uisng command : $command"

    if (!(Get-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\PSAppDeploy\$appName.$number")){

    Function GenerateGUID {

        #Declare Local Memory
        Set-Variable -Name GetGUID -Scope Local -Force
    
        $GetGUID = [guid]::NewGuid()
        $GetGUID = $GetGUID.ToString().ToUpper()
        
        return $GetGUID

        #Cleanup Local Memory
        Remove-Variable -Name GetGUID -Scope Local -Force
   
    }

    $GUID = GenerateGUID

    Set-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\PSAppDeploy\$appName.$Number"  -Name "GUID" -Value "{$GUID}" -Type String

    Set-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\{$GUID}" -Name "Version" -Value $appVersion -Type String
    Set-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\{$GUID}" -Name "Component" -Value $appName.$Number -Type String
    Set-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\{$GUID}" -Name "StubPath" -Value $command -Type String

    }
}

function Remove-ActiveSetup ([int]$Number) {
    
    $GUID = Get-RegistryKey "HKEY_LOCAL_MACHINE\SOFTWARE\PSAppDeploy\$appName.$Number" -Value "GUID"

    Remove-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\PSAppDeploy\$appName.$Number"
    Remove-RegistryKey -Key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\$GUID"

}
Oct 21, 2014 at 9:03 PM
Or see
Set-ActiveSetup and Remove-ActiveSetup: Per-user changes (as requested) Part1
https://psappdeploytoolkit.codeplex.com/discussions/565009
and
Set-ActiveSetup and Remove-ActiveSetup: Per-user changes (as requested) Part2
https://psappdeploytoolkit.codeplex.com/discussions/565010
Oct 22, 2014 at 2:01 AM
Yeah I saw your entry too late unfortunately!

So I just finished up what I was working on.

Your functions do a lot more than mine but mine is very simple and targeted to what I needed.