Skip to content

Run Test-Path before Remove-Item to prevent Health Checker exception#516

Merged
bill-long merged 1 commit into
microsoft:mainfrom
lusassl-msft:lusassl-testscriptversionfix
Apr 23, 2021
Merged

Run Test-Path before Remove-Item to prevent Health Checker exception#516
bill-long merged 1 commit into
microsoft:mainfrom
lusassl-msft:lusassl-testscriptversionfix

Conversation

@lusassl-msft

@lusassl-msft lusassl-msft commented Apr 23, 2021

Copy link
Copy Markdown
Contributor

Issue:
Health Checkers makes use of the new Test-ScriptVersion function. In case an update is processed we're trying to remove an existing HealthChecker.ps1 and HealthChecker.old file which may be there caused by previous update. In case there is no HealthChecker.ps1 in Temp and/or no HealthChecker.old we run into an error on HealthChecker (caused by how we handle errors in the script). We show the following:

image

Reason:
This is caused by the Remove-Item call against a non-existing HealthChecker.ps1 and/or non-existing HealthChecker.old files (update never happend before).

[DBG]: PS C:\Users\administrator.EXCHANGELABS\Desktop\hc>> $error[0]
Remove-Item : Cannot find path 'C:\Users\ADMINI~1.EXC\AppData\Local\Temp\HealthChecker.ps1' because it does not exist.
At C:\Users\administrator.EXCHANGELABS\Desktop\hc\HealthChecker.ps1:255 char:17
+ ...             Remove-Item $tempFullName -Force -Confirm:$false -ErrorAc ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\ADMINI...althChecker.ps1:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Fix:
This can be easily avoided by running Test-Path before Remove-Item.

Validation:
Validated in Lab. Possible Pester test case.

@bill-long bill-long merged commit f05fbd8 into microsoft:main Apr 23, 2021
@lusassl-msft lusassl-msft deleted the lusassl-testscriptversionfix branch April 23, 2021 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants