如何通过 Puppet exec 命令设置 Powershell ExecutionPolicy with unless 属性 on Windows?
How to set Powershell ExecutionPolicy via Puppet exec command with unless property on Windows?
我正在使用 Puppet Enterprise 版本 3.8.6 来管理 Windows 服务器。
我试图使 Powershell 执行策略的设置幂等。我测试了几个假设,最后一个是这样的:
# Setting Powershell Execution Policy to unrestricted
exec { 'Set PowerShell execution policy unrestricted':
command => 'Set-ExecutionPolicy Unrestricted',
unless => 'if ((Get-ExecutionPolicy).ToString().Equals("Unrestricted")) { exit 0 } else { exit 1 }',
provider => powershell
}
我已经用双引号和单引号进行了测试,甚至在无限制的单词上转义了双引号(例如 \"Unrestricted\")。
我也测试了命令,但没有成功:
(Get-ExecutionPolicy).ToString(). -eq "Unrestricted"
它将 ExecutionPolicy 更改为不受限制,但在每个 Puppet 运行 中。它一直落在 else 子句中。该命令适用于 Powershell。
我希望它只在需要时应用。
仅供参考:我已经检查了 Puppet 文档并在线搜索了。
我检查了一些链接:
http://glennsarti.github.io/blog/powershell-puppet-module-exit-codes/
https://docs.puppet.com/pe/latest/windows_config_mgmnt.html#executing-arbitrary-powershell-code
我认为这是由于 PowerShell 模块调用 PowerShell 的方式 - 它通过了 -ExecutionPolicy Bypass
as part of the powershell.exe startup arguments,因此本地作用域将始终 return 绕过,从而导致它每次都在 unless 上失败。
尝试将 -Scope LocalMachine
添加到您的 unless 语句中。
# Setting Powershell Execution Policy to unrestricted
exec { 'Set PowerShell execution policy unrestricted':
command => 'Set-ExecutionPolicy Unrestricted',
unless => 'if ((Get-ExecutionPolicy -Scope LocalMachine).ToString() -eq "Unrestricted") { exit 0 } else { exit 1 }',
provider => powershell
}
我正在使用 Puppet Enterprise 版本 3.8.6 来管理 Windows 服务器。 我试图使 Powershell 执行策略的设置幂等。我测试了几个假设,最后一个是这样的:
# Setting Powershell Execution Policy to unrestricted
exec { 'Set PowerShell execution policy unrestricted':
command => 'Set-ExecutionPolicy Unrestricted',
unless => 'if ((Get-ExecutionPolicy).ToString().Equals("Unrestricted")) { exit 0 } else { exit 1 }',
provider => powershell
}
我已经用双引号和单引号进行了测试,甚至在无限制的单词上转义了双引号(例如 \"Unrestricted\")。 我也测试了命令,但没有成功:
(Get-ExecutionPolicy).ToString(). -eq "Unrestricted"
它将 ExecutionPolicy 更改为不受限制,但在每个 Puppet 运行 中。它一直落在 else 子句中。该命令适用于 Powershell。 我希望它只在需要时应用。
仅供参考:我已经检查了 Puppet 文档并在线搜索了。 我检查了一些链接: http://glennsarti.github.io/blog/powershell-puppet-module-exit-codes/ https://docs.puppet.com/pe/latest/windows_config_mgmnt.html#executing-arbitrary-powershell-code
我认为这是由于 PowerShell 模块调用 PowerShell 的方式 - 它通过了 -ExecutionPolicy Bypass
as part of the powershell.exe startup arguments,因此本地作用域将始终 return 绕过,从而导致它每次都在 unless 上失败。
尝试将 -Scope LocalMachine
添加到您的 unless 语句中。
# Setting Powershell Execution Policy to unrestricted
exec { 'Set PowerShell execution policy unrestricted':
command => 'Set-ExecutionPolicy Unrestricted',
unless => 'if ((Get-ExecutionPolicy -Scope LocalMachine).ToString() -eq "Unrestricted") { exit 0 } else { exit 1 }',
provider => powershell
}