If statement returns error when $false is expected

Hello PowerShell experts!

I hope someone can help with the below.

I am creating a Scale Set in Azure and using DSC to configure the VMs. The scale set (of 50 VMs) is created once a week for about 12hrs and then discarded at the end of the day.

The DSC Configuration File has been successful for the past 5 or 6 months without any glitches until recently when the Scale Set provisioning failed.

Looking at the error from Azure, it is related to DSC.

Looking at the DSC log file:

VERBOSE: [2018-12-03 00:18:36Z] [VERBOSE] [Feb201800053Z]: LCM: [ Start

Resource ] [[xScript]Reboot]

VERBOSE: [2018-12-03 00:18:36Z] [VERBOSE] [Feb201800053Z]: LCM: [ Start Test

] [[xScript]Reboot]

VERBOSE: [2018-12-03 00:18:36Z] [VERBOSE] [Feb201800053Z]:

[[xScript]Reboot] Begin executing test script.

VERBOSE: [2018-12-03 00:18:36Z] [VERBOSE] [Feb201800053Z]:

[[xScript]Reboot] Executing script:

If (Get-eventlog system -After (get-date).Date -Source

‘User32’ ) {

Return $true

} Else {

Return $false


VERBOSE: [2018-12-03 00:18:36Z] [VERBOSE] [Feb201800053Z]: LCM: [ End Test

] [[xScript]Reboot] in 0.3590 seconds.

VERBOSE: [2018-12-03 00:18:36Z] [ERROR] PowerShell DSC resource

MSFT_xScriptResource failed to execute Test-TargetResource functionality with

error message: System.InvalidOperationException: The test script threw an

error. —> System.ArgumentException: No matches found


— End of inner exception stack trace —

VERBOSE: [2018-12-03 00:18:36Z] [VERBOSE] [Feb201800053Z]: LCM: [ End Set



The DSC configuration file section is configured as below:

xScript Reboot


SetScript = {

Restart-Computer -force


TestScript = {

If (Get-eventlog system -After (get-date).Date -Source ‘User32’ ) {

Return $true

} Else {

Return $false



GetScript = { }

DependsOn = “[xScript]ConfigPlexos”

PsDscRunAsCredential = $Credential



Essentially what it is supposed to do is check if the VM has rebooted after installing a package (denoted here by the line DependsOn) . If it hasn’t, then it restarts.

I was able to fix this issue by adding -ErrorAction Silently Continue on the IF statement.

The question I have is ‘Why!?’

The if statement checks if there is an event in the System log where the source is User32. If there is, the TestScript should return $true. If there isn’t, then the TestScript should return $false. It worked fine for 5 or 6 months… Why is it now spitting an error ‘No Matches Found’? It shouldn’t spit out an error but should rather spit out $false shouldn’t it??

Thank you in advance for your help and efforts.


After some trials and errors… It throws an error, if the source is not present in the eventlog. If the source is present, but it doesn’t satisfy your filter, it doesn’t throw.

Check on the server(s) that cause the issue if the -Source User32 actually exist in the eventlog.


get-eventlog -LogName System | select -Unique source

The error as far as I can tell is because the source doesn’t exist or isn’t registered.
If the statement errors out it will output that error message.
So you need to do silentlycontinue to avoid the output.

It will give you the “false” result that you expect but it will also output the error message.

This can always happen and error handling is the only way to handle this scenario. PowerShell gives us the capability to put expressions as condition for If loop.
I have seen many such scripts. what you really need here is to handle use -ErrorAction SilentlyContinue as mentioned above. When we write scripts, we miss such instances where errors can occur, but realizes when this kind off scenarios comes. But you have to decide whether to return false if the event source itself is not there .

Hello everyone,

Thank you for all your input.

After I posted my question, I did some trials on my laptop and all 3 of you are correct in that:

The Get-EventLog cmdlet will first retrieve all the contents of the log and then filter the result for the searched item. If the item is present, the result is $true if the item is present within the searched time frame and $false if it is not. Now, if the item is not present in the whole log, then the output is an error and $false.

So yes, @phansen and @Frederick, the error is because the source is absent in the entire log.

@kvprasoon, -ErrorAction SilentlyContinue is what I had added to fix the issue.

Now, what is even more interesting (and will be tested soon) is why has this not appeared in the past 5 or 6 months. What I mean is, if the command was there this whole time without the -ErrorAction SilentlyContinue switch and, if the VM Scale Set is provisioned from scratch (therefore the logs should be empty), why has the failure just occurred? Stay tuned, I will test and post my findings here in 2 days’ time for anyone who is / will be interested.

Again, thank you all for your help, input and efforts!