windows - 使用模拟用户在 File.Encrypt 上获取 System.IO.IOException

标签 windows powershell chef-infra test-kitchen

我正在尝试以编程方式加密文件夹(使用 Windows EFS)。以下 powershell 代码在通过 ISE powershell 控制台运行时工作正常。

$obj = New-Object -TypeName System.IO.FileInfo 'D:\Temp'      
$obj.Encrypt()

然而,通过带有测试厨房的 Chef Recipe 在模拟用户下运行此命令会产生以下错误

powershell 的配方包装器:

ruby_block 'Enable encryption on folder' do
  block do
 command = <<-EOH
   # Encrypt the folder
   $obj = New-Object -TypeName System.IO.FileInfo 'D:\\Temp'      
   $obj.Encrypt()
   EOH
   powershell_out!(command, { user: username, password: pwd,
                           domain: domain})
  end
end

产生以下堆栈跟踪:

PSMessageDetails      : 
Exception             : System.Management.Automation.MethodInvocationException:
                     Exception calling "Encrypt" with "0" argument(s): 
                    "The parameter is incorrect.
                    " ---> System.IO.IOException: The parameter is 
                    incorrect.

                       at System.IO.__Error.WinIOError(Int32 errorCode, 
                    String maybeFullPath)
                       at System.IO.File.Encrypt(String path)
                       at CallSite.Target(Closure , CallSite , Object )
                       --- End of inner exception stack trace ---
                       at System.Management.Automation.ExceptionHandlingOps
                    .CheckActionPreference(FunctionContext funcContext, 
                    Exception exception)
                       at System.Management.Automation.Interpreter.ActionCa
                    llInstruction`2.Run(InterpretedFrame frame)
                       at System.Management.Automation.Interpreter.EnterTry
                    CatchFinallyInstruction.Run(InterpretedFrame frame)
                       at System.Management.Automation.Interpreter.EnterTry
                    CatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          : 
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : IOException
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 5
PipelineIterationInfo : {}

注意:被模拟的用户是 Administrators 组的一部分,并且对 D:\Temp 具有完全控制权。另一个关键观察结果是,如果我在通过测试厨房运行 chef 之前以该用户的身份执行交互式登录(RDP session ),则上面的方法成功且没有任何问题,并且文件夹在本地加密,就像在控制台中以交互方式运行 powershell 一样。

异常似乎表明 System.IO.File.Encrypt 的路径参数未在内部某处设置/传递,但我不知道为什么这一次有效,而另一次无效。我确实尝试通过让配方作为该用户针对本地主机运行 Invoke-Command 来创建用户配置文件,它确实创建了配置文件,例如(C:\users\... 已创建),但是 Encrypt() 调用仍然出错,出现与上述相同的异常。 这看起来不像是 Chef 或测试厨房的问题,而是在加密文件系统复杂性的 powershell/windows 方面遗漏了一些东西,非常感谢任何帮助。

谢谢

最佳答案

不幸的是,我不相信您可以通过远程 session 加密文件。如果您要在 native Windows PowerShell 远程 session 上尝试相同的操作,您将收到 UnauthorizedAccessException。如果您使用的是 Test-Kitchen,请尝试使用提升的 winrm 传输。

示例 yaml:

平台: - 名称:eval-win2012r2-standard 操作系统类型:Windows 运输: 名称:winrm 高架:真

这会模拟本地登录 session 并可能解决此错误。

关于windows - 使用模拟用户在 File.Encrypt 上获取 System.IO.IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42799719/

相关文章:

ruby - Chef 和 ruby​​ 模板——如何遍历键值对?

node.js - Node ffi - GetWindowRect

windows - 批量 IF 变量比较神秘地不起作用

powershell - Test-AzureRmResourceGroupDeployment 不验证嵌套资源

powershell如何使用Windows方法将名称解析为IP地址

c# - PowerShell参数取值建议

ubuntu - 使用 Chef 开源服务器部署带有密码的私有(private) git 存储库

attributes - Chef 属性与数据袋

windows - Abstract Window Toolkit 的 JDK 中的 Native API 调用在哪里

windows - 欧拉问题和 Int64 类型递归的性能问题