PowerShell | Parsing log file for certain line

GyroTwister
GyroTwister used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jose Gabriel Ortega CastroEE Rookie/Solution Guide/Topic Advisor and CEO Faru Bonon IT
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
Commented:
Get-Content with the -wait parameter is dynamic. So you need to break it for the script to move on. So even if it matches it will just sit there still monitoring the file if you don't break it.

Try this:

$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]*"} | %{break}

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

Open in new window

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.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Jeremy WeisingerSenior Network Consultant / Engineer

Commented:
Let us know if you have any questions.
Senior Windows Specialist
Commented:
Well after a few days of crunching code I came up with the following script to uninstall and re-install the SCCM Client:

<# Declaring Variables #>
$CCMDir = "C:\Windows\CCM"
$CCMLog = "C:\Windows\ccmsetup\Logs\ccmsetup.log"
$CCMRegKey = "HKLM:\Software\Microsoft\CCMSetup"
$CCMSetup = "C:\Windows\ccmsetup\ccmsetup.exe"
$CCMSetupArg = " /uninstall"
$CCMSetupDir = "C:\Windows\ccmsetup"

$NewSetup = "\\<MgmtPoint>\SMS_<SMSSiteCode>\Client\ccmsetup.exe"
$NewSetupArg = " /mp:<MgmtPoint> /logon SMSSITECODE=<SMSSiteCode> FSP=<FallBackPoint>"

$SMSCFG = "C:\Windows\SMSCFG.ini"
$SMSMIF = "C:\Windows\SMS*.mif"
$SMSRegKey = "HKLM:\Software\Microsoft\SMS"

Clear-Host

<# Delete an existing log file #>
If (Test-Path $CCMLog) {Remove-Item $CCMLog; Write-Host "Deleting existing Logfile" -ForegroundColor Green}

<# Uninstalling SCCM Client #>
Start-Process $CCMSetup $CCMSetupArg

Do {
    $Answer = Get-Process ccmsetup -ErrorAction SilentlyContinue
    If (! ($Answer)) {
        Write-Host "CCMSetup not yet started, sleeping......" -ForegroundColor Green
        Start-Sleep -Seconds 2
        }
    }
    Until ($Answer)

<# Clear Variable for Re-Use #>
Clear-Variable Answer

<# Check for existence of log-file #>
If (! (Test-Path $CCMLog)) {Start-Sleep -Seconds 2}
Write-Host "Monitoring LogFile" -ForegroundColor Green

<# Monitoring the log file for successfull uninstall of client #>
Do {
    $Answer = Get-Content -Path $CCMLog -Tail 1 | Where {$_ -match "\<\!\[LOG\[CcmSetup is exiting with return code 0\]LOG\]"}
    If ($Answer) {
        $IsItDone = $True
        break;
        }
    }
    Until ($IsItDone)

<# Clear Variable for Re-Use #>
Clear-Variable Answer
Clear-Variable IsItDone
    
<# Check if process has stopped #>
Do {
    $Answer = Get-Process ccmsetup -ErrorAction SilentlyContinue
    If ($Answer) {
        Write-Host "CCMSetup still running, sleeping......" -ForegroundColor Green
        Start-Sleep -Seconds 2
        }
    }
    Until (! ($Answer))

<# Clear Variable for Re-Use #>
Clear-Variable Answer

<# Deletion of obsolete registry key #>
If (Test-Path $CCMRegKey) {Remove-Item -Path $CCMRegKey -Recurse -Force -ErrorAction SilentlyContinue; Write-Host "Deleting CCM Registry" -ForegroundColor Green}
If (Test-Path $SMSRegKey) {Remove-Item -Path $SMSRegKey -Recurse -Force -ErrorAction SilentlyContinue; Write-Host "Deleting SMS Registry" -ForegroundColor Green}

<# Deletion of obsolete directories #>
If (Test-Path $CCMDir) {Remove-Item -Path $CCMDir -Recurse -Force -ErrorAction SilentlyContinue; Write-Host "Deleting directory CCM" -ForegroundColor Green}
If (Test-Path $CCMSetupDir) {Remove-Item -Path $CCMSetupDir -Recurse -Force -ErrorAction SilentlyContinue; Write-Host "Deleting directory CCMSetup" -ForegroundColor Green}

<# Deletion of obsolete files #>
if (Test-Path $SMSCFG) {Remove-Item $SMSCFG; Write-Host "Deleting SMS Config" -ForegroundColor Green}
if (Test-Path $SMSMIF) {Remove-Item $SMSMIF; Write-Host "Deleting SMS*.mif" -ForegroundColor Green}

Write-Host "SCCM Client succesfully uninstalled!" -ForegroundColor Yellow

Write-Host "Sleeping 5 seconds before reinstall of SCCM Client" -ForegroundColor Cyan
Start-Sleep -Seconds 5

<# Installing SCCM Client #>
Start-Process $NewSetup $NewSetupArg -Verb RunAs

Do {
    $Answer = Get-Process ccmsetup -ErrorAction SilentlyContinue
    If (! ($Answer)) {
        Write-Host "CCMSetup not yet started, sleeping......" -ForegroundColor Cyan
        Start-Sleep -Seconds 2
        }
    }
    Until ($Answer)

<# Clear Variable for Re-Use #>
Clear-Variable Answer

<# Check for existence of log-file #>
If (! (Test-Path $CCMLog)) {Start-Sleep -Seconds 2}
Write-Host "Monitoring LogFile" -ForegroundColor Cyan

<# Monitoring the log file for successfull install of client #>
Do {
    $Answer = Get-Content -Path $CCMLog -Tail 1 | Where {$_ -match "\<\!\[LOG\[CcmSetup is exiting with return code 0\]LOG\]"}
    If ($Answer) {
        $IsItDone = $True
        break;
        }
    }
    Until ($IsItDone)

<# Clear Variable for Re-Use #>
Clear-Variable Answer
Clear-Variable IsItDone
    
<# Check if process has stopped #>
Do {
    $Answer = Get-Process ccmsetup -ErrorAction SilentlyContinue
    If ($Answer) {
        Write-Host "CCMSetup still running, sleeping......" -ForegroundColor Cyan
        Start-Sleep -Seconds 2
        }
    }
    Until (! ($Answer))

Write-Host "SCCM Client successfully installed!" -ForegroundColor Cyan

Open in new window


This is working quite good. However I could use some pointers how to improve the script. For example I'm looking for an indicator while the program is (in)installing. At this time I only see the line "Monitoring LogFile". Is there a possibility to show something of progress while monitoring the log-file?

Any comment is welcome!
Jeremy WeisingerSenior Network Consultant / Engineer

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial