DanysysTeam / PS-SFTA

PowerShell Set File Type Association
253 stars 53 forks source link

Register-FTA throws an error when StrictMode enabled #12

Closed nagysifa closed 2 years ago

nagysifa commented 2 years ago

Windows 11 build 22000 PSVersion 5.1.22000.65 PS-SFTA 1.2.0

A simple example to reproduce the error:

Set-ExecutionPolicy -Scope Process Bypass
Set-StrictMode -Version Latest
. .\SFTA.ps1
Register-FTA "$Env:SystemRoot\System32\notepad.exe" .ps1

Register-FTA throws the following error:

The property '.ps1' cannot be found on this object. Verify that the property exists.
At C:\Users\User\Desktop\SFTA.ps1:333 char:27
+     (Get-ItemProperty ("$($_.PSPath)\Capabilities\" + (@("URLAssociat ...
+                           ~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict
Danyfirex commented 2 years ago

Hello, Issue is related to the use Set-StrictMode -Version Latest. It arises an error due to the object property is not found. You can solve it in any of these three ways:

1.- Remove Set-StrictMode If it's possible.

2.- Use a try catch in the code at line 333:

  try {
      $allApplicationAssociationToasts += Get-ChildItem -Path HKLM:SOFTWARE\Clients\StartMenuInternet\* , HKCU:SOFTWARE\Clients\StartMenuInternet\* -ErrorAction SilentlyContinue | 
      ForEach-Object {
      (Get-ItemProperty ("$($_.PSPath)\Capabilities\" + (@("URLAssociations", "FileAssociations") | Select-Object -Index $Extension.Contains("."))) -ErrorAction SilentlyContinue).$Extension
      }  
    }
    catch {}

3.- Fix line 333 with Select-Object

(Get-ItemProperty ("$($_.PSPath)\Capabilities\" + (@("URLAssociations", "FileAssociations") | Select-Object -Index $Extension.Contains("."))) -ErrorAction SilentlyContinue) | Select-Object -ExpandProperty $Extension -ErrorAction SilentlyContinue

To set Notepad.exe as default You can use the ProgId:

Set-FTA 'txtfile' .ps1
nagysifa commented 2 years ago

Thanks for the quick reply, using the first solution listed (temporarily disabling StrictMode), since it does not require modifying the SFTA script.