Unblocker script works in Win 10 but not Win 7 Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
RickC
Posts: 253
Joined: 27 Oct 2013, 08:32

Unblocker script works in Win 10 but not Win 7

14 Oct 2016, 19:55

When you download an unsigned file from the internet it's marked as 'Blocked'. This has the effect of popping up a UAC prompt asking 'Do you want to allow this app from an unknown publisher to make changes to your PC?' when you come to use the file. It can be time-consuming unblocking files individually so I wrote a script which uses PowerShell to unblock all files is a selected folder.

Whilst it works perfectly in Windows 10 Anniversary Update (much to my surprise :) ), I'm at a loss to explain why it doesn't unblock files in Windows 7. I'm using AHK Unicode 1.1.24.01 x64 on both devices. Can anyone look at the code and provide a reason?

Code: Select all

; This script was prompted by https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/10/powertip-use-powershell-to-unblock-files-in-folder/
#SingleInstance force

; Prompt to 'Run as Admin', i.e. show UAC dialog
If Not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
   ExitApp
}

; Create the GUI elements
Gui, Add, Text, x20 y10 w170 h20 , Select SOURCE folder
Gui, Add, Button, x25 y30 w100 h30 , SOURCE
Gui, Add, Button, x25 y65 w100 h30 , Cancel
Gui -SysMenu
Gui, Show, w150 h100, Windows File Unblocker
return

; Determine what the SOURCE button does
ButtonSOURCE:
FileSelectFolder, SourceVar, *%A_MyDocuments%, 4, Select a SOURCE folder ;Select a source folder (with default location)

; Submit the results of the user's choice
Gui, submit ;Save the selected folder path to a variable
;MsgBox %SourceVar% ;Use MsgBox to test output to commandline

Gui, Destroy ;Close the first GUI so it doesn't muck up the progress bar

; Create and run the PowerShell command
psScript =
(
    Get-ChildItem -Path '%SourceVar%' -Recurse | Unblock-File
)

Run PowerShell.exe -Command &{%psScript%} ,, hide

; Create a second GUI showing looping progress bar ('cos unblocking loads of files can take some time)
; Credit to polyethene for the progress bar routine (https://autohotkey.com/board/topic/12306-infinite-progress-bar/#entry79882)

Gui, Add, Progress, vlvl -Smooth 0x8 w350 h18 ; PBS_MARQUEE = 0x8
Gui, Show, , Unblocking...    Please wait...
SetTimer, Unblocking, 45
Return

Unblocking:
GuiControl, , lvl, 1
Process, Exist, powershell.exe, ; Check whether PowerShell still running
If (!Errorlevel){
SetTimer, Unblocking, Off ; Turn off process timer
Gui, Destroy ; Close the second GUI

MsgBox, 64, Windows File Unblocker, Finished! Your files have been unblocked! ; Inform user that process has finished
;Run, %SourceVar% ; Open the folder to show files
ExitApp
}
Return

; Determine what the CANCEL button does
ButtonCancel:
ExitApp

Esc::ExitApp ; Use the Esc key to exit the app
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Unblocker script works in Win 10 but not Win 7  Topic is solved

15 Oct 2016, 08:23

I didn't look at your script (sorry!), but I'd like to point out that unblocking a file from AutoHotkey can be simply done with FileDelete, %filename%:Zone.Identifier:$DATA, negating the need for PowerShell (always a good thing IMO :troll:).
While unblocking from the Explorer changes the INI file inside that stream so that the zone is set to 4 (probably with a method in IAttachmentExecute), even that PowerShell cmdlet won't go to such lengths and will just delete the stream:

Code: Select all

        foreach (string str5 in list)
        {
            if (base.ShouldProcess(str5))
            {
                AlternateDataStreamUtilities.DeleteFileStream(str5, "Zone.Identifier");
            }
        }
(As an aside, I prefer to configure my system to not mark files downloaded from the Internet in the first place because I find the practice extremely annoying, but still warn me nevertheless if I'm running a file that was downloaded from the Internet on another computer...)
RickC
Posts: 253
Joined: 27 Oct 2013, 08:32

Re: Unblocker script works in Win 10 but not Win 7

15 Oct 2016, 10:04

qwerty12 wrote:I didn't look at your script (sorry!), but I'd like to point out that unblocking a file from AutoHotkey can be simply done with FileDelete, %filename%:Zone.Identifier:$DATA, negating the need for PowerShell (always a good thing IMO :troll:).
Thank you, qwerty12 for the information. I should have realised that the Blocked flag was implemented as Zone.Identifier data within an Alternate Data Stream. I also had no idea the data could be removed using FileDelete. Hopefully this info (and example of how to use) will be added to AHK's documentation.

Your reply didn't answer my question why my script doesn't work in Win 7 but this is now not important. I now have the info about how to clear the blocks in Win 7 without using PowerShell. Thank you again.
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Unblocker script works in Win 10 but not Win 7

15 Oct 2016, 10:32

RickC wrote:I also had no idea the data could be removed using FileDelete. Hopefully this info (and example of how to use) will be added to AHK's documentation.
I can't remember where on the Internet I read it, but the WinAPI functions like DeleteFile and CreateFile etc. are programmed in such a way that they'll let you delete or create, respectively, streams in a file if you append a colon onto the filename and specify a stream name. Since FileDelete calls DeleteFile I figured it would work. I know it's not what you're after, and even mentioning it makes me feel a little silly, but the moderator just me has posted code samples on this forum that use the FindFirstStreamW etc. functions that show what ADSes are present in a file.
Your reply didn't answer my question why my script doesn't work in Win 7 but this is now not important. I now have the info about how to clear the blocks in Win 7 without using PowerShell. Thank you again.
I'm sorry, but it's just that I really don't know. I wrote a PowerShell script to let me set the Advanced sharing settings in the Network and Sharing Centre programatically on a fresh install and that took me a few days because of my incompetence - I just really prefer using AutoHotkey if I can get away with it... Maybe the ExecutionPolicy (if applicable - I really don't know!) was not set to Unrestricted on the Windows 7 machine? Hopefully, you'll get an answer from someone who actually knows what they're talking about!
RickC
Posts: 253
Joined: 27 Oct 2013, 08:32

Re: Unblocker script works in Win 10 but not Win 7

18 Oct 2016, 12:39

This continued to bug me and I've now found out why my script doesn't work in Windows 7. It turns out that that Get-ChildItem -Path '%SourceVar%' -Recurse | Unblock-File is not supported in the default version of PowerShell in Windows 7, i.e. PowerShell 2. As soon as I installed PowerShell 3 my script worked. It's a pain installing PowerShell 3 so I'll just use a different method.
User avatar
hasantr
Posts: 779
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Unblocker script works in Win 10 but not Win 7

24 Nov 2020, 06:58

RickC wrote:
18 Oct 2016, 12:39
This continued to bug me and I've now found out why my script doesn't work in Windows 7. It turns out that that Get-ChildItem -Path '%SourceVar%' -Recurse | Unblock-File is not supported in the default version of PowerShell in Windows 7, i.e. PowerShell 2. As soon as I installed PowerShell 3 my script worked. It's a pain installing PowerShell 3 so I'll just use a different method.
Hello RickC. I wonder which method you used in the end. I am also dealing with the same problem. I cannot find out if some files on the local network are blocked.
RickC
Posts: 253
Joined: 27 Oct 2013, 08:32

Re: Unblocker script works in Win 10 but not Win 7

24 Nov 2020, 09:31

hasantr wrote:
24 Nov 2020, 06:58
Hello RickC. I wonder which method you used in the end. I am also dealing with the same problem. I cannot find out if some files on the local network are blocked.
It was 4 years ago now and I have only one device left still running Windows 7 (and that now uses PowerShell 5... so it became a moot point).

At the time I ended up using Internet Explorer Options (Security > Downloads > Custom level) and Windows Firewall to turn automatic blocking off for downloaded files (i.e. so a Zone.Identifier alternate data stream wasn't added in the first place). I can't find the original article I used but here's a similar one: https://www.techwalla.com/articles/how-do-i-stop-windows-from-blocking-my-downloads

Nir Sofer's AlternateStreamView (https://www.nirsoft.net/utils/alternate_data_streams.html) may help you identify which downloaded files have this flag set and qwerty12's post earlier in this thread may help with scripting the removal of any alternate data streams. Sorry I don't have a ready-to-use solution for Windows 7 but it's no longer an issue for me.

Hope this helps...
User avatar
hasantr
Posts: 779
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Unblocker script works in Win 10 but not Win 7

25 Nov 2020, 00:56

RickC wrote:
24 Nov 2020, 09:31
hasantr wrote:
24 Nov 2020, 06:58
Hello RickC. I wonder which method you used in the end. I am also dealing with the same problem. I cannot find out if some files on the local network are blocked.
It was 4 years ago now and I have only one device left still running Windows 7 (and that now uses PowerShell 5... so it became a moot point).

At the time I ended up using Internet Explorer Options (Security > Downloads > Custom level) and Windows Firewall to turn automatic blocking off for downloaded files (i.e. so a Zone.Identifier alternate data stream wasn't added in the first place). I can't find the original article I used but here's a similar one: https://www.techwalla.com/articles/how-do-i-stop-windows-from-blocking-my-downloads

Nir Sofer's AlternateStreamView (https://www.nirsoft.net/utils/alternate_data_streams.html) may help you identify which downloaded files have this flag set and qwerty12's post earlier in this thread may help with scripting the removal of any alternate data streams. Sorry I don't have a ready-to-use solution for Windows 7 but it's no longer an issue for me.

Hope this helps...
These solutions would be too indirect for my situation. I have to continue my code with a different approach.
Thank you.

Return to “Ask For Help”

Who is online

Users browsing this forum: hasantr, songdg and 46 guests