Reading and Creating SCCM Client Logs with PowerShell

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

Recently, Microsoft released PowerShell v5.1 which includes a new module called System Center Configuration Manager (SCCM) Client Logs that allows administrators to easily read and create client logs in the Windows Event Viewer or log files on disk. This blog will go through how this functionality has been implemented and what one might be able to do with it.

The “force sccm client to check in powershell” is a PowerShell command that allows for the reading and creation of SCCM Client Logs.

Reading and Creating SCCM Client Logs with PowerShell

You’re definitely acquainted with the CMtrace log-reading tool if you’re an SCCM administrator. CMTrace understands the SCCM client logs’ special structure, but did you realize you can do the same thing using PowerShell?

You may read the following using PowerShell:

  • Install logs for SCCM client software
  • Update logs for SCCM clients
  • Client hardware inventory records in SCCM

…and pretty much the rest of them!

The SCCM client logs have a certain format. You must grasp that schema in order to acquire an uniform output from them. Fortunately, it has already been done for you.

Using PowrShell to read SCCM Client Logs

Consider the PowerShell code below, which searches SCCM client logs.

You may query remote machines and give the log name you’d want to examine using the Get-SCCMClientLog PowerShell function, which has two parameters: ComputerName and LogName.

Introduction to PowerShell Functions

After you’ve added this function to your PowerShell console, you may use it as shown below. The example below will obtain the execmgr log file from a remote machine named PC.

Get-SccmClientLog -ComputerName PC -LogName execmgr Get-SccmClientLog -ComputerName PC -LogName execmgr

The function has already done all of the hard work for you.

Creating SCCM Client Logs on Your Own

What if you need to utilize the Get-SCCMClientLog method for anything else? You can even make your own SCCM-style log files!

Reading-and-Creating-SCCM-Client-Logs-with-PowerShellCMTrace is a good example.

Look at how lovely the sortable columns and red highlights are! You would imagine that CMTrace can display any kind of text log, and you’d be correct. However, in CMTrace, have a check at the The file WindowsUpdate.log.

1647501849_978_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellThe file WindowsUpdate.log

Have you noticed that all of the columns have vanished? You can still use CMTrace to see ordinary log files, but you won’t have access to some of the features that make it so useful. You’ll quickly discover that a text file must be correctly structured in order for all of those useful columns to appear, as well as to properly indicate which lines should be highlighted yellow vs. red vs. nothing.

I’d want to demonstrate Write-Log and Start-Log, two PowerShell functions. These routines were created particularly to log the activities of your script in a log file that can subsequently be viewed in CMTrace.

By the conclusion of this article, you’ll have a function that you can use in your scripts to create log files that CMTrace can read correctly.

Start-Log

I created a method named Start-Log to avoid having to give the same SCCM client log file location over and over again. This function should be invoked at the beginning of your script. This method merely generates a text file and (more importantly) sets ScriptLogFilePath, a global variable.

[CmdletBinding()] param ([ValidateScript($ -Parent | Test-Path | Split-Path $ -Parent | Split-Path $ -Parent | Split-Path $ -Parent | Split-Path $ -Parent [string] $FilePath))))))))))))))) If (!(Test-Path $FilePath)) is true, then ## Create the log file New-Item $FilePath -Type File | Out-Null ## Set the global variable $global:ScriptLogFilePath = $FilePath to be used as the FilePath for all future Write-Log calls in this session Write-Error

You’re definitely acquainted with the CMtrace log-reading tool if you’re an SCCM administrator. CMTrace understands the SCCM client logs’ special structure, but did you realize you can do the same thing using PowerShell?

You may read the following using PowerShell:

  • Install logs for SCCM client software
  • Update logs for SCCM clients
  • Client hardware inventory records in SCCM

…and pretty much the rest of them!

The SCCM client logs have a certain format. You must grasp that schema in order to acquire an uniform output from them. Fortunately, it has already been done for you.

Using PowrShell to read SCCM Client Logs

Consider the PowerShell code below, which searches SCCM client logs.

You may query remote machines and give the log name you’d want to examine using the Get-SCCMClientLog PowerShell function, which has two parameters: ComputerName and LogName.

Introduction to PowerShell Functions

After you’ve added this function to your PowerShell console, you may use it as shown below. The example below will obtain the execmgr log file from a remote machine named PC.

Get-SccmClientLog -ComputerName PC -LogName execmgr Get-SccmClientLog -ComputerName PC -LogName execmgr

The function has already done all of the hard work for you.

Creating SCCM Client Logs on Your Own

What if you need to utilize the Get-SCCMClientLog method for anything else? You can even make your own SCCM-style log files!

Reading-and-Creating-SCCM-Client-Logs-with-PowerShellCMTrace is a good example.

Look at how lovely the sortable columns and red highlights are! You would imagine that CMTrace can display any kind of text log, and you’d be correct. However, in CMTrace, have a check at the The file WindowsUpdate.log.

1647501849_978_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellThe file WindowsUpdate.log

Have you noticed that all of the columns have vanished? You can still use CMTrace to see ordinary log files, but you won’t have access to some of the features that make it so useful. You’ll quickly discover that a text file must be correctly structured in order for all of those useful columns to appear, as well as to properly indicate which lines should be highlighted yellow vs. red vs. nothing.

I’d want to demonstrate Write-Log and Start-Log, two PowerShell functions. These routines were created particularly to log the activities of your script in a log file that can subsequently be viewed in CMTrace.

By the conclusion of this article, you’ll have a function that you can use in your scripts to create log files that CMTrace can read correctly.

Start-Log

I created a method named Start-Log to avoid having to give the same SCCM client log file location over and over again. This function should be invoked at the beginning of your script. This method merely generates a text file and (more importantly) sets ScriptLogFilePath, a global variable.

[CmdletBinding()] param ( [ValidateScript({ Split-Path $_ -Parent | Test-Path })] [string]$FilePath ) try { if (!(Test-Path $FilePath)) { ## Create the log file New-Item $FilePath -Type File | Out-Null } ## Set the global variable to be used as the FilePath for all subsequent Write-Log calls in this session $global:ScriptLogFilePath = $FilePath } catch { Write-Error $_.Exception.Message }

This is a very basic function. However, in order to avoid having to give LogFile every time we need to use our Write-Log function in our scripts, it is essential. We can just call Write-Log and it will know the log file location since we created a global variable ahead of time.

Write-Log

You may now perform Write-Log to write SCCM client log messages to the log file after using Start-Log in your script. Message and LogLevel are the two arguments for Write-Log. The message is simple. Simply put, that’s what you’d want to enter into the log. LogLevel requires some explanation.

The line must be recorded in a certain manner for CMTrace to indicate it as red or yellow. It has to have a string that looks like this: type=”1.” This key might be 1,2, or 3 digits long. These are the severity levels in your script.

For instance, if I wanted to log a basic informative message, I’d use a 1. If I want to record a more serious action, I’ll use 2, which will be marked in yellow. Finally, if I want that line highlighted red in CMTrace, I could pick 3.

[Parameter(Mandatory = $true)] param ([Parameter(Mandatory = $true)] param ([Parameter(Mand [string] $Message, [Parameter()] [ValidateSet(1, 2, 3)] [ValidateSet(1, 2, 3)] [ValidateSet(1, ([int]$LogLevel = 1))))))))))))))))

Have you taken notice of the LogLevel parameter? It will set it to 1 by default, but you can always override it if you need to write anything more serious that occurs throughout the execution of your script.

Then you’ll need that helpful date/time column to appear correctly. This needed a precise date/time format, which this string manipulation skill provided.

$TimeGenerated = $$$$$$$ “a dollar (Get-Date -Format HH:mm:ss). $((Get-Date).Millisecond)+000”

Next, I’ll create a log line template that has all of the necessary formatting for the line to appear appropriately in CMTrace.

$Line = ‘<![LOG[{0}]LOG]!><time=”{1}” date=”{2}” component=”{3}” context=”” type=”{4}” thread=”” file=””>’

It’s then a question of constructing what will go in the’s after you’ve obtained the template. I’ll use this to create an array, which I’ll then feed to the $Line to replace all of our’s with actual data.

$LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)”, $LogLevel = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.Script

These are listed in the same sequence as the’s. $Message will be converted to $TimeGenerated, $Today will be converted to today’s date, and $LogLevel will be converted to today’s date.

Have you noticed that I skipped number three? This is the point at which I go all ninja on you. I decided to make advantage of a component column in CMTrace that I had never utilized before.

I needed to know the name of the script and the line number where Write-Log was invoked. “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)” is the command that does this.

I then use PowerShell’s string formatting to combine these two variables to create $Line.

$Line -f = $Line $LineFormat

Then it’s only a question of writing $Line to a text file that Start-Log has already established.

Add-Content -Value is a command that adds content to a document. $ScriptLogFilePath $Line -Path

How Does It Work?

Let’s imagine I write a script named LogDemo.ps1 that looks like this:

C:MyLog.log -FilePath Start-Log “The path to the script log file is [$ScriptLogFilePath],” writes the host. ‘basic activity’ Write-Log -Message Write-Log -Warning -LogLevel 2 -Message ‘warning’ Write-Log -Error -LogLevel 3 -Message ‘Error’

This script creates a log file at C:MyLog.log and then uses the LogLevel arguments to write three different degrees of severity to the log.

When I use Get-Content to inspect the output of this file, it seems to be rather ugly.

<![LOG[simple activity]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:3″ context=”” type=”1″ thread=”” file=””> <![LOG[warning]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:4″ context=”” type=”2″ thread=”” file=””> <![LOG[Error]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:5″ context=”” type=”3″ thread=”” file=””>

Let’s take a look at this in CMTrace and see what we’ve got.

1647501850_593_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellPowerShell creates CMTrace fields.

Isn’t it lovely?

Even if you’re not an SCCM administrator, I strongly suggest that you use CMTrace for all of your log viewing requirements.

Simply open the log files in CMTrace after you’ve received them in the proper format (and you now have no reason not to) and marvel at everything that CMTrace has to offer!

.Exception.Message

You’re definitely acquainted with the CMtrace log-reading tool if you’re an SCCM administrator. CMTrace understands the SCCM client logs’ special structure, but did you realize you can do the same thing using PowerShell?

You may read the following using PowerShell:

  • Install logs for SCCM client software
  • Update logs for SCCM clients
  • Client hardware inventory records in SCCM

…and pretty much the rest of them!

The SCCM client logs have a certain format. You must grasp that schema in order to acquire an uniform output from them. Fortunately, it has already been done for you.

Using PowrShell to read SCCM Client Logs

Consider the PowerShell code below, which searches SCCM client logs.

You may query remote machines and give the log name you’d want to examine using the Get-SCCMClientLog PowerShell function, which has two parameters: ComputerName and LogName.

Introduction to PowerShell Functions

After you’ve added this function to your PowerShell console, you may use it as shown below. The example below will obtain the execmgr log file from a remote machine named PC.

Get-SccmClientLog -ComputerName PC -LogName execmgr Get-SccmClientLog -ComputerName PC -LogName execmgr

The function has already done all of the hard work for you.

Creating SCCM Client Logs on Your Own

What if you need to utilize the Get-SCCMClientLog method for anything else? You can even make your own SCCM-style log files!

Reading-and-Creating-SCCM-Client-Logs-with-PowerShellCMTrace is a good example.

Look at how lovely the sortable columns and red highlights are! You would imagine that CMTrace can display any kind of text log, and you’d be correct. However, in CMTrace, have a check at the The file WindowsUpdate.log.

1647501849_978_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellThe file WindowsUpdate.log

Have you noticed that all of the columns have vanished? You can still use CMTrace to see ordinary log files, but you won’t have access to some of the features that make it so useful. You’ll quickly discover that a text file must be correctly structured in order for all of those useful columns to appear, as well as to properly indicate which lines should be highlighted yellow vs. red vs. nothing.

I’d want to demonstrate Write-Log and Start-Log, two PowerShell functions. These routines were created particularly to log the activities of your script in a log file that can subsequently be viewed in CMTrace.

By the conclusion of this article, you’ll have a function that you can use in your scripts to create log files that CMTrace can read correctly.

Start-Log

I created a method named Start-Log to avoid having to give the same SCCM client log file location over and over again. This function should be invoked at the beginning of your script. This method merely generates a text file and (more importantly) sets ScriptLogFilePath, a global variable.

[CmdletBinding()] param ( [ValidateScript({ Split-Path $_ -Parent | Test-Path })] [string]$FilePath ) try { if (!(Test-Path $FilePath)) { ## Create the log file New-Item $FilePath -Type File | Out-Null } ## Set the global variable to be used as the FilePath for all subsequent Write-Log calls in this session $global:ScriptLogFilePath = $FilePath } catch { Write-Error $_.Exception.Message }

This is a very basic function. However, in order to avoid having to give LogFile every time we need to use our Write-Log function in our scripts, it is essential. We can just call Write-Log and it will know the log file location since we created a global variable ahead of time.

Write-Log

You may now perform Write-Log to write SCCM client log messages to the log file after using Start-Log in your script. Message and LogLevel are the two arguments for Write-Log. The message is simple. Simply put, that’s what you’d want to enter into the log. LogLevel requires some explanation.

The line must be recorded in a certain manner for CMTrace to indicate it as red or yellow. It has to have a string that looks like this: type=”1.” This key might be 1,2, or 3 digits long. These are the severity levels in your script.

For instance, if I wanted to log a basic informative message, I’d use a 1. If I want to record a more serious action, I’ll use 2, which will be marked in yellow. Finally, if I want that line highlighted red in CMTrace, I could pick 3.

[Parameter(Mandatory = $true)] param ([Parameter(Mandatory = $true)] param ([Parameter(Mand [string] $Message, [Parameter()] [ValidateSet(1, 2, 3)] [ValidateSet(1, 2, 3)] [ValidateSet(1, ([int]$LogLevel = 1))))))))))))))))

