Wanted to share a quick tip with you today regarding using PowerShell from within Chef. More specifically, using the dsc_resource to run PowerShell script. I routinely use this resource to run PowerShell on Windows nodes through Chef. In this case, the test script always returned false. The test was a simple test-path call against a directory and even after I knew the folder was created, the test continued to return false.
After much testing I found the only difference between the test script that worked and this one was the type of slash I was using. The problematic test was using back slashes and the others were all using forward slashes.
Notice the differences on line 5 of each sample. The only difference is the type of slash. Changing to forward slashes fixed the issue.
I ended up spending hours on this. Please learn from my mistakes. I suspect this is likely a problem in other areas where you embed PowerShell within Chef but haven’t tested this further.
Backslash is an escape character in double quotes, so you’d need double backalashes.
I should have thought of that. Thanks Steve!
In many languages yes, but not in PowerShell strings. The backtick ` is the escape character and not the backslash \
Right, but the above example is first evaluated via the ruby interpreter before being passed to powershell.