Unblock a file with PowerShell?

女生的网名这么多〃 提交于 2019-11-27 05:24:44

问题


I am trying to have PowerShell unblock a file in Win2K8 R2.

Does anyone have a pointer as to the syntax?


回答1:


If you are using PowerShell v3, you can use the Unblock-File cmdlet.


The "blocking" part is simply an alternate data stream of the file, named "Zone.Identifier". You can display it in CMD by using input redirection (no other way to get to a stream in CMD, though):

H:\Downloads> more < test.exe:Zone.Identifier
[ZoneTransfer]
ZoneId=3

You can find them using dir /r on Windows Vista and later:

2009-10-24  12:18        54.538.056 test.exe
                                 24 test.exe:Zone.Identifier:$DATA

Also in CMD you can easily get rid of that by overwriting it (using output redirection, this time):

echo.>myDownloadedFile.exe:Zone.Identifier

which isn't quite the same as removing the ADS completely, but works in that Explorer doesn't complain anymore.

There doesn't seem to be native support for handling ADS from within PowerShell (as mentioned on The PowerShell Guy's blog here. That article also has some information how to get that functionality in PowerShell). You could, however, simply call cmd:

cmd /c "echo.>test.exe:Zone.Identifier"

That works from PowerShell as well.

Another option would be Mark Russinovich's streams utility which allows you to inspect a file's ADS and also to delete them. So

streams -d myDownloadedFile.exe

does work as well.




回答2:


The PoshCode module includes Set-DownloadFlag and Remove-DownloadFlag functions which work as advertised. :) I've just pulled that piece out into it's own script contribution http://poshcode.org/1430 ... it will work on PowerShell 1 too, if you use the New-Type function in place of Add-Type ( http://poshcode.org/720 )




回答3:


Oneliner to remove zone informarion(inspired by accepted answer) for all children (with correct quoting).

get-childitem -rec | % { cmd /c "echo.>""$($_.FullName)"":Zone.Identifier" }

Not strictly answer to the question, just want to make sure when I next come up with this problem there is solution already :).

PS. Works in PS 2.0




回答4:


new to posting in forums like this and this might be an old topic but here is what you are looking for.

get-item -Path "path to file(s)" -Stream "Zone.Identifier" -ErrorAction "SilentlyContinue"

This should list out files that are blocked only.

Unblock-File -Path "Path to blocked file(s)"

This will unblock them.




回答5:


Remove the alternate file stream using Streams.exe see this post: http://www.paraesthesia.com/archive/2010/05/19/unblocking-multiple-files-at-once.aspx




回答6:


I wrote a little function that uses the Win32 API to delete the Zone.Identifier NTFS alternate data stream which is what Windows uses to determine whether a file is to be blocked.

.NET doesn't have access to alternate data streams so the function uses a technique called platform invoking to call the native Win32 API. The benefit of this over the some other solutions for PowerShell is that it supports the PowerShell pipeline so you can pipe a list of file paths or System.IO.FileInfo objects to the function. The function also doesn't have any external dependencies and actually deletes the alternate data stream instead of just deleting it's contents.

http://andyarismendi.blogspot.com/2012/02/unblocking-files-with-powershell.html




回答7:


To unblock a folder and it's subfolder recursive (>= PowerShell v3) you can use the Get-ChildItem (gci) command:

Get-ChildItem "C:\Temp\" -recurse | Unblock-File

where C:\Temp is the starting folder.




回答8:


I haven't seen any answer yet that seems to use the proper powershell cmdlets to do this.

Here we can find DLLs in the current folder that contain the zone.identifier:

Get-Item -Path .\*.dll -stream * | where {$_.Stream -eq "Zone.Identifier" }

Here we zap just only the unwanted streams, unlike some answers above that might damage other streams:

Remove-Item  -Path .\*.dll -stream Zone.Identifier



回答9:


If you server does not have Powershell > v3 ($PSVersionTable.PSVersion.Major -ge 3). Then use good old reliable DOS:

for /f "tokens=*" %f in ('dir /b *.*') do echo.>"%f":Zone.Identifier 



回答10:


If you are using PowerShell 3.0 or above vesion, Unblock-file PowerShell cmdlet should solve this problem with unblocking the file, even though if you don't have unblock button on the file properties window.

The Unblock-File cmdlet lets you open files that were downloaded from the Internet. It unblocks Windows PowerShell script files that were downloaded from the Internet so you can run them, even when the Windows PowerShell execution policy is RemoteSigned. By default, these files are blocked to protect the computer from untrusted files.

Just open the powerShell window and follow below syntax. To find more information about the syntax go to here

Example :

unblock-file -path C:\Downloads\MyFileName.chm

Unblock file with PowerShell screen shot

Warning: Do not unblock unsecure files.




回答11:


I'll have to amend @Mike 's answer: this won't work if there are spaces in $_.FullName (e.g. like in "C:\Program Files") so it has to be:

get-childitem -rec | % { cmd /c "echo.>""$($_.FullName)"":Zone.Identifier" }



回答12:


You can search for blocked files like this:

get-item * -stream zone*

Then to unblock the files, pipe that to remove-item or "rm" to delete the zone.identifier streams:

get-item * -stream zone* | Remove-Item

In case you want recursive search:

get-childitem -recurse | get-item -stream zone*



回答13:


Do you mean this:

set-executionpolicy remotesigned

This will allow you to execute local scripts without them being signed, and remote ones if they are signed. More info available here.



来源:https://stackoverflow.com/questions/1617509/unblock-a-file-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!