Avatar of GyroTwister
Flag for Netherlands asked on

PowerShell | Parsing log file for certain line

Hallo all,

I'm trying to monitor the process of uninstalling a SCCM client from a computer. Normally you should give the command "C:\Windows\ccmsetup\ccmsetup.exe /uninstall" and wait for it.

However the uninstall process keeps track of its progress in a logfile which can be found here: "C:\Windows\ccmsetup\Logs\ccmsetup.log".

When the uninstall is completed the last line of the logfile should start with: "<![LOG[CcmSetup is exiting with return code 0]LOG]". Here after there is more information displayed however that is not needed the determine the uninstall has completed successfully.

I seem unable to catch the last line and stop the job and go on with the other steps of uninstalling. Anyone any ideas? This is my code so far:
$SCCMUninstall = "C:\Windows\ccmsetup\ccmsetup.exe /uninstall"
$CCMLog = "C:\Windows\ccmsetup\Logs\ccmsetup.log"

Start-Job $SCCMUninstall

While (! (Test-Path $CCMLog)) {Sleep -Seconds 10}

Write-Host ("Monitoring " + $CCMLog)

Get-Content -Path $CCMLog -Tail 1 -Wait | Where {$_ -like "*<![LOG[CcmSetup is exiting with return code 0]LOG]*"}

Write-Host ("SCCM Client succesfully uninstalled!")

Open in new window


Avatar of undefined
Last Comment

8/22/2022 - Mon
Jose Gabriel Ortega Castro

In the -like try this one:

$_ -match "\<\!\[LOG\[CcmSetup is exiting with return code 0\]LOG\]"

So the whole line would be:
Get-Content -Path $CCMLog -Tail 1 -Wait | Where {$_ -match "\<\!\[LOG\[CcmSetup is exiting with return code 0\]LOG\]"}

Also I redo some lines:

    [Parameter(Position=0,Mandatory=$false)]$CCMLog = "C:\Windows\ccmsetup\Logs\ccmsetup.log",
    [Parameter(Position=1,Mandatory=$false)]$SCCMUninstall = "C:\Windows\ccmsetup\ccmsetup.exe /uninstall"

    $job =Start-Job $SCCMUninstall
    While (! (Test-Path $CCMLog)) {Sleep -Seconds 10}
    Write-Host ("Monitoring $CCMLog")

    $job | Wait-Job
    #Try it for a file that contains it and then remove the #from the below lines. "#for( and #}"
        $answer=Get-Content -Path $CCMLog -Tail 1 | Where {$_ -match "\<\!\[LOG\[CcmSetup is exiting with return code 0\]LOG\]"}
    Write-Host ("SCCM Client succesfully uninstalled!")

Open in new window

Jeremy Weisinger

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Hello all,

Thanks for the first pointers. It is still not working as I expect but with the provided solutions from you, I am testing a better script. Once it works I'll let you know.
Jeremy Weisinger

Let us know if you have any questions.
Your help has saved me hundreds of hours of internet surfing.
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Jeremy Weisinger

To get the status, you would need to be getting feedback from the uninstall process and then know how to interpret that feedback to give relevant status info. I'm not sure how to do that.

You could show the timestamp of when the uninstall started and that might be a good enough gauge to know how much time is left... ?