We help IT Professionals succeed at work.

Hoist the colors! We’ve added location flags to usernames sitewide, so it's easier to connect with the global community on EE. View My Profile

x

PowerShell | Parsing log file for certain line

GyroTwister
GyroTwister asked
on
450 Views
Last Modified: 2017-12-05
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

Comment
Watch Question

Jose Gabriel Ortega CastroCEO Faru Bonon IT&Agency /Top-Rated Freelancer (Upwork)/Photographer
CERTIFIED EXPERT
Awarded 2018
Distinguished Expert 2018

Commented:
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

Jeremy WeisingerSenior Network Consultant / Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
GyroTwisterSenior Windows Specialist

Author

Commented:
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 WeisingerSenior Network Consultant / Engineer
CERTIFIED EXPERT

Commented:
Let us know if you have any questions.
Senior Windows Specialist
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Jeremy WeisingerSenior Network Consultant / Engineer
CERTIFIED EXPERT

Commented:
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... ?
GyroTwisterSenior Windows Specialist

Author

Commented:
-
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.