Hello, I’m new to the deploy toolkit project and looking for a little help. I have a custom function in AppDeployToolkitExtensions.ps1 that uninstalls Appx packages. I’m leveraging the Remove-AppxProvisionedPackage cmdlet but I want logging so that’s why the custom function:
Function Uninstall-AppxPackage
{
<#
.DESCRIPTION
Uninstall-AppxPackage removes all AppX package and provisionings matching the specified package name.
.PARAMETER PackageName
The name of the package to uninstall. Performs a contains match on the application display name by default.
.PARAMETER Exact
Specifies that the named application must be matched using the exact name.
.PARAMETER LogName
Overrides the default log file name.
.EXAMPLE
Uninstall-AppxPackage -PackageName 'Microsoft.Office.Excel_16001'
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)][string]$PackageName,
[Parameter(Mandatory = $false)][string]$LogName = "",
[Parameter(Mandatory = $false)][switch]$Exact = $false
)
$Source = $MyInvocation.MyCommand
If ($Exact) { $Name = $PackageName }
Else { $Name = "*$PackageName*" }
$appxProvisionedPackages = Get-AppxProvisionedPackage -Online | Where-Object { $_.PackageName -like $Name }
Write-Log -Message "Found [$($appxProvisionedPackages.Count)] application(s) that matched the specified criteria [$Name]." -Source $Source
If ($appxProvisionedPackages.Count -gt 0)
{
ForEach ($appxProvisionedPackage In $appxProvisionedPackages)
{
$OEMLogFile = $configMSILogDir + '\' + $appxProvisionedPackage.DisplayName + ' ' + $appxProvisionedPackage.Version + ' Uninstall.log'
If ($LogName -ne "") { $OEMLogFile = $configMSILogDir + '\' + $LogName }
$DismLogContent = ""; If (Test-Path ($env:windir + '\logs\DISM\dism.log')) { $DismLogContent = Get-Content -Path ($env:windir + '\logs\DISM\dism.log') -Raw }
Write-Log -Message "Remove provisioning for $($appxProvisionedPackage.PackageName)" -Source $Source
$appxProvisionedPackage | Remove-AppxProvisionedPackage -Online
Write-Log -Message "Remove application $($appxProvisionedPackage.PackageName)" -Source $Source
Get-AppxPackage -AllUsers | Where-Object { $_.PackageFullName -like ($appxProvisionedPackage.DisplayName + "_" + $appxProvisionedPackage.Version + "*") } | Remove-AppxPackage
Try
{
If ($DismLogContent.Length -gt 0) { Set-Content -Path $OEMLogFile -Value ((Get-Content -Path ($env:windir + '\logs\DISM\dism.log') -Raw).Replace($DismLogContent, '')) }
Else { Set-Content -Path $OEMLogFile -Value (Get-Content -Path ($env:windir + '\logs\DISM\dism.log') -Raw) }
Write-Log -Message "OEM log file $OEMLogFile generated." -Source $Source
}
Catch
{
Write-Log -Message "Failed to create OEM log file $OEMLogFile." -Source $Source -Severity 3
If (-not $ContinueOnError) { Throw "Failed to create OEM log file $OEMLogFile." }
Return
}
}
}
Else
{
Write-Log -Message "No applications that matched the specified criteria [$Name] were found for removal. Continue..." -Source $Source
}
}
The problem I’m having is that Write-Log isn’t actually writing anything to the log file. Other functions in AppDeployToolkitExtensions.ps1 that leverage Write-Log work as expected. And if I run the code in Sapien I can see the output that I’d expect to be written to the log.
The function is called in Deploy-Application.ps1 like this:
$AppxPackageNames = @('Microsoft.Office.Excel_16001', 'Microsoft.Office.PowerPoint_16001', 'Microsoft.Office.Word_16001')
ForEach ($package In $AppxPackageNames) { Uninstall-AppxPackage -PackageName $package }
#$AppxPackageNames | ForEach-Object { Uninstall-AppxPackage -PackageName $_ }
I actually use the Remove-MSIApplications right before this and it logs fine too. I’m kinda out of ideas on what the problem could be. Any help would be greatly appreciated.
Edit: Everything else in the function works as expected.