Have you taken notice of the LogLevel parameter? It will set it to 1 by default, but you can always override it if you need to write anything more serious that occurs throughout the execution of your script.

Then you’ll need that helpful date/time column to appear correctly. This needed a precise date/time format, which this string manipulation skill provided.

$TimeGenerated = $$$$$$$ “a dollar (Get-Date -Format HH:mm:ss). $((Get-Date).Millisecond)+000”

Next, I’ll create a log line template that has all of the necessary formatting for the line to appear appropriately in CMTrace.

$Line = ‘<![LOG[{0}]LOG]!><time=”{1}” date=”{2}” component=”{3}” context=”” type=”{4}” thread=”” file=””>’

It’s then a question of constructing what will go in the’s after you’ve obtained the template. I’ll use this to create an array, which I’ll then feed to the $Line to replace all of our’s with actual data.

$LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)”, $LogLevel = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.Script

These are listed in the same sequence as the’s. $Message will be converted to $TimeGenerated, $Today will be converted to today’s date, and $LogLevel will be converted to today’s date.

Have you noticed that I skipped number three? This is the point at which I go all ninja on you. I decided to make advantage of a component column in CMTrace that I had never utilized before.

I needed to know the name of the script and the line number where Write-Log was invoked. “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)” is the command that does this.

I then use PowerShell’s string formatting to combine these two variables to create $Line.

$Line -f = $Line $LineFormat

Then it’s only a question of writing $Line to a text file that Start-Log has already established.

Add-Content -Value is a command that adds content to a document. $ScriptLogFilePath $Line -Path

How Does It Work?

Let’s imagine I write a script named LogDemo.ps1 that looks like this:

C:MyLog.log -FilePath Start-Log “The path to the script log file is [$ScriptLogFilePath],” writes the host. ‘basic activity’ Write-Log -Message Write-Log -Warning -LogLevel 2 -Message ‘warning’ Write-Log -Error -LogLevel 3 -Message ‘Error’

This script creates a log file at C:MyLog.log and then uses the LogLevel arguments to write three different degrees of severity to the log.

When I use Get-Content to inspect the output of this file, it seems to be rather ugly.

<![LOG[simple activity]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:3″ context=”” type=”1″ thread=”” file=””> <![LOG[warning]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:4″ context=”” type=”2″ thread=”” file=””> <![LOG[Error]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:5″ context=”” type=”3″ thread=”” file=””>

Let’s take a look at this in CMTrace and see what we’ve got.

1647501850_593_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellPowerShell creates CMTrace fields.

Isn’t it lovely?

Even if you’re not an SCCM administrator, I strongly suggest that you use CMTrace for all of your log viewing requirements.

Simply open the log files in CMTrace after you’ve received them in the proper format (and you now have no reason not to) and marvel at everything that CMTrace has to offer!

.Exception.Message

You’re definitely acquainted with the CMtrace log-reading tool if you’re an SCCM administrator. CMTrace understands the SCCM client logs’ special structure, but did you realize you can do the same thing using PowerShell?

You may read the following using PowerShell:

  • Install logs for SCCM client software
  • Update logs for SCCM clients
  • Client hardware inventory records in SCCM

…and pretty much the rest of them!

The SCCM client logs have a certain format. You must grasp that schema in order to acquire an uniform output from them. Fortunately, it has already been done for you.

Using PowrShell to read SCCM Client Logs

Consider the PowerShell code below, which searches SCCM client logs.

You may query remote machines and give the log name you’d want to examine using the Get-SCCMClientLog PowerShell function, which has two parameters: ComputerName and LogName.

Introduction to PowerShell Functions

After you’ve added this function to your PowerShell console, you may use it as shown below. The example below will obtain the execmgr log file from a remote machine named PC.

Get-SccmClientLog -ComputerName PC -LogName execmgr Get-SccmClientLog -ComputerName PC -LogName execmgr

The function has already done all of the hard work for you.

Creating SCCM Client Logs on Your Own

What if you need to utilize the Get-SCCMClientLog method for anything else? You can even make your own SCCM-style log files!

Reading-and-Creating-SCCM-Client-Logs-with-PowerShellCMTrace is a good example.

Look at how lovely the sortable columns and red highlights are! You would imagine that CMTrace can display any kind of text log, and you’d be correct. However, in CMTrace, have a check at the The file WindowsUpdate.log.

1647501849_978_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellThe file WindowsUpdate.log

Have you noticed that all of the columns have vanished? You can still use CMTrace to see ordinary log files, but you won’t have access to some of the features that make it so useful. You’ll quickly discover that a text file must be correctly structured in order for all of those useful columns to appear, as well as to properly indicate which lines should be highlighted yellow vs. red vs. nothing.

I’d want to demonstrate Write-Log and Start-Log, two PowerShell functions. These routines were created particularly to log the activities of your script in a log file that can subsequently be viewed in CMTrace.

By the conclusion of this article, you’ll have a function that you can use in your scripts to create log files that CMTrace can read correctly.

Start-Log

I created a method named Start-Log to avoid having to give the same SCCM client log file location over and over again. This function should be invoked at the beginning of your script. This method merely generates a text file and (more importantly) sets ScriptLogFilePath, a global variable.

[CmdletBinding()] param ( [ValidateScript({ Split-Path $_ -Parent | Test-Path })] [string]$FilePath ) try { if (!(Test-Path $FilePath)) { ## Create the log file New-Item $FilePath -Type File | Out-Null } ## Set the global variable to be used as the FilePath for all subsequent Write-Log calls in this session $global:ScriptLogFilePath = $FilePath } catch { Write-Error $_.Exception.Message }

This is a very basic function. However, in order to avoid having to give LogFile every time we need to use our Write-Log function in our scripts, it is essential. We can just call Write-Log and it will know the log file location since we created a global variable ahead of time.

Write-Log

You may now perform Write-Log to write SCCM client log messages to the log file after using Start-Log in your script. Message and LogLevel are the two arguments for Write-Log. The message is simple. Simply put, that’s what you’d want to enter into the log. LogLevel requires some explanation.

The line must be recorded in a certain manner for CMTrace to indicate it as red or yellow. It has to have a string that looks like this: type=”1.” This key might be 1,2, or 3 digits long. These are the severity levels in your script.

For instance, if I wanted to log a basic informative message, I’d use a 1. If I want to record a more serious action, I’ll use 2, which will be marked in yellow. Finally, if I want that line highlighted red in CMTrace, I could pick 3.

[Parameter(Mandatory = $true)] param ([Parameter(Mandatory = $true)] param ([Parameter(Mand [string] $Message, [Parameter()] [ValidateSet(1, 2, 3)] [ValidateSet(1, 2, 3)] [ValidateSet(1, ([int]$LogLevel = 1))))))))))))))))

Have you taken notice of the LogLevel parameter? It will set it to 1 by default, but you can always override it if you need to write anything more serious that occurs throughout the execution of your script.

Then you’ll need that helpful date/time column to appear correctly. This needed a precise date/time format, which this string manipulation skill provided.

$TimeGenerated = $$$$$$$ “a dollar (Get-Date -Format HH:mm:ss). $((Get-Date).Millisecond)+000”

Next, I’ll create a log line template that has all of the necessary formatting for the line to appear appropriately in CMTrace.

$Line = ‘<![LOG[{0}]LOG]!><time=”{1}” date=”{2}” component=”{3}” context=”” type=”{4}” thread=”” file=””>’

It’s then a question of constructing what will go in the’s after you’ve obtained the template. I’ll use this to create an array, which I’ll then feed to the $Line to replace all of our’s with actual data.

$LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)”, $LogLevel = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.Script

These are listed in the same sequence as the’s. $Message will be converted to $TimeGenerated, $Today will be converted to today’s date, and $LogLevel will be converted to today’s date.

Have you noticed that I skipped number three? This is the point at which I go all ninja on you. I decided to make advantage of a component column in CMTrace that I had never utilized before.

I needed to know the name of the script and the line number where Write-Log was invoked. “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)” is the command that does this.

I then use PowerShell’s string formatting to combine these two variables to create $Line.

$Line -f = $Line $LineFormat

Then it’s only a question of writing $Line to a text file that Start-Log has already established.

Add-Content -Value is a command that adds content to a document. $ScriptLogFilePath $Line -Path

How Does It Work?

Let’s imagine I write a script named LogDemo.ps1 that looks like this:

C:MyLog.log -FilePath Start-Log “The path to the script log file is [$ScriptLogFilePath],” writes the host. ‘basic activity’ Write-Log -Message Write-Log -Warning -LogLevel 2 -Message ‘warning’ Write-Log -Error -LogLevel 3 -Message ‘Error’

This script creates a log file at C:MyLog.log and then uses the LogLevel arguments to write three different degrees of severity to the log.

When I use Get-Content to inspect the output of this file, it seems to be rather ugly.

<![LOG[simple activity]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:3″ context=”” type=”1″ thread=”” file=””> <![LOG[warning]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:4″ context=”” type=”2″ thread=”” file=””> <![LOG[Error]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:5″ context=”” type=”3″ thread=”” file=””>

Let’s take a look at this in CMTrace and see what we’ve got.

1647501850_593_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellPowerShell creates CMTrace fields.

Isn’t it lovely?

Even if you’re not an SCCM administrator, I strongly suggest that you use CMTrace for all of your log viewing requirements.

Simply open the log files in CMTrace after you’ve received them in the proper format (and you now have no reason not to) and marvel at everything that CMTrace has to offer!

.Exception.Message

You’re definitely acquainted with the CMtrace log-reading tool if you’re an SCCM administrator. CMTrace understands the SCCM client logs’ special structure, but did you realize you can do the same thing using PowerShell?

You may read the following using PowerShell:

  • Install logs for SCCM client software
  • Update logs for SCCM clients
  • Client hardware inventory records in SCCM

…and pretty much the rest of them!

The SCCM client logs have a certain format. You must grasp that schema in order to acquire an uniform output from them. Fortunately, it has already been done for you.

Using PowrShell to read SCCM Client Logs

Consider the PowerShell code below, which searches SCCM client logs.

You may query remote machines and give the log name you’d want to examine using the Get-SCCMClientLog PowerShell function, which has two parameters: ComputerName and LogName.

Introduction to PowerShell Functions

After you’ve added this function to your PowerShell console, you may use it as shown below. The example below will obtain the execmgr log file from a remote machine named PC.

Get-SccmClientLog -ComputerName PC -LogName execmgr Get-SccmClientLog -ComputerName PC -LogName execmgr

The function has already done all of the hard work for you.

Creating SCCM Client Logs on Your Own

What if you need to utilize the Get-SCCMClientLog method for anything else? You can even make your own SCCM-style log files!

Reading-and-Creating-SCCM-Client-Logs-with-PowerShellCMTrace is a good example.

Look at how lovely the sortable columns and red highlights are! You would imagine that CMTrace can display any kind of text log, and you’d be correct. However, in CMTrace, have a check at the The file WindowsUpdate.log.

1647501849_978_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellThe file WindowsUpdate.log

Have you noticed that all of the columns have vanished? You can still use CMTrace to see ordinary log files, but you won’t have access to some of the features that make it so useful. You’ll quickly discover that a text file must be correctly structured in order for all of those useful columns to appear, as well as to properly indicate which lines should be highlighted yellow vs. red vs. nothing.

I’d want to demonstrate Write-Log and Start-Log, two PowerShell functions. These routines were created particularly to log the activities of your script in a log file that can subsequently be viewed in CMTrace.

By the conclusion of this article, you’ll have a function that you can use in your scripts to create log files that CMTrace can read correctly.

Start-Log

I created a method named Start-Log to avoid having to give the same SCCM client log file location over and over again. This function should be invoked at the beginning of your script. This method merely generates a text file and (more importantly) sets ScriptLogFilePath, a global variable.

[CmdletBinding()] param ( [ValidateScript({ Split-Path $_ -Parent | Test-Path })] [string]$FilePath ) try { if (!(Test-Path $FilePath)) { ## Create the log file New-Item $FilePath -Type File | Out-Null } ## Set the global variable to be used as the FilePath for all subsequent Write-Log calls in this session $global:ScriptLogFilePath = $FilePath } catch { Write-Error $_.Exception.Message }

This is a very basic function. However, in order to avoid having to give LogFile every time we need to use our Write-Log function in our scripts, it is essential. We can just call Write-Log and it will know the log file location since we created a global variable ahead of time.

Write-Log

You may now perform Write-Log to write SCCM client log messages to the log file after using Start-Log in your script. Message and LogLevel are the two arguments for Write-Log. The message is simple. Simply put, that’s what you’d want to enter into the log. LogLevel requires some explanation.

The line must be recorded in a certain manner for CMTrace to indicate it as red or yellow. It has to have a string that looks like this: type=”1.” This key might be 1,2, or 3 digits long. These are the severity levels in your script.

For instance, if I wanted to log a basic informative message, I’d use a 1. If I want to record a more serious action, I’ll use 2, which will be marked in yellow. Finally, if I want that line highlighted red in CMTrace, I could pick 3.

[Parameter(Mandatory = $true)] param ([Parameter(Mandatory = $true)] param ([Parameter(Mand [string] $Message, [Parameter()] [ValidateSet(1, 2, 3)] [ValidateSet(1, 2, 3)] [ValidateSet(1, ([int]$LogLevel = 1))))))))))))))))

Have you taken notice of the LogLevel parameter? It will set it to 1 by default, but you can always override it if you need to write anything more serious that occurs throughout the execution of your script.

Then you’ll need that helpful date/time column to appear correctly. This needed a precise date/time format, which this string manipulation skill provided.

$TimeGenerated = $$$$$$$ “a dollar (Get-Date -Format HH:mm:ss). $((Get-Date).Millisecond)+000”

Next, I’ll create a log line template that has all of the necessary formatting for the line to appear appropriately in CMTrace.

$Line = ‘<![LOG[{0}]LOG]!><time=”{1}” date=”{2}” component=”{3}” context=”” type=”{4}” thread=”” file=””>’

It’s then a question of constructing what will go in the’s after you’ve obtained the template. I’ll use this to create an array, which I’ll then feed to the $Line to replace all of our’s with actual data.

$LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)”, $LogLevel = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.Script

These are listed in the same sequence as the’s. $Message will be converted to $TimeGenerated, $Today will be converted to today’s date, and $LogLevel will be converted to today’s date.

Have you noticed that I skipped number three? This is the point at which I go all ninja on you. I decided to make advantage of a component column in CMTrace that I had never utilized before.

I needed to know the name of the script and the line number where Write-Log was invoked. “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)” is the command that does this.

I then use PowerShell’s string formatting to combine these two variables to create $Line.

$Line -f = $Line $LineFormat

Then it’s only a question of writing $Line to a text file that Start-Log has already established.

Add-Content -Value is a command that adds content to a document. $ScriptLogFilePath $Line -Path

How Does It Work?

Let’s imagine I write a script named LogDemo.ps1 that looks like this:

C:MyLog.log -FilePath Start-Log “The path to the script log file is [$ScriptLogFilePath],” writes the host. ‘basic activity’ Write-Log -Message Write-Log -Warning -LogLevel 2 -Message ‘warning’ Write-Log -Error -LogLevel 3 -Message ‘Error’

This script creates a log file at C:MyLog.log and then uses the LogLevel arguments to write three different degrees of severity to the log.

When I use Get-Content to inspect the output of this file, it seems to be rather ugly.

<![LOG[simple activity]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:3″ context=”” type=”1″ thread=”” file=””> <![LOG[warning]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:4″ context=”” type=”2″ thread=”” file=””> <![LOG[Error]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:5″ context=”” type=”3″ thread=”” file=””>

Let’s take a look at this in CMTrace and see what we’ve got.

1647501850_593_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellPowerShell creates CMTrace fields.

Isn’t it lovely?

Even if you’re not an SCCM administrator, I strongly suggest that you use CMTrace for all of your log viewing requirements.

Simply open the log files in CMTrace after you’ve received them in the proper format (and you now have no reason not to) and marvel at everything that CMTrace has to offer!

.Exception.Message

You’re definitely acquainted with the CMtrace log-reading tool if you’re an SCCM administrator. CMTrace understands the SCCM client logs’ special structure, but did you realize you can do the same thing using PowerShell?

You may read the following using PowerShell:

  • Install logs for SCCM client software
  • Update logs for SCCM clients
  • Client hardware inventory records in SCCM

…and pretty much the rest of them!

The SCCM client logs have a certain format. You must grasp that schema in order to acquire an uniform output from them. Fortunately, it has already been done for you.

Using PowrShell to read SCCM Client Logs

Consider the PowerShell code below, which searches SCCM client logs.

You may query remote machines and give the log name you’d want to examine using the Get-SCCMClientLog PowerShell function, which has two parameters: ComputerName and LogName.

Introduction to PowerShell Functions

After you’ve added this function to your PowerShell console, you may use it as shown below. The example below will obtain the execmgr log file from a remote machine named PC.

Get-SccmClientLog -ComputerName PC -LogName execmgr Get-SccmClientLog -ComputerName PC -LogName execmgr

The function has already done all of the hard work for you.

Creating SCCM Client Logs on Your Own

What if you need to utilize the Get-SCCMClientLog method for anything else? You can even make your own SCCM-style log files!

Reading-and-Creating-SCCM-Client-Logs-with-PowerShellCMTrace is a good example.

Look at how lovely the sortable columns and red highlights are! You would imagine that CMTrace can display any kind of text log, and you’d be correct. However, in CMTrace, have a check at the The file WindowsUpdate.log.

1647501849_978_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellThe file WindowsUpdate.log

Have you noticed that all of the columns have vanished? You can still use CMTrace to see ordinary log files, but you won’t have access to some of the features that make it so useful. You’ll quickly discover that a text file must be correctly structured in order for all of those useful columns to appear, as well as to properly indicate which lines should be highlighted yellow vs. red vs. nothing.

I’d want to demonstrate Write-Log and Start-Log, two PowerShell functions. These routines were created particularly to log the activities of your script in a log file that can subsequently be viewed in CMTrace.

By the conclusion of this article, you’ll have a function that you can use in your scripts to create log files that CMTrace can read correctly.

Start-Log

I created a method named Start-Log to avoid having to give the same SCCM client log file location over and over again. This function should be invoked at the beginning of your script. This method merely generates a text file and (more importantly) sets ScriptLogFilePath, a global variable.

[CmdletBinding()] param ( [ValidateScript({ Split-Path $_ -Parent | Test-Path })] [string]$FilePath ) try { if (!(Test-Path $FilePath)) { ## Create the log file New-Item $FilePath -Type File | Out-Null } ## Set the global variable to be used as the FilePath for all subsequent Write-Log calls in this session $global:ScriptLogFilePath = $FilePath } catch { Write-Error $_.Exception.Message }

This is a very basic function. However, in order to avoid having to give LogFile every time we need to use our Write-Log function in our scripts, it is essential. We can just call Write-Log and it will know the log file location since we created a global variable ahead of time.

Write-Log

You may now perform Write-Log to write SCCM client log messages to the log file after using Start-Log in your script. Message and LogLevel are the two arguments for Write-Log. The message is simple. Simply put, that’s what you’d want to enter into the log. LogLevel requires some explanation.

The line must be recorded in a certain manner for CMTrace to indicate it as red or yellow. It has to have a string that looks like this: type=”1.” This key might be 1,2, or 3 digits long. These are the severity levels in your script.

For instance, if I wanted to log a basic informative message, I’d use a 1. If I want to record a more serious action, I’ll use 2, which will be marked in yellow. Finally, if I want that line highlighted red in CMTrace, I could pick 3.

[Parameter(Mandatory = $true)] param ([Parameter(Mandatory = $true)] param ([Parameter(Mand [string] $Message, [Parameter()] [ValidateSet(1, 2, 3)] [ValidateSet(1, 2, 3)] [ValidateSet(1, ([int]$LogLevel = 1))))))))))))))))

Have you taken notice of the LogLevel parameter? It will set it to 1 by default, but you can always override it if you need to write anything more serious that occurs throughout the execution of your script.

Then you’ll need that helpful date/time column to appear correctly. This needed a precise date/time format, which this string manipulation skill provided.

$TimeGenerated = $$$$$$$ “a dollar (Get-Date -Format HH:mm:ss). $((Get-Date).Millisecond)+000”

Next, I’ll create a log line template that has all of the necessary formatting for the line to appear appropriately in CMTrace.

$Line = ‘<![LOG[{0}]LOG]!><time=”{1}” date=”{2}” component=”{3}” context=”” type=”{4}” thread=”” file=””>’

It’s then a question of constructing what will go in the’s after you’ve obtained the template. I’ll use this to create an array, which I’ll then feed to the $Line to replace all of our’s with actual data.

$LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)”, $LogLevel = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.Script

These are listed in the same sequence as the’s. $Message will be converted to $TimeGenerated, $Today will be converted to today’s date, and $LogLevel will be converted to today’s date.

Have you noticed that I skipped number three? This is the point at which I go all ninja on you. I decided to make advantage of a component column in CMTrace that I had never utilized before.

I needed to know the name of the script and the line number where Write-Log was invoked. “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)” is the command that does this.

I then use PowerShell’s string formatting to combine these two variables to create $Line.

$Line -f = $Line $LineFormat

Then it’s only a question of writing $Line to a text file that Start-Log has already established.

Add-Content -Value is a command that adds content to a document. $ScriptLogFilePath $Line -Path

How Does It Work?

Let’s imagine I write a script named LogDemo.ps1 that looks like this:

C:MyLog.log -FilePath Start-Log “The path to the script log file is [$ScriptLogFilePath],” writes the host. ‘basic activity’ Write-Log -Message Write-Log -Warning -LogLevel 2 -Message ‘warning’ Write-Log -Error -LogLevel 3 -Message ‘Error’

This script creates a log file at C:MyLog.log and then uses the LogLevel arguments to write three different degrees of severity to the log.

When I use Get-Content to inspect the output of this file, it seems to be rather ugly.

