There is actually a way to apply conditional logic to workaround the fact that the file type does not support setting attributes using an exec. I would recommend the approach that I've outlined below as it relies on cmd.exe instead of PowerShell, and will therefore execute much more quickly.
The dir commands take switches for the attributes which can then be used to query the file system, to ensure idempotency. For instance:
C:\>dir /ah c:\pagefile.sys
|
Volume in drive C is Windows 2008
|
Volume Serial Number is D8AA-E11A
|
|
Directory of c:\
|
|
03/23/2016 04:33 PM 536,870,912 pagefile.sys
|
1 File(s) 536,870,912 bytes
|
0 Dir(s) 6,025,601,024 bytes free
|
|
C:\>dir /ah c:\missing.file
|
Volume in drive C is Windows 2008
|
Volume Serial Number is D8AA-E11A
|
|
Directory of c:\
|
|
File Not Found
|
|
C:\>dir /ah c:\inetpub
|
Volume in drive C is Windows 2008
|
Volume Serial Number is D8AA-E11A
|
|
Directory of c:\inetpub
|
|
File Not Found
|
I have implemented a manifest that will write foo.txt and mark it as hidden when it's not already hidden:
$attrib = 'h'
|
$filepath = 'c:\foo.txt'
|
|
file { $filepath:
|
ensure => present,
|
content => 'bar'
|
}
|
|
exec { 'foo attributes':
|
command => "c:\\windows\\system32\\cmd.exe /c attrib +${attrib} ${filepath}",
|
onlyif => "c:\\windows\\system32\\cmd.exe /c dir \\a${attrib} ${filepath} | findstr \"File Not Found\"",
|
require => File[$filepath]
|
}
|
If you wish to extend the example to multiple attributes, some modifications would be necessary (or you'd have to call exec multiple times) – the attrib command can take an argument like +shr, but dir must have the args separated like /as /ah /ar. Exercise left up to the reader if that's necessary.
|