While loop not exiting

I’m trying to figure out why my while loop never exits:

This script waits for a Window Scheduled Task to no longer exist and then it should exit the loop, but it is waiting even though I go in and delete the Scheduled Task.

$taskName = “Remove EPM-gent”
$taskExists = Get-ScheduledTask | Where-Object {$_. TaskName }

while($taskExists) {
Start-Sleep -Seconds 10
} else {

Where-Object {$_.TaskName }

This will always return a value since “TaskName” is a property for all tasks. You need to refine your search to something like:

Get-ScheduledTask | Where-Object{$_.TaskName -Match $TaskName}
1 Like

Also you store tasks in $taskExists once and do nothing to clear that out or recheck in the loop.


Could you elaborate?

taskExists gets assigned a value once, before the loop therefore it always has a value. while($taskExists) will always evaulate to true. You need some type of condition within the loop that, under one or more conditions, will set the value of $taskExists to $null otherwise you get your infinite loop.

Since you basically already have a valid way to check I would suggest a do while loop

$taskName = "Remove EPM-agent"
do {
    Start-Sleep -Seconds 10
    $task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
} while ($task)
1 Like

Another question here sort of related.

In my script near the end it calls my function called “IsComplete” which checks for the existence of folders and writes to the log accordingly … writing to the log is another function.

So I have “IsComplete” called and after that the copy log logic. It never seems to get to the copying of the logs.

Here is “IsComplete” and after is the copy log logic.

function IsComplete {
$Ivanti = Test-Path –Path “C:\Program Files (x86)\Ivanti”;
$Landesk = Test-Path –Path “C:\Program Files (x86)\Landesk”;

if (($Ivanti -eq $false) -and ($Landesk -eq $false)){

WriteLog “### SUCCESS ###”
WriteLog “Uninstall Complete, Please reboot”

Elseif (($Landesk -eq $true) -and ($Ivanti -eq $false)){

WriteLog "Uninstaller was unable to remove all LANDesk folders. Please disable any security applications and try again or manually remove the LANDesk folders. "

Elseif (($Ivanti -eq $true) -and ($Landesk -eq $false)){

WriteLog "Uninstaller was unable to remove all Ivanti folders. Please disable any security applications and try again or manually remove the LANDesk folders. "


WriteLog “Uninstaller was unable to remove all product folders. Please disable any security applications and try again.”




Here is the copy log logic:


$file = Get-Content 'C:\ProgramData\EPMUninstallConsole\EPMUninstallConsole.exe.log'
$containsWord = $file | %{$_ -match "### SUCCESS ###"}
$hostname = $env:computername

     if ($containsWord -contains $true) {
       Copy-Item -Path 'C:\ProgramData\EPMUninstallConsole\EPMUninstallConsole.exe.log' -Destination \\EPM2022-TECO\EPM_AGENT_REMOVAL_LOGS\Success\$hostname-success.log -force
       } else { 
       Copy-Item -Path 'C:\ProgramData\EPMUninstallConsole\EPMUninstallConsole.exe.log' -Destination \\EPM2022-TECO\EPM_AGENT_REMOVAL_LOGS\Failure\$hostname-fail.log -force

Welcome to the forum. :wave:t3:

Please help us helping you. It is hard to distinguish between prosa and code when you don’t format your code properly.

So please go back, edit your posts once again and fix the formatting of your code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

I’d suggest starting a new thread for a new question. It can be super confusing following chains where lots of folk are participating trying to assist, and sometimes adding a new question a) causes additional confusion and b) won’t get the visibility because its a question under an existing thread.

1 Like

Is there a way to delete my post altogether?

I am curious. Why would you like to do that? And have you tried to figure that out by yourself?