registry - 使用 Chef 访问另一个用户的注册表

标签 registry chef-infra chef-windows

是否可以使用 Chef 访问其他用户的注册表?我有 chef-client 作为系统运行,我想修改 User1 的注册表?有没有办法做到这一点?

registry_key资源提供了一种访问 HKEY_Users 的方法,但我看不到将用户名映射到 SID 的方法。

最佳答案

这最终变得温和令人费解,看着它让我感到畏缩。但它似乎有效!

我想通过注册表修改另一个用户的环境变量,如 this Server Fault answer 中所述,但我也想用 Chef 创建用户。那么问题是 Windows 不会为用户创建注册表配置单元,直到他们登录。

如果相关用户确实存在,您可以跳到修改用户的注册表项

强制 Windows 创建用户注册表配置单元

Chef 内置的 executebatch 资源似乎不支持提供密码,所以它看起来不像是 user任何一个的属性都可以在 Windows 上使用。但是the official Chef Windows cookbook包含一个 windows_task 资源,该资源确实包含一个用于指定用户密码的属性。

现在的问题是授予相关用户“作为批处理作业登录”的本地安全策略权限。为此,我们可以使用 SecEdit .

确保您的 Recipe 依赖于官方的 Chef Windows Recipe ;在您的 Recipe 的 metadata.rb 文件中添加:

depends "windows"

这是 Chef Recipe 代码:

group "BatchJobUsers" do
  append true
  members ["AnotherUser"]
  action :create
end

cookbook_file "BatchJobUsers-AddBatchLogonRight.inf" do
  path "#{ENV['TEMP']}\\BatchJobUsers-AddBatchLogonRight.inf"
  action :create_if_missing
end

execute "secedit" do
  cwd "#{ENV['TEMP']}"
  command "secedit /configure /db secedit.sdb /cfg BatchJobUsers-AddBatchLogonRight.inf"
end

windows_task "force-creation-of-AnotherUser-user-registry-hive" do
  command "echo Force creation of AnotherUser user registry hive"
  user "AnotherUser"
  password "password-for-another-user"
  action [:create, :run]
end

windows_task "force-creation-of-AnotherUser-user-registry-hive" do
  action :delete
end

您需要向 COOKBOOK/files/default 目录添加一个名为 BatchJobUsers-AddBatchLogonRight.inf 的文件;它应该包含以下内容:

[Unicode]
Unicode=yes
[Version]
signature="$CHICAGO$"
Revision=1
[Privilege Rights]
SeBatchLogonRight = "BatchJobUsers"

修改用户的注册表项

确保您的 Recipe 依赖于官方的 Chef Windows Recipe ;在您的 Recipe 的 metadata.rb 文件中添加:

depends "windows"

在您的 Recipe 中,添加以下行:

::Chef::Recipe.send(:include, Windows::RegistryHelper)

然后您可以像这样在您的配方中使用函数 resolve_user_to_sid:

get_user_sid = lambda { resolve_user_to_sid("USER_NAME") }

registry_key "Create environment variable registry keys" do
  key lazy { "HKEY_USERS\\#{ get_user_sid.call }\\Environment"
  values [{
      :name => "Variable",
      :type => :string,
      :data => "variable_data"
          }]
  recursive true
  action :create
end

必须延迟评估 key 属性(即在 Chef 运行配方的收敛阶段评估)以处理不存在的用户。

关于registry - 使用 Chef 访问另一个用户的注册表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22945786/

相关文章:

ruby - Chef Run(Omnibus 安装)中的 PATH 在哪里强制执行/配置?

amazon-ec2 - 测试厨房:如何读取 kitchen.yml 中的平台特定属性

knife - Chef 客户端未安装在目标 Windows 计算机上

ms-access - 存在 32 位 Office 时手动安装 64 位 MS Access ODBC 驱动程序

c# - 访问 Visual Studio 2017 的私有(private)注册表配置单元

c# - 在 C# 中为所有用户设置注册表项

c++ - ActiveQt Com 应用程序示例 - COM 服务器未在 Windows 注册表中注册 (Qt4.7.4)

ruby - 关于 Ruby/ChefSpec 编码风格的反馈

ssl - Windows Chef 添加 ssl 证书并绑定(bind)到 IIS