0


I am trying to encrypt the web.config file after deploying automatically.

As per the Link :https://blogs.iis.net/msdeploy/archive/2013/07/09/webdeploy-3-5-rtw.aspx

I am using below command:
msdeploy.exe –verb:sync –source:iisapp=”sourceTestSite” –dest:iisapp=”destinationTestSite” –EnableRule:EncryptWebConfig

But then I am getting error:
Error Code: ERROR_FAILED_TO_ENCRYPT_WEB_CONFIG

I do not want to first encrypt and then deploy. I am thinking to run deployment script and after deployment it should encrypt automatically probably using MSDEploy command.

I tried below threads but did not get any help:
Failed to encrypt destination web.config when using MS build plugin in Jenkins

Also I wanted to keep my secret file in separate location but I found encryption process will not work for that
How to encrypt a file linked to a web.config


This time I am trying to run command on remote server to encrypt the web.config file by using below code. I am running below code in my machine and trying to encrypt the web.config file present on my myRemoteServer.

$currentDirectory = (Get-Location)
$user = "domain1\username1"
$section = "appSettings" 
$app= "/MyWeb"
$version="v4.0.30319"
$computername ="myRemoteServer"
$pwd = ConvertTo-SecureString -String "mysecret@11" -AsPlainText -Force

$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$pwd

$encryptcmd1= Set-Location "C:\windows\Microsoft.Net\Framework\$version"

$encryptCmd2 = ".\aspnet_regiis.exe -pe ""appSettings"" -app ""/MyWeb"""

$encryptCmd = "$encryptcmd1 $encryptcmd2"
try
{
invoke-command -ComputerName $computername -Credential $credential -ScriptBlock {$encryptCmd}
}
catch 
{
    Log-Message $_
}

Set-Location $currentDirectory

It doesn't throw any exception. However it is not working and not encrypting web.config file on that server. I want to know where / what is wrong here.

Community
  • 1
  • 1

1 Answers1

-1

his function will encrypt a sections of a web.config file.

function Encrypt-ConfigurationSection([int] $id, [string] $app, [string] $section, [string] $version){
$currentDirectory = (Get-Location)
Set-Location "C:\windows\Microsoft.Net\Framework\$version\"
.\aspnet_regiis.exe -pe $section -app $app -site $id -prov "RsaProtectedConfigurationProvider"
Set-Location $currentDirectory
}

Example call

Encrypt-ConfigurationSection 1 ‘/WebApplication1’ ‘connectionStrings’ ‘v4.0.30319’

This function will decrypt a sections of a web.config file.

function Decrypt-ConfigurationSection([int] $id, [string] $app, [string] $section, [string] $version){
$currentDirectory = (Get-Location)
Set-Location "C:\windows\Microsoft.Net\Framework\$version\"
.\aspnet_regiis.exe -pd $section -app $app -site $id
Set-Location $currentDirectory
}

Example Call

Decrypt-ConfigurationSection 1 ‘/WebApplication1’ ‘connectionStrings’ ‘v4.0.30319’

I got help from this website:

https://joshjoubert.wordpress.com/2013/03/28/encrypting-and-decrypting-sections-of-a-web-config-with-powershell/