Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script which can be used to transform an XML file using XDT. All you need to do is download the script and call it. The script will download the files it needs to run.
#!/usr/bin/env powershell
<#
.SYNOPSIS
You can use this script to easly transform any XML file using XDT.
To use this script you can just save it locally and execute it. The script
will download its dependencies automatically.
Created by sayediHashimi
Modified by obscurerichard
Thanks Stack Overflow: https://stackoverflow.com/questions/8989737/web-config-transforms-outside-of-microsoft-msbuild
#>
[cmdletbinding()]
param(
[Parameter(
Mandatory=$true,
Position=0)]
$sourceFile,
[Parameter(
Mandatory=$true,
Position=1)]
$transformFile,
[Parameter(
Mandatory=$true,
Position=2)]
$destFile,
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = 'http://nuget.org/nuget.exe'
)
<#
.SYNOPSIS
If nuget is not in the tools
folder then it will be downloaded there.
#>
function Get-Nuget(){
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = 'http://nuget.org/nuget.exe'
)
process{
$nugetDestPath = Join-Path -Path $toolsDir -ChildPath nuget.exe
if(!(Test-Path $nugetDestPath)){
'Downloading nuget.exe' | Write-Verbose
# download nuget
$webclient = New-Object System.Net.WebClient
$webclient.DownloadFile($nugetDownloadUrl, $nugetDestPath)
# double check that is was written to disk
if(!(Test-Path $nugetDestPath)){
throw 'unable to download nuget'
}
}
# return the path of the file
$nugetDestPath
}
}
function GetTransformXmlExe(){
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\")
)
process{
if(!(Test-Path $toolsDir)){
New-Item -Path $toolsDir -ItemType Directory | Out-Null
}
$xdtExe = (Get-ChildItem -Path $toolsDir -Include 'SlowCheetah.Xdt.exe' -Recurse) | Select-Object -First 1
if(!$xdtExe){
'Downloading xdt since it was not found in the tools folder' | Write-Verbose
# nuget install Microsoft.Web.Xdt Microsoft.Web.Xdt -Version 2.1.1 OutputDirectory toolsDir
$cmdArgs = @('install','Microsoft.Web.Xdt','-Version','2.1.1','-OutputDirectory',(Resolve-Path $toolsDir).ToString())
'Calling nuget.exe to download Microsoft.Web.XmlTransform with the following args: [{0} {1}]' -f (Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl), ($cmdArgs -join ' ') | Write-Verbose
&(Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl) $cmdArgs | Out-Null
$cmdArgs = @('install','SlowCheetah.Xdt','-Prerelease','-Version','1.1.7-beta','-OutputDirectory',(Resolve-Path $toolsDir).ToString())
# nuget install SlowCheetah.Xdt -Prerelease -Version 1.1.7-beta -OutputDirectory toolsDir
'Calling nuget.exe to download SlowCheetah.Xdt with the following args: [{0} {1}]' -f (Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl), ($cmdArgs -join ' ') | Write-Verbose
&(Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl) $cmdArgs | Out-Null
$xdtExe = (Get-ChildItem -Path $toolsDir -Include 'SlowCheetah.Xdt.exe' -Recurse) | Select-Object -First 1
# copy the xdt assemlby if the xdt directory is missing it
$xdtDllExpectedPath = (Join-Path $xdtExe.Directory.FullName 'Microsoft.Web.XmlTransform.dll')
if(!(Test-Path $xdtDllExpectedPath)){
# copy the xdt.dll next to the slowcheetah .exe
$xdtDll = (Get-ChildItem -Path $toolsDir -Include 'Microsoft.Web.XmlTransform.dll' -Recurse) | Select-Object -First 1
if(!$xdtDll){ throw 'Microsoft.Web.XmlTransform.dll not found' }
Copy-Item -Path $xdtDll.Fullname -Destination $xdtDllExpectedPath | Out-Null
}
}
if(!$xdtExe){
throw ('SlowCheetah.Xdt not found. Expected location: [{0}]' -f $xdtExe)
}
$xdtExe
}
}
function Transform-Xml{
[cmdletbinding()]
param(
[Parameter(
Mandatory=$true,
Position=0)]
$sourceFile,
[Parameter(
Mandatory=$true,
Position=1)]
$transformFile,
[Parameter(
Mandatory=$true,
Position=2)]
$destFile,
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\")
)
process{
# slowcheetah.xdt.exe <source file> <transform> <dest file>
$cmdArgs = @((Resolve-Path $sourceFile).ToString(),
(Resolve-Path $transformFile).ToString(),
$destFile)
'Calling slowcheetah.xdt.exe with the args: [{0} {1}]' -f (GetTransformXmlExe), ($cmdArgs -join ' ') | Write-Verbose
&(GetTransformXmlExe -toolsDir $toolsDir) $cmdArgs
}
}
Transform-Xml -sourceFile $sourceFile -transformFile $transformFile -destFile $destFile
@luis-paulo-silva

This comment has been minimized.

Copy link

@luis-paulo-silva luis-paulo-silva commented Jun 15, 2021

Hi,

I've hit a minor snag while running this and after a while of digging around to figure out the issue I found out that the nuget.exe was actually having a bit of an issue downloading the dependencies.

"Unable to find version '2.1.1' of package 'Microsoft.Web.Xdt'." This was the message I got while debugging the powershell script above.

Tried running it manually and found this out: "WARNING: The underlying connection was closed: An unexpected error occurred on a send."
This usually signals a cipher/protocol issue.

Following the link below we get an answer guiding the troubled developer into a .reg file.
https://stackoverflow.com/questions/20445638/nuget-fails-the-underlying-connection-was-closed-an-unexpected-error-occurred

In sum, if you have the issue above:

  • Create a .reg file
  • Edit the file with notepad

.reg file contents:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001
@sayedihashimi

This comment has been minimized.

Copy link
Owner Author

@sayedihashimi sayedihashimi commented Jun 15, 2021

Thanks @luis-paulo-silva. I found some docs that discuss these keys at https://docs.microsoft.com/en-us/mem/configmgr/core/plan-design/security/enable-tls-1-2-client, they state that if those keys are changed the computer should be restarted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment