尝试从Internet机运行ps1文件时,unblock-file可以摆脱提示警告

时间:2018-11-21 02:54:58

标签: powershell

我想基于远程计算机上的exif数据重命名照片,代码如下:

Unblock-File -path ..\exif-datetaken.ps1

Get-ChildItem *.jpg | foreach {
#Write-Host "$_`t->`t" -ForegroundColor Cyan -NoNewLine 
$date = (..\exif-datetaken.ps1 $_.FullName)
if ($date -eq $null) {
    Write-Host '{ No ''Date Taken'' in Exif }' -ForegroundColor Cyan    
    return
}
$newName = $date.ToString('yyyy-MM-dd HH-mm-ss') + $_.extension
$newName = (Join-Path $_.DirectoryName $newName)
Write-Host $newName -ForegroundColor Cyan
mv $_ $newName
}

我使用unblock-file摆脱警告,因为我需要单击按钮手动确认每张照片, 但是我发现取消阻止文件不起作用,我仍然收到警告提示,

解决此问题的方法是否错误?

1 个答案:

答案 0 :(得分:0)

您从网络上下载的几乎所有文件都将附加ADS(备用日期流),这表明该文件来自网络,因此不值得信任。

您必须使用Unblock-File摆脱该ADS。

如果要对大量下载的文件执行此操作,则必须对所有文件都执行此操作,然后对它们执行任何其他操作。

ForEach ($TargetPath in $TargetPaths)
{
    $TargetPath
    Get-ChildItem -Path $TargetPath -Recurse | Unblock-File
    Start-Sleep -Seconds 1
}

# Your rename code here

如果在下载任何文件后都遇到提示,则可能需要谨慎地实施-Confirm或-Force开关。

Unblock-File -Confirm:$false
Rename-Item -Confirm:$false

取消锁定实际上仅是下载文件所需的。 如果您收到其他提示,则说明文件已锁定或存在权限问题。但是,由于您没有显示错误,因此我们只能猜测。