Link to home
Start Free TrialLog in
Avatar of GyroTwister
GyroTwisterFlag 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 J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

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:

[CmdletBinding()]
param(
    [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 #}"
    do{
        $answer=Get-Content -Path $CCMLog -Tail 1 | Where {$_ -match "\<\!\[LOG\[CcmSetup is exiting with return code 0\]LOG\]"}
        if($answer){
            $isitdone=$true
            break;
        }
    }
    while(!($isitdone))
    Write-Host ("SCCM Client succesfully uninstalled!")

Open in new window

SOLUTION
Avatar of Jeremy Weisinger
Jeremy Weisinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GyroTwister

ASKER

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.
Avatar of Jeremy Weisinger
Jeremy Weisinger

Let us know if you have any questions.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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... ?
-