我的其他Puppet代码运行正常。我可以从下面的驱动程序开始成功安装打印机。但是我遇到的是“除非”被忽略。对Puppet中的PowerShell不太熟悉,但是也许有人可以发现我所缺少的东西。
$printers.each |Integer $index, Hash $printer| {
exec { "add printer driver ${printer[driver_name]} for ${printer[name]}":
command => "Add-PrinterDriver -Name \"${printer[driver_name]}\"",
unless => "& if((Get-PrinterDriver -Name \"${printer[driver_name]}\" - ErrorAction SilentlyContinue) -eq $null){exit 1}else{exit 0} }",
provider => powershell,
我尝试过
"& if((Get-PrinterDriver -Name \"${printer[driver_name]}\" - ErrorAction SilentlyContinue) -eq $null){exit 1}else{exit 0} }",
和"& { exit ((Get-PrinterDriver -Name \"${printer[driver_name]}\" -ErrorAction Stop) -eq $null) }",
答案 0 :(得分:2)
我发现在运行--debug
和--verbose
时,$ null找不到任何值。我需要使用$null
转义\$null
并删除else {exit 0}
。
下面的新代码。
exec { "add printer driver ${printer[driver_name]} for ${printer[name]}":
command => "Add-PrinterDriver -Name \"${printer[driver_name]}\"",
unless => "if ((Get-PrinterDriver -Name \"${printer[driver_name]}\" -ErrorAction SilentlyContinue) -eq \$null) {exit 1}",
provider => powershell,
}