DanysysTeam / PS-SFTA

PowerShell Set File Type Association
253 stars 53 forks source link

Suggestion: Use alternative approach for obtaining the user SID #9

Closed fgimian closed 2 years ago

fgimian commented 2 years ago

Hey there folks, firstly thank you so SO much for writing this and making it open source!!! 😄

I notice that Set-FTA was quite slow on my system:

e.g.

C:\Users\Fots\Downloads> Measure-Command { Set-FTA -ProgId foobar2000.WAV -Extension .wav -Verbose }
VERBOSE: ProgId: foobar2000.WAV
VERBOSE: Extension/Protocol: .wav
VERBOSE: Getting Hash For foobar2000.WAV   .wav
VERBOSE: baseInfo: .wavs-1-5-21-4006214458-3003202-965951313-1001foobar2000.wav01d83c0f87537800user choice set via windows user experience {d18b6dd5-6124-4341-9318-804003bafa0b}
VERBOSE: Hash: K+RBO0qUSjg=
VERBOSE: Write Registry Extension: .wav
VERBOSE: Remove Extension UserChoice Key If Exist: Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.wav\UserChoice
VERBOSE: Write Reg Extension UserChoice OK

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 4
Milliseconds      : 805
Ticks             : 48050688
TotalDays         : 5.56142222222222E-05
TotalHours        : 0.00133474133333333
TotalMinutes      : 0.08008448
TotalSeconds      : 4.8050688
TotalMilliseconds : 4805.0688

After some investigation, I found that the local Get-UserSid was the main cause of delay. I have always used another approach to obtain the Sid which is shown below:

    $userSid = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value.ToLower()

Switching to this approach, I now see a significant improvement in performance:

C:\Users\Fots\Downloads> Measure-Command { Set-FTA -ProgId foobar2000.WAV -Extension .wav -Verbose }
VERBOSE: ProgId: foobar2000.WAV
VERBOSE: Extension/Protocol: .wav
VERBOSE: Getting Hash For foobar2000.WAV   .wav
VERBOSE: baseInfo: .wavs-1-5-21-4006214458-3003202-965951313-1001foobar2000.wav01d83c0fceda0400user choice set via windows user experience {d18b6dd5-6124-4341-9318-804003bafa0b}
VERBOSE: Hash: Et6Y8vcr12U=
VERBOSE: Write Registry Extension: .wav
VERBOSE: Remove Extension UserChoice Key If Exist: Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.wav\UserChoice
VERBOSE: Write Reg Extension UserChoice OK

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 242
Ticks             : 2422168
TotalDays         : 2.80343518518519E-06
TotalHours        : 6.72824444444444E-05
TotalMinutes      : 0.00403694666666667
TotalSeconds      : 0.2422168
TotalMilliseconds : 242.2168

Just wanted to share this in case you wish to update the script for everyone.

Thanks heaps Fotis

Danyfirex commented 2 years ago

Hello @fgimian, Try this old version. I was using other method before, but @Larpico was getting an issue #7 in domain accounts with the same name as a local account so I merged his pull request. I was thinking in revert it time ago or maybe add a switch. let me know how it works.

fgimian commented 2 years ago

Thanks @Danyfirex. I believe you may have meant to link me to the previous commit.

I have tested that version prior to this commit and it performs as expected. That commit sadly has resulted in a very significant delay when running Set-FTA.

If this approach is really necessary for domain accounts, it may be worth having a specific switch to enable that behaviour instead of making it the default I think.

Huge love and thanks Fotis

Danyfirex commented 2 years ago

Updated. Now by default it uses old SID method. For special case shown in #7 use -DomainSID switch.