<![LOG[simple activity]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:3″ context=”” type=”1″ thread=”” file=””> <![LOG[warning]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:4″ context=”” type=”2″ thread=”” file=””> <![LOG[Error]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:5″ context=”” type=”3″ thread=”” file=””>

Let’s take a look at this in CMTrace and see what we’ve got.

1647501850_593_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellPowerShell creates CMTrace fields.

Isn’t it lovely?

Even if you’re not an SCCM administrator, I strongly suggest that you use CMTrace for all of your log viewing requirements.

Simply open the log files in CMTrace after you’ve received them in the proper format (and you now have no reason not to) and marvel at everything that CMTrace has to offer!

.Exception.Message

You’re definitely acquainted with the CMtrace log-reading tool if you’re an SCCM administrator. CMTrace understands the SCCM client logs’ special structure, but did you realize you can do the same thing using PowerShell?

You may read the following using PowerShell:

  • Install logs for SCCM client software
  • Update logs for SCCM clients
  • Client hardware inventory records in SCCM

…and pretty much the rest of them!

The SCCM client logs have a certain format. You must grasp that schema in order to acquire an uniform output from them. Fortunately, it has already been done for you.

Using PowrShell to read SCCM Client Logs

Consider the PowerShell code below, which searches SCCM client logs.

You may query remote machines and give the log name you’d want to examine using the Get-SCCMClientLog PowerShell function, which has two parameters: ComputerName and LogName.

Introduction to PowerShell Functions

After you’ve added this function to your PowerShell console, you may use it as shown below. The example below will obtain the execmgr log file from a remote machine named PC.

Get-SccmClientLog -ComputerName PC -LogName execmgr Get-SccmClientLog -ComputerName PC -LogName execmgr

The function has already done all of the hard work for you.

Creating SCCM Client Logs on Your Own

What if you need to utilize the Get-SCCMClientLog method for anything else? You can even make your own SCCM-style log files!

Reading-and-Creating-SCCM-Client-Logs-with-PowerShellCMTrace is a good example.

Look at how lovely the sortable columns and red highlights are! You would imagine that CMTrace can display any kind of text log, and you’d be correct. However, in CMTrace, have a check at the The file WindowsUpdate.log.

1647501849_978_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellThe file WindowsUpdate.log

Have you noticed that all of the columns have vanished? You can still use CMTrace to see ordinary log files, but you won’t have access to some of the features that make it so useful. You’ll quickly discover that a text file must be correctly structured in order for all of those useful columns to appear, as well as to properly indicate which lines should be highlighted yellow vs. red vs. nothing.

I’d want to demonstrate Write-Log and Start-Log, two PowerShell functions. These routines were created particularly to log the activities of your script in a log file that can subsequently be viewed in CMTrace.

By the conclusion of this article, you’ll have a function that you can use in your scripts to create log files that CMTrace can read correctly.

Start-Log

I created a method named Start-Log to avoid having to give the same SCCM client log file location over and over again. This function should be invoked at the beginning of your script. This method merely generates a text file and (more importantly) sets ScriptLogFilePath, a global variable.

[CmdletBinding()] param ( [ValidateScript({ Split-Path $_ -Parent | Test-Path })] [string]$FilePath ) try { if (!(Test-Path $FilePath)) { ## Create the log file New-Item $FilePath -Type File | Out-Null } ## Set the global variable to be used as the FilePath for all subsequent Write-Log calls in this session $global:ScriptLogFilePath = $FilePath } catch { Write-Error $_.Exception.Message }

This is a very basic function. However, in order to avoid having to give LogFile every time we need to use our Write-Log function in our scripts, it is essential. We can just call Write-Log and it will know the log file location since we created a global variable ahead of time.

Write-Log

You may now perform Write-Log to write SCCM client log messages to the log file after using Start-Log in your script. Message and LogLevel are the two arguments for Write-Log. The message is simple. Simply put, that’s what you’d want to enter into the log. LogLevel requires some explanation.

The line must be recorded in a certain manner for CMTrace to indicate it as red or yellow. It has to have a string that looks like this: type=”1.” This key might be 1,2, or 3 digits long. These are the severity levels in your script.

For instance, if I wanted to log a basic informative message, I’d use a 1. If I want to record a more serious action, I’ll use 2, which will be marked in yellow. Finally, if I want that line highlighted red in CMTrace, I could pick 3.

[Parameter(Mandatory = $true)] param ([Parameter(Mandatory = $true)] param ([Parameter(Mand [string] $Message, [Parameter()] [ValidateSet(1, 2, 3)] [ValidateSet(1, 2, 3)] [ValidateSet(1, ([int]$LogLevel = 1))))))))))))))))

Have you taken notice of the LogLevel parameter? It will set it to 1 by default, but you can always override it if you need to write anything more serious that occurs throughout the execution of your script.

Then you’ll need that helpful date/time column to appear correctly. This needed a precise date/time format, which this string manipulation skill provided.

$TimeGenerated = $$$$$$$ “a dollar (Get-Date -Format HH:mm:ss). $((Get-Date).Millisecond)+000”

Next, I’ll create a log line template that has all of the necessary formatting for the line to appear appropriately in CMTrace.

$Line = ‘<![LOG[{0}]LOG]!><time=”{1}” date=”{2}” component=”{3}” context=”” type=”{4}” thread=”” file=””>’

It’s then a question of constructing what will go in the’s after you’ve obtained the template. I’ll use this to create an array, which I’ll then feed to the $Line to replace all of our’s with actual data.

$LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)”, $LogLevel = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.Script

These are listed in the same sequence as the’s. $Message will be converted to $TimeGenerated, $Today will be converted to today’s date, and $LogLevel will be converted to today’s date.

Have you noticed that I skipped number three? This is the point at which I go all ninja on you. I decided to make advantage of a component column in CMTrace that I had never utilized before.

I needed to know the name of the script and the line number where Write-Log was invoked. “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)” is the command that does this.

I then use PowerShell’s string formatting to combine these two variables to create $Line.

$Line -f = $Line $LineFormat

Then it’s only a question of writing $Line to a text file that Start-Log has already established.

Add-Content -Value is a command that adds content to a document. $ScriptLogFilePath $Line -Path

How Does It Work?

Let’s imagine I write a script named LogDemo.ps1 that looks like this:

C:MyLog.log -FilePath Start-Log “The path to the script log file is [$ScriptLogFilePath],” writes the host. ‘basic activity’ Write-Log -Message Write-Log -Warning -LogLevel 2 -Message ‘warning’ Write-Log -Error -LogLevel 3 -Message ‘Error’

This script creates a log file at C:MyLog.log and then uses the LogLevel arguments to write three different degrees of severity to the log.

When I use Get-Content to inspect the output of this file, it seems to be rather ugly.

<![LOG[simple activity]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:3″ context=”” type=”1″ thread=”” file=””> <![LOG[warning]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:4″ context=”” type=”2″ thread=”” file=””> <![LOG[Error]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:5″ context=”” type=”3″ thread=”” file=””>

Let’s take a look at this in CMTrace and see what we’ve got.

1647501850_593_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellPowerShell creates CMTrace fields.

Isn’t it lovely?

Even if you’re not an SCCM administrator, I strongly suggest that you use CMTrace for all of your log viewing requirements.

Simply open the log files in CMTrace after you’ve received them in the proper format (and you now have no reason not to) and marvel at everything that CMTrace has to offer!

.Exception

This is a very basic function. However, in order to avoid having to give LogFile every time we need to use our Write-Log function in our scripts, it is essential. We can just call Write-Log and it will know the log file location since we created a global variable ahead of time.

Write-Log

You may now perform Write-Log to write SCCM client log messages to the log file after using Start-Log in your script. Message and LogLevel are the two arguments for Write-Log. The message is simple. Simply put, that’s what you’d want to enter into the log. LogLevel requires some explanation.

The line must be recorded in a certain manner for CMTrace to indicate it as red or yellow. It has to have a string that looks like this: type=”1.” This key might be 1,2, or 3 digits long. These are the severity levels in your script.

For instance, if I wanted to log a basic informative message, I’d use a 1. If I want to record a more serious action, I’ll use 2, which will be marked in yellow. Finally, if I want that line highlighted red in CMTrace, I could pick 3.

[Parameter(Mandatory = $true)] param ([Parameter(Mandatory = $true)] param ([Parameter(Mand [string] $Message, [Parameter()] [ValidateSet(1, 2, 3)] [ValidateSet(1, 2, 3)] [ValidateSet(1, ([int]$LogLevel = 1))))))))))))))))

Have you taken notice of the LogLevel parameter? It will set it to 1 by default, but you can always override it if you need to write anything more serious that occurs throughout the execution of your script.

Then you’ll need that helpful date/time column to appear correctly. This needed a precise date/time format, which this string manipulation skill provided.

$TimeGenerated = $$$$$$$ “a dollar (Get-Date -Format HH:mm:ss). $((Get-Date).Millisecond)+000”

Next, I’ll create a log line template that has all of the necessary formatting for the line to appear appropriately in CMTrace.

$Line = ‘<![LOG[{0}]LOG]!><time=”{1}” date=”{2}” component=”{3}” context=”” type=”{4}” thread=”” file=””>’

It’s then a question of constructing what will go in the’s after you’ve obtained the template. I’ll use this to create an array, which I’ll then feed to the $Line to replace all of our’s with actual data.

$LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)”, $LogLevel = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), “$($MyInvocation.Script

These are listed in the same sequence as the’s. $Message will be converted to $TimeGenerated, $Today will be converted to today’s date, and $LogLevel will be converted to today’s date.

Have you noticed that I skipped number three? This is the point at which I go all ninja on you. I decided to make advantage of a component column in CMTrace that I had never utilized before.

I needed to know the name of the script and the line number where Write-Log was invoked. “$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)” is the command that does this.

I then use PowerShell’s string formatting to combine these two variables to create $Line.

$Line -f = $Line $LineFormat

Then it’s only a question of writing $Line to a text file that Start-Log has already established.

Add-Content -Value is a command that adds content to a document. $ScriptLogFilePath $Line -Path

How Does It Work?

Let’s imagine I write a script named LogDemo.ps1 that looks like this:

C:MyLog.log -FilePath Start-Log “The path to the script log file is [$ScriptLogFilePath],” writes the host. ‘basic activity’ Write-Log -Message Write-Log -Warning -LogLevel 2 -Message ‘warning’ Write-Log -Error -LogLevel 3 -Message ‘Error’

This script creates a log file at C:MyLog.log and then uses the LogLevel arguments to write three different degrees of severity to the log.

When I use Get-Content to inspect the output of this file, it seems to be rather ugly.

<![LOG[simple activity]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:3″ context=”” type=”1″ thread=”” file=””> <![LOG[warning]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:4″ context=”” type=”2″ thread=”” file=””> <![LOG[Error]LOG]!><time=”18:56:26.307+000″ date=”12-03-2015″ component=”LogDemo.ps1:5″ context=”” type=”3″ thread=”” file=””>

Let’s take a look at this in CMTrace and see what we’ve got.

1647501850_593_Reading-and-Creating-SCCM-Client-Logs-with-PowerShellPowerShell creates CMTrace fields.

Isn’t it lovely?

Even if you’re not an SCCM administrator, I strongly suggest that you use CMTrace for all of your log viewing requirements.

Simply open the log files in CMTrace after you’ve received them in the proper format (and you now have no reason not to) and marvel at everything that CMTrace has to offer!

Frequently Asked Questions

How do I read SCCM client logs?

A: In order to view the logs of your SCCM client, you must be a member of either the local Administrators group or part of an Organization that has Remote Administration access. For more information on how to achieve this please visit our article How To View Client Logs in Microsoft System Center Configuration Manager 。

How do you write a log in PowerShell?

A: $log = new-object System.Diagnostics.EventLog;
$log.CreateEntry(This is a log entry);
$log

How do I run a SCCM file in PowerShell?

A: There are multiple ways to run a script in PowerShell, but the easiest method is by using the RunAs command.

Related Tags

  • sccm powershell script log
  • cmtrace log format

Table of Content