MrShushhh
MrShushhh

Reputation: 3

Troubleshoot Powershell script inside Chef recipe

I'm fairly new to Chef but I've gone through all the online tutorials and feel like I have a decent grasp on the basics. I'm trying to run a Powershell script from inside a recipe. The script itself runs fine from Powershell and updates the DNS IP settings as expected. When I run the recipe it also completes without error but the embedded script does't update the DNS settings so I'm assuming it's not executing properly and I'm not sure where to look for errors/logs.

The recipe is posted below and is executed via chef-client.bat --local-mode --runlist 'recipe[prod_server::dns_settings]'

   powershell_script 'Set DNS Servers' do
  code = <<-EOH
  $primary = "10.1.10.2"
     $secondary = "10.1.10.225"
     $DNSServers = "$primary","$secondary"
     $message=""
    function setDNS($DNSServers)
    {
       try
        {
          $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration |where{$_.IPEnabled -eq "TRUE"}
          Foreach($NIC in $NICs)
       {
         $message += $NIC.SetDNSServerSearchOrder(@($DNSServers)) | Out-String   # set the DNS IPs and capture output to string
       }
          }
       catch
          {
      $message += "An error occcured while setting NIC object." + "`n`rError: $_";
          }
       #write-host $message #if necessary, display result messages
    }
    setDNS($DNSServers)
  EOH
     end

Upvotes: 0

Views: 2140

Answers (1)

Nick H.
Nick H.

Reputation: 1616

I'm 95% sure you're actually hitting an error while trying to run the script and not handling it correctly. I know because this exact error just happened to me, a silent failure and for me it was because of the double quotes.

Two Workarounds:

  1. Remove all double quotes. Escaping MIGHT work but I've never had good luck with it.

powershell_script 'Set DNS Servers' do code = <<-EOH $primary = '10.1.10.2' $secondary = '10.1.10.225' $DNSServers = $primary,$secondary $message='' function setDNS($DNSServers) { try { $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration |where{$_.IPEnabled -eq 'TRUE'} Foreach($NIC in $NICs) { $message += $NIC.SetDNSServerSearchOrder(@($DNSServers)) | Out-String # set the DNS IPs and capture output to string } } catch { $message += 'An error occcured while setting NIC object.' + '`n`rError: $_'; } #write-host $message #if necessary, display result messages } setDNS($DNSServers) EOH end

  1. Put the script in a script file, write the file out as a resource, and then notify the execute resource to execute the powershell script:

```

script_path =
    win_friendly_path(File.join(Chef::Config[:file_cache_path],"os-permissions.ps1"))
  script_name = "os-permissions.ps1"

  execute script_name do  
    command "powershell \"#{script_path}\""  
    action :nothing  
  end  

  cookbook_file script_path do
    source "os-permissions.ps1"
    action :create
    notifies :run, "execute[#{script_name}]", :immediately
  end

With # 2, I have a script in my cookbook called os-permissions.ps1 in files\default\

I use # 2 when I can't remove all of the double quotes from my script. I also sometimes execute the scripts directly from the cookbook directly but that's not as straightforward and probably not best-practice (just me being lazy)

Upvotes: 2

Related Questions