Need to convert powershell script to chef cookbook

I have one powershell script and that needs to be converted into a chef cookbook.I am unable to understand the conversion of powershell script. Need someone to please help me out oin this.

$MsDeplpoyExe = “C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe”
$pkgLocation = “$PSScriptRoot\pkg”
$publishUrl = “https://:/msdeploy.axd”
$deployUser = “Domain\Administrator”
$deployPassword = “”
$IISSiteName = “testdeploy”

Function Change-SetParameterValue
param ([xml]$xmlDoc, [string]$paramName, [string]$newValue)

# XPath query
$checkParam = $xmlDoc.SelectSingleNode("/parameters/setParameter[@name='$paramName']")

if($checkParam -ne $null)
    Echo "Setting '$paramName' with value '$newValue'"
else { Echo "Parameter '$paramName' not found!" }


#Builds VS proj
function DeployPrj($projName)
$zipFile = “$pkgLocation$”
$paramFile = “$pkgLocation$projName.SetParameters.xml”

[xml]$xml = Get-Content $paramFile;

if($xml -eq $null) { Write-Error 'Unable to get XML document, please check directory path' -ErrorAction Stop }
#change the parameters file so the to set web.config settings for this instance
Change-SetParameterValue $xml 'IIS Web Application Name' $IISSiteName

$arguments ="-source:package='$zipFile'",

& $MsDeplpoyExe $arguments


DeployPrj "CMSApp"
DeployPrj “CMSApp_AppCode”

Here is a Chef article on using PowerShell script code in a cookbook:

For your $MsDeployExe variable, you will most likely need to use double whacks ("\") for your value. Basically any variable that uses a single backslash will need to be converted to double-whacks.

A disclaimer: I haven’t used chef on Windows yet, so what I’m writing here is theoretical.

The fundamental difference between the PowerShell script and chef is that the powershell script describes actions - the steps to take. You would generally only run a PowerShell script once. Chef describes the desired state. You can run it as many times as you want, and it will only take actions the first time (or whenever something actually needs to be done). Very often, to actually accomplish what you need, you would embed a PowerShell script in Chef in the first place.

With that said, why not leave the PowerShell script intact (or extract bits and pieces, as needed) and write a Chef wrapper that calls it?

That said, your PowerShell script seems to pretty much just be a wrapper around msdeploy.exe? There is an msdeploy cookbook in the supermarket at . It is several years old, but you could always clone it and use it as a starting point for what you actually need.

Kevin Keane
Whom the IT Pros Call
The NetTech
Our values: Privacy, Liberty, Justice

Can you please explain what EOH plays role in the powershell_script defination. I am getting an error while running cookbook is "Name space error initializer not defined::EOH

Thanks in advance…

Can you share the contents of your cookbook recipe?

powershell_script ‘deploy’ do
guard_interpreter :powershell_script
code <<-EOH
$arguments =’-source:package=“C:\Install\pkg\MYCVO\”’, ‘-dest:auto’, ‘-setParam:name="IIS’, ‘Web’, ‘Application’, ‘Name",value=“Check”’, ‘- setParam:name="CMSConnectionString-Web.config’, ‘Connection’, ‘String",value="Server=Ericson\SQL2014EXPRESS;’, ‘Database=Test_catia_upgrade;’, ‘User’, ‘Id=catia;’, ‘Password=password1;"’, ‘-verb:sync’,’-enableRule:DoNotDeleteRule’, '-allowUntrusted’

Error is : Namespace error not initialize ;EOH

Thanks in advance


Rohit, for starters, make sure to include double-backslashes “\”.


Should be:


Also, how are you calling your cookbook from Chef? I was not able to repro the EOH namespace error.