Brad
Brad

Reputation: 2185

Chef multiple conditions

I have the following chef/powershell code:

# Configure Registry Keys
powershell_script 'Configure Registry Keys' do
    code <<-EOH

    # Set the default location for data files
    Set-ItemProperty -Path "HKLM:\\Software\\Microsoft\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQLServer" -Name DefaultData -Value "D:\\Program Files\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQL\\DATA"

    # Set the default location for log files
    Set-ItemProperty -Path "HKLM:\\Software\\Microsoft\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQLServer" -Name DefaultLog -Value "L:\\Program Files\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQL\\DATA"

    # Set the default location for backup files
    Set-ItemProperty -Path "HKLM:\\Software\\Microsoft\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQLServer" -Name BackupDirectory -Value "B:\\sqlbackups"

    restart-service *sql* -force
    EOH
    guard_interpreter :powershell_script
    not_if "(Get-ItemProperty \"HKLM:\\Software\\Microsoft\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQLServer\").PSObject.Properties.Name -contains \"DefaultData\""
    not_if "(Get-ItemProperty \"HKLM:\\Software\\Microsoft\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQLServer\").PSObject.Properties.Name -contains \"DefaultLog\""
    not_if "(Get-ItemProperty \"HKLM:\\Software\\Microsoft\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQLServer\").PSObject.Properties.Name -contains \"BackupDirectory\""
 end

However when I run it it fails to run:

 * powershell_script[Configure Registry Keys] action run (skipped due to not_if)

That's because one of the keys exists but the other two do not.

What I'd like to happen is for the keys to get created if ANY of them don't exist. It seems like with multiple not_ifs if any of them result in a $true then it skipped.

What I want is that if any of them return $false then execute.

I tried to switch my not_if to an only_if but that seems to have the same problem (just in reverse)

So how can I concatenate multiple not_if or only_if statements together so that they all have to return $false (or all $true - depending on if only_if is being used)

Hopefully this makes sense but please let me know if it doesn't :)

Upvotes: 0

Views: 983

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19644

Here's a test for any of them not existing:

not_if "$Name=(Get-ItemProperty 'HKLM:\\Software\\Microsoft\\Microsoft SQL Server\\MSSQL14.MSSQLSERVER\\MSSQLServer').PSObject.Properties.Name; $Name -contains 'DefaultData' -and $Name -contains 'DefaultLog' -and $Name -contains 'BackupDirectory'"

Upvotes: 2

Related Questions