The objective is:

  • command: To delete any file that uses the name of our target link so the command do not fail.
  • command: To create a junction link in windows using puppet
  • onlyif: Evaluate that it exists and is a junction link so we do not create it again

exec { ‘CreateMkLink’:
command => ‘Remove-Item C:\chad\imagenes\ ; cmd /c mklink /J C:\chad\imagenes\ C:\data\images’,
provider => powershell,
onlyif => ‘if (dir C:\chad\ -force | ?{$_.LinkType} | select LinkType,Name | ForEach-Object { «$_».trim() } |findstr /R /C:»Junction.*Imagenes}$») { exit 1 } else { exit 0 }’
}

I had to trim the output of the command in the onlyif statement because it has weird whitespaces (when more than one coincidence is found) and I wanted to be explicit about the directory name when using findstr regex.