Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (84 sloc) 4.58 KB
<#
.Synopsis
Invoke a PowerShell scriptblock in a Nav container
.Description
If you are running as administrator, this function will create a session to a Nav Container and invoke a scriptblock in this session.
If you are not an administrator, this function will create a PowerShell script in the container and use docker exec to launch the PowerShell script in the container.
.Parameter containerName
Name of the container in which you want to invoke a PowerShell scriptblock
.Parameter scriptblock
A pre-compiled PowerShell scriptblock to invoke
.Parameter argumentList
Arguments to transfer to the scriptblock in form of an object[]
.Example
Invoke-ScriptInNavContainer -containerName dev -scriptblock { $env:UserName }
.Example
[xml](Invoke-ScriptInNavContainer -containerName dev -scriptblock { Get-Content -Path (Get-item 'c:\Program Files\Microsoft Dynamics NAV\*\Service\CustomSettings.config').FullName })
#>
function Invoke-ScriptInNavContainer {
Param(
[Parameter(Mandatory=$true)]
[string]$containerName,
[Parameter(Mandatory=$true)]
[ScriptBlock] $scriptblock,
[Parameter(Mandatory=$false)]
[Object[]] $argumentList
)
if ($usePsSession) {
$session = Get-NavContainerSession -containerName $containerName -silent
Invoke-Command -Session $session -ScriptBlock $scriptblock -ArgumentList $argumentList
} else {
$file = Join-Path $containerHelperFolder ([GUID]::NewGuid().Tostring()+'.ps1')
$outputFile = "$file.output"
try {
if ($argumentList) {
$encryptionKey = $null
$xml = [xml]([System.Management.Automation.PSSerializer]::Serialize($argumentList))
$nsmgr = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xml.NameTable
$nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/powershell/2004/04");
$nodes = $xml.SelectNodes("//ns:SS", $nsmgr)
if ($nodes.Count -gt 0) {
$encryptionKey = New-Object Byte[] 16
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($encryptionKey)
'$encryptionkey = [System.Management.Automation.PSSerializer]::Deserialize('''+([xml]([System.Management.Automation.PSSerializer]::Serialize($encryptionKey))).OuterXml+''')' | Add-Content $file
}
foreach($node in $nodes) {
$node.InnerText = ConvertFrom-SecureString -SecureString ($node.InnerText | ConvertTo-SecureString) -Key $encryptionkey
}
'$xml = [xml](''' + $xml.OuterXml + ''')' | Add-Content $file
if ($encryptionKey) {
'$nsmgr = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xml.NameTable' | Add-Content $file
'$nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/powershell/2004/04")' | Add-Content $file
'$nodes = $xml.SelectNodes("//ns:SS", $nsmgr)' | Add-Content $file
'foreach($node in $nodes) { $node.InnerText = ConvertFrom-SecureString -SecureString ($node.InnerText | ConvertTo-SecureString -Key $encryptionKey) }' | Add-Content $file
}
'$argumentList = [System.Management.Automation.PSSerializer]::Deserialize($xml.OuterXml)' | Add-Content $file
}
'$runPath = "c:\Run"
$myPath = Join-Path $runPath "my"
function Get-MyFilePath([string]$FileName)
{
if ((Test-Path $myPath -PathType Container) -and (Test-Path (Join-Path $myPath $FileName) -PathType Leaf)) {
(Join-Path $myPath $FileName)
} else {
(Join-Path $runPath $FileName)
}
}
. (Get-MyFilePath "prompt.ps1") -silent | Out-Null
. (Get-MyFilePath "HelperFunctions.ps1") | Out-Null
$txt2al = $NavIde.replace("finsql.exe","txt2al.exe")
Set-Location $runPath
' | Add-Content $file
'$result = Invoke-Command -ScriptBlock {' + $scriptblock.ToString() + '} -ArgumentList $argumentList' | Add-Content $file
'if ($result) { [System.Management.Automation.PSSerializer]::Serialize($result) | Set-Content "'+$outputFile+'" }' | Add-Content $file
docker exec $containerName powershell $file
if (Test-Path -Path $outputFile -PathType Leaf) {
[System.Management.Automation.PSSerializer]::Deserialize((Get-content $outputFile))
}
} finally {
Remove-Item $file -Force -ErrorAction SilentlyContinue
Remove-Item $outputFile -Force -ErrorAction SilentlyContinue
}
}
}
Export-ModuleMember -function Invoke-ScriptInNavContainer
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.