文档指出目录中的加密属性只是一个标志,指示其所有子级都应该加密。
对于文件,您可以使用
切换加密(Get-Item -Path filename).Decrypt()
(Get-Item -Path filename).Encrypt()
这些方法在
FileInfo
中定义,在 DirectoryInfo
中不存在。在这两种情况下,您都不能直接设置属性,即这不会执行任何操作:
(Get-Item -Path filename).Attributes -= 'Encrypted'
(此类属性设置适用于
Archive
和 ReadOnly
等,但不适用于 Compressed
、Encrypted
、Directory
等)
我想做的是:
这可以通过脚本实现吗?
注意:我确实不想想先填充目录,然后再对每个文件调用
Decrypt()
;这并不能解决所有新文件未加密的问题。
这并不明显(您必须想知道为什么
System.IO.DirectoryInfo
实例不公开 .Encrypt()
和 .Decrypt()
方法,因为您必须想知道为什么尝试通过 Encrypted
删除 .Attributes
属性是 静静地忽略),但是 System.IO.File
类具有静态 .Encrypt()
和 .Decrypt()
方法,它们也可以在 directory 路径上操作。
因此:
# Create a new dir. inside an encrypted dir., which by default
# will have the Encrypted attribute set too.
$dirInfo = New-Item -Type Directory -Path $encryptedDir -Name NewUnencryptedDir
# Remove the Encrypted attribute, so that files and subdirs. created inside
# will be unencrypted.
# Note: Be sure to always pass a *full* path, because .NET's current dir.
# usually differs from PowerShell's.
[IO.File]::Decrypt($dirInfo)
在尝试各种解决方案后,我无法禁用文件夹的加密标志,包括授予每个人完全控制权、终止文件夹上打开句柄的所有进程等,然后遇到这个问题,并且证书的指纹是正确的一个。
mkelement0 的解决方案几乎成功了。
因此,在我的例子中,这是成功取消文件夹标记的单行命令:
[IO.File]::Decrypt((get-item "<foldername>" -Force))