Skip to content

Commit 0c483c7

Browse files
committed
Improvements to CloseProcesses handling
1 parent c1b8e26 commit 0c483c7

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/PSAppDeployToolkit.Tools/PSScriptAnalyzer/Measure-ADTCompatibility.psm1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,19 @@ function Measure-ADTCompatibility
18841884
'ForceCloseAppsCountdown' = { "-ForceCloseProcessesCountdown $_" }
18851885
'AllowDeferCloseApps' = '-AllowDeferCloseProcesses' # Should inspect switch values here in case of -Switch:$false
18861886
'CloseApps' = {
1887-
$quoteChar = if ($boundParameters.CloseApps.Value.StringConstantType -eq 'DoubleQuoted') { '"' } else { "'" }
1887+
if ($boundParameters.CloseApps.Value.StringConstantType -eq 'DoubleQuoted')
1888+
{
1889+
$quoteChar = '"'
1890+
}
1891+
elseif ($boundParameters.CloseApps.Value.StringConstantType -eq 'DoubleQuoted')
1892+
{
1893+
$quoteChar = "'"
1894+
}
1895+
else
1896+
{
1897+
# Not a string, pass through original value
1898+
return "-CloseProcesses $_"
1899+
}
18881900
$closeProcesses = $_.ToString().Trim('"').Trim("'") -split ',' | & {
18891901
process
18901902
{

src/PSAppDeployToolkit.Tools/Public/Convert-ADTDeployment.ps1

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ function Convert-ADTDeployment
298298
# Find the hashtable in the v4 template script that holds the adtSession splat
299299
$hashtableAst = $tempScriptAst.Find({
300300
param ($ast)
301-
$ast -is [System.Management.Automation.Language.AssignmentStatementAst] -and $ast.Left.VariablePath.UserPath -eq 'adtSession'
301+
$ast -is [System.Management.Automation.Language.AssignmentStatementAst] -and ($ast.Left | Get-Member -Name VariablePath) -and $ast.Left.VariablePath.UserPath -eq 'adtSession'
302302
}, $true)
303303

304304
if ($hashtableAst)
@@ -333,8 +333,20 @@ function Convert-ADTDeployment
333333
$boundParameters = ($spBinder::BindCommand($saiwAst, $true)).BoundParameters
334334
$closeProcesses = $null
335335
if ($boundParameters.TryGetValue('CloseProcesses', [ref]$closeProcesses)) {
336+
if ($closeProcesses.Value | Get-Member -Name VariablePath)
337+
{
338+
$assignmentAst = $inputScriptAst.Find({
339+
param ($ast)
340+
$ast -is [System.Management.Automation.Language.AssignmentStatementAst] -and ($ast.Left | Get-Member -Name VariablePath) -and $ast.Left.VariablePath.UserPath -eq $closeProcesses.Value.VariablePath.UserPath
341+
}, $true)
342+
$appProcessesToClose = if ($assignmentAst) { $assignmentAst.Right.Extent.Text } else { '@()' }
343+
}
344+
else
345+
{
346+
$appProcessesToClose = $closeProcesses.Value.Extent.Text
347+
}
336348
Write-Verbose -Message "Updating variable [AppProcessesToClose]"
337-
$hashtableContent = $hashtableContent -replace "(?m)(^\s*AppProcessesToClose\s*=)\s*@\(\)", "`$1 $($closeProcesses.Value.Extent.Text)"
349+
$hashtableContent = $hashtableContent -replace "(?m)(^\s*AppProcessesToClose\s*=)\s*@\(\)", "`$1 $appProcessesToClose"
338350
}
339351
}
340352

@@ -392,7 +404,7 @@ function Convert-ADTDeployment
392404
if (Test-Path -LiteralPath $Destination)
393405
{
394406
Write-Verbose -Message "Removing existing destination folder [$Destination]"
395-
Remove-Item -LiteralPath $Destination -Recurse -Force -ErrorAction SilentlyContinue -WhatIf
407+
Remove-Item -LiteralPath $Destination -Recurse -Force -ErrorAction SilentlyContinue
396408
}
397409

398410
# Sometimes previous actions were leaving a lock on the temp folder, so set up a retry loop
@@ -401,18 +413,18 @@ function Convert-ADTDeployment
401413
try
402414
{
403415
Write-Verbose -Message "Moving folder [$tempFolderPath] to [$Destination]"
416+
[System.Threading.Thread]::Sleep(500)
404417
Move-Item -Path $tempFolderPath -Destination $Destination -Force -PassThru:$PassThru
405418
Write-Information -MessageData "Conversion successful: $Destination"
406419
break
407420
}
408421
catch
409422
{
410-
Write-Verbose -Message "Failed to move folder. Trying again in 500ms."
411-
[System.Threading.Thread]::Sleep(500)
412423
if ($i -eq 4)
413424
{
414425
throw
415426
}
427+
Write-Verbose -Message "Failed to move folder. Trying again in 500ms."
416428
}
417429
}
418430

0 commit comments

Comments
 (0)