On Monday, April 15, 2013 9:14:21 AM UTC-5, Gary Park wrote:
Hello,
I am in the process of "trying out" Puppet, and so far, it is going really well, and I can see a clear line of how we can use it internally.
Ideally, what I would like to do is to only run a PowerShell exec command, if a web page doesn't currently exist on the server (i.e. the PowerShell script is responsible for deploying the Web Pages (into SharePoint in this case) and I only want to run this step, if these pages don't already exist. To that end, I have done something like this:
onlyif => '$webRequest = [System.Net.WebRequest]::Create("
http://some-url.test.aspx"); $webRequest.UseDefaultCredentials = $true; try { if([int]$webRequest.GetResponse().StatusCode -eq "200") { exit 0; } else { exit 1; } } catch [System.Net.WebException] { exit 1; }'
Which, at the command line, has the correct result. However, when I try to run this, I get an error saying that $webRequest is not recognised.
Puppet invokes the specified command directly, not via the [standard | Power] shell, so whether that works at the (some) command line is irrelevant.
Which leads me to think that using variables within the onlyif is not supported. Is that correct? If so, what is the best approach for doing this, or am I going up the wrong path?
No, that's not correct. I suppose you expect '$webRequest' to be meaningful to (and the whole command sequence to be executed by) PowerShell, but you haven't told Puppet to run it via PowerShell. Instead, you've told Puppet to execute a command named literally '$webRequest'. Refer to the docs on the Exec type's "windows" provider for information and examples of how to make this sort of thing work:
docs.puppetlabs.com/references/3.1.latest/type.html#exec . What they say about the 'command' parameter applies equally to 'onlyif'.
John