Windows 10 Script to define Custom Protocol into Registry

80 views Asked by At

For a web application that needs to execute a PowerShell script, I'm working on a script that would define the required Custom Protocol entry into the Registry.

I have a working Protocol definition that was manually generated and it works as needed. When testing the script that would generate an equivalent entry (with a different name of course), the script passes all syntactic checks but, when invoked from the application, it does not yield the desired result (which would be a new text file into a specific folder, feature that works perfectly well when the manually created protocol is invoked).

The invocation within the application's JavaScript is:

:
:
// var l_Test_URL = 'fddmyalbs2:MyApp?Command=' + encodeURIComponent(l_Request_JSON_Str) ;
var l_Test_URL = 'fdmyalbs:MyApp?Command=' + encodeURIComponent(l_Request_JSON_Str) ;


var l_Tempo_Element       = document.createElement("a") ;
l_Tempo_Element.setAttribute    ("id"   , "Invoke_Custom_Prototol"           ) ;
l_Tempo_Element.setAttribute    ("href" , l_Test_URL                         ) ;
l_Tempo_Element.addEventListener("click", function (e) { e.stopPropagation()}) ;

l_Tempo_Element.click() ;

l_Tempo_Element.remove() ;
:
:

In the above, the name fdmyalbs represents the manually created protocol that works as expected while fddmyalbs2 represents the one generated by the script which does not work.

The batch script that defines the Custom Protocol is:

@ECHO OFF

SETLOCAL 

    set scriptpath=%~dp0

    set ScriptFullPath=%scriptpath%FD_Albums_MNGR.ps1
    
    REM Exit script if the "command" part of the protocol definition is present...
    REM --------------------------------------------------------------------------
        reg query HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open\command > null
    
        set Query_Result=%errorlevel%
    
        if "%Query_Result%"=="0" (
            echo.
            echo Custom Protocol appears to be well defined. Terminating script...
            echo.
            goto:EOF
        )

echo.
echo.
echo "Verifying/Complete Custom Protocol definition into Registry...

REM
REM Custom Prodocol check and (if needed) definition...
REM ===================================================
REM Checking Protocol Name...
    
    reg query HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2 > null
    
    set Query_Result=%errorlevel%
    
    if "%Query_Result%"=="0" (
        echo "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2 OK!"
    ) else (
        echo "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2 missing. Registering..."
        reg add HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2 /t REG_SZ /d "FDAlbums Files Managment" /f
        reg add HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2 /v "URL Protocol" /t REG_SZ /d "" /f

    )

echo.
echo.

REM Checking shell...
    
    reg query HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell > null
    
    set Query_Result=%errorlevel%
    
    if "%Query_Result%"=="0" (
        echo "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell OK!"
    ) else (
        echo "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell missing. Registering..."
        reg add HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell /t REG_SZ /f
    )

echo.
echo.

REM Checking open...

    reg query HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open > null
    
    set Query_Result=%errorlevel%
    
    if "%Query_Result%"=="0" (
        echo "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open OK!"
    ) else (
        echo "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open missing. Registering..."
        reg add HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open /t REG_SZ /f
    )

echo.
echo.

REM Checking command...

    reg query HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open\command > null
    
    set Query_Result=%errorlevel%
    
    if "%Query_Result%"=="0" (
        echo "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open\command OK!"
    ) else (
        echo "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open\command missing. Registering..."
        reg add HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open\command /t REG_SZ /d "powershell.exe\"%ScriptFullPath%FD_Albums_MNGR.ps1\" \"%%~1\""
    )
    

ENDLOCAL
ECHO ON

I should note that the result of the last reg add command I had to manually change via the Register Editor application since the very last piece of it is not generated as the one of the manually generated protocol which, again, does the job. The difference was that the piece representing the passed parameter was different (it should be "%1").

Last, here is the result of the command For %G In (LM CU) Do @%SystemRoot%\System32\reg.exe Query "HK%G\SOFTWARE\Classes\<protocol-name>" /S 2>NUL for each of the two protocols.

C:\>For %G In (LM CU) Do @%SystemRoot%\System32\reg.exe Query "HK%G\SOFTWARE\Classes\fdmyalbs" /S 2>NUL

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fdmyalbs
    (Default)    REG_SZ    FDAlbums Files Managment
    URL Protocol    REG_SZ

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fdmyalbs\shell
    (Default)    REG_SZ

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fdmyalbs\shell\open
    (Default)    REG_SZ

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fdmyalbs\shell\open\command
    (Default)    REG_SZ    "powershell.exe"c:\D_Disk\MyAlbums\FD_Albums_MNGR.ps1 "%1"


C:\>For %G In (LM CU) Do @%SystemRoot%\System32\reg.exe Query "HK%G\SOFTWARE\Classes\fddmyalbs2" /S 2>NUL

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2
    (Default)    REG_SZ    FDAlbums Files Managment
    URL Protocol    REG_SZ

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell
    (Default)    REG_SZ

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open
    (Default)    REG_SZ

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\fddmyalbs2\shell\open\command
    (Default)    REG_SZ    "powershell.exe"c:\D_Disk\MyAlbums\FD_Albums_MNGR.ps1 "%1"

Note: "powershell.exe"c:\D_Disk\MyAlbums\... and "powershell.exe" c:\D_Disk\MyAlbums\... (added space) yield the same result for both protocols.

Why does the protocol generated by the script not work while when checking their definitions they appear to be identical except for the protocol name?

0

There are 0 answers