我正在编写一个salt状态,它将检查注册表项的值。这是返回'NULL‘。我怀疑我的'getregvalue‘变量没有用我传递给cmd.run的变量进行解析。当-Path和-Name值被硬编码时,这就像预期的那样工作。如何动态正确地传递-Path和-Name变量?
{% for regconfig in registryconfig.settings %}
{% set getregvalue = salt['cmd.run']('Get-ItemPropertyValue -Path regconfig.hive ~ ":\" ~ regconfig.path -Name regconfig.vname',shell='powershell') %}
{% endfor %}
c:\temp\debug.txt:
file.managed:
- contents: |
{{ getregvalue |yaml(False) |indent(8) }}这是配置文件
{%- load_yaml as registrysettings %}
dev:
settings:
- hive: 'HKLM'
path: 'SOFTWARE\Database Settings\dbstate'
vname: ConnectionString
{%- endload %}
{%- set registryconfig = salt['grains.filter_by'](registrysettings,
grain='env') %}发布于 2022-11-08 13:57:14
您要确保字符串被引用,而代码没有:
("Get-ItemPropertyValue -Path " ~ regconfig.hive ~ ":\\" ~ regconfig.path ~ " -Name " ~ regconfig.vname, shell="powershell")如果路径和名称也需要引用PowerShell,那么也包括这些:
("Get-ItemPropertyValue -Path '" ~ regconfig.hive ~ ":\\" ~ regconfig.path ~ "' -Name '" ~ regconfig.vname ~ "'", shell="powershell")发布于 2022-11-09 15:38:14
{% set getregvalue = salt['cmd.run']("Get-ItemPropertyValue -Path '" ~ regconfig.hive ~ ":\\" ~ regconfig.path ~ '" -Name '" ~ regconfig.vname ~ "'", shell='powershell') %}
感谢“橙狗”为我指明了正确的方向。引用字符串和参数为我解决了这个问题。
https://stackoverflow.com/questions/74355749
复制相似问题