Home > database >  Update web.config at azure release with powershell
Update web.config at azure release with powershell

Time:12-22

i need update the web.config when project is released at azure release. Add child at <system.webServer>. I tried with some many examples but always end in error with 'PowerShell' task in the release. Im getting this error

2022-12-19T16:09:40.9984812Z True   v4.0.30319     C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.IO.Compression.FileSystem\v4.0_4.0.0.0__b77a...
2022-12-19T16:09:41.1401626Z Exception calling "Open" with "2" argument(s): "Empty path name is not legal."
2022-12-19T16:09:41.1402644Z At C:\AzureAgents\Agent1\_work\_temp\aad4d934-d9ac-485c-9511-d8cb4e50e6d4.ps1:14 char:1
2022-12-19T16:09:41.1403839Z   $zip =  [System.IO.Compression.ZipFile]::Open($zipfileName.FullName," ...
2022-12-19T16:09:41.1404348Z   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-12-19T16:09:41.1404890Z       CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
2022-12-19T16:09:41.1405353Z       FullyQualifiedErrorId : ArgumentException

Heres the PS.

# cd to the agent artifacts directory (where the zip file exist)
cd $env:Agent_ReleaseDirectory
cd _Your.Project-CI\drop
$fileToEdit = "web.config"


[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem");
# Open zip and find the particular file (assumes only one inside the Zip file)
$zipfileName = dir -filter '*.zip'

$zip =  [System.IO.Compression.ZipFile]::Open($zipfileName.FullName,"Update")


$configFile = $zip.Entries.Where({$_.name -like $fileToEdit})

# Read the contents of the file
$desiredFile = [System.IO.StreamReader]($configFile).Open()
$text = $desiredFile.ReadToEnd()
$desiredFile.Close()
$desiredFile.Dispose()

$contentToAdd1 = @'
    <system.webServer>
    <modules>
    <albert>
    <remove name="WebDAVModule" />
    </modules>
'@
#$text[3] = $text[3] -replace '<system.webServer>',$contentToAdd1
$text = $text -replace '<system.webServer>',$contentToAdd1
 
#update file with new content
$desiredFile = [System.IO.StreamWriter]($configFile).Open()
$desiredFile.BaseStream.SetLength(0)

# Insert the $text to the file and close
$desiredFile.Write($text)
$desiredFile.Flush()
$desiredFile.Close()

# Write the changes and close the zip file
$zip.Dispose()

CodePudding user response:

Please add a $Sourcefolder in your script like below to check:

$Sourcefolder= "$env:Agent_ReleaseDirectory\_Your.Project-CI\drop"

$zipfileName = dir $Sourcefolder -filter '*.zip'

And change Open($zipfileName.FullName,"Update") to OpenRead($zipfileName.FullName):

$zip = [System.IO.Compression.ZipFile]::OpenRead($zipfileName.FullName)
  • Related