Need to convert powershell script to chef cookbook


#1

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'"
    $checkParam.Value=$newValue
}
else { Echo "Parameter '$paramName' not found!" }

}

#Builds VS proj
function DeployPrj($projName)
{
$zipFile = “$pkgLocation$projName.zip”
$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
$xml.Save($paramFile);


$arguments ="-source:package='$zipFile'",
    "-dest:auto,computerName='$publishUrl',userName='$deployUser',password='$deployPassword',authType='Basic',includeAcls='False'",
    "-setParamFile:$paramFile",
    "-verb:sync",
    "-enableRule:DoNotDeleteRule",
    "-allowUntrusted"

& $MsDeplpoyExe $arguments

}

DeployPrj "CMSApp"
DeployPrj “CMSApp_AppCode”


#2

Here is a Chef article on using PowerShell script code in a cookbook: https://docs.chef.io/resource_powershell_script.html

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.


#3

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 https://supermarket.chef.io/cookbooks/msdeploy . 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
http://www.4nettech.com
Our values: Privacy, Liberty, Justice
See https://www.4nettech.com/corp/the-nettech-values.html


#4

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…


#5

Can you share the contents of your cookbook recipe?


#6

@kswem
powershell_script ‘deploy’ do
guard_interpreter :powershell_script
code <<-EOH
$arguments =’-source:package=“C:\Install\pkg\MYCVO\CMSApp.zip”’, ‘-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’
EOH
end

Error is : Namespace error not initialize ;EOH

Thanks in advance


#7

Thanks


#8

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

package="C:\Install\pkg\MYCVO\CMSApp.zip"
Server=Ericson\SQL2014EXPRESS

Should be:

package="C:\\Install\\pkg\MYCVO\\CMSApp.zip"
Server=Ericson\\SQL2014EXPRESS

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