Author Posts

May 26, 2014 at 4:37 am

I try to empty the task scheduler log remote on 2008 R2 servers with

[System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog("Microsoft-Windows-TaskScheduler/Operational")

Can't load the assembly. Error:
Unable to find type [System.Diagnostics.Eventing.Reader.EventLogSession]: make sure that the assembly containing this t
ype is loaded.

preload like

[System.Reflection.Assembly]::LoadWithPartialName("System.Diagnostics.Eventing.Reader.EventLogSession")

does not work.
Fired locally it works, just not in a remote session. Any idea?

May 26, 2014 at 5:23 am

Do you have the .NET Framework 3.5 or later installed on the remote server?

The EventLogSession class was introduced with that version and you shouldn't need to load anything because the class is part of System.Core which is being loaded automatically.

May 26, 2014 at 5:38 am

Yes sir. Like I said: local powershell the snippet works, but not when using remoting.

May 26, 2014 at 6:57 am

Your code runs without issue for me when I run it from my test Windows 8.1 client machine against my test Windows 2012R2 web server:


Invoke-Command -ComputerName web01 {
    [System.Reflection.Assembly]::LoadWithPartialName("System.Diagnostics.Eventing.Reader.EventLogSession")
    [System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog("Microsoft-Windows-TaskScheduler/Operational")
}

It also works without adding the assembly:


Invoke-Command -ComputerName web01 {
    [System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog("Microsoft-Windows-TaskScheduler/Operational")
}

May 26, 2014 at 7:16 am

Yes, it works fine on our 2012/2012 R2 servers, but not on 2008 R2.
Unfortunately the majority of the servers are still 2008R2

May 26, 2014 at 7:29 am

It works for me remotely on Win2008 R2 servers as well. That why I've asked if the .NET Framework 3.5 or later is installed.

Please be so kind to run below command against one of your Win2008 R2 servers and post the output.

Invoke-Command -ComputerName RDS01 -ScriptBlock { [appdomain]::currentdomain.getassemblies() | Where-Object FullName -like '*System.Core*' } | Format-Table -Property ImageRuntimeVersion, FullName -AutoSize

May 26, 2014 at 7:49 am

Had to adapt the code a bit to let it work with posh 2
I'm getting no returns. locally nor remote.

But it still works on the server it self.

May 26, 2014 at 8:05 am

Ok. I think I've figured it out using a fresh Win2008 R2 install in a VM.

1. You've PowerShell 2.0 and .NET Framework 3.5.1 installed.
2. [System.Reflection.Assembly]::LoadWithPartialName("System.Diagnostics.Eventing.Reader.EventLogSession")
Does not load the System.Core which contains the class you want to use.

A solution that works on my fresh machine with PowerShell 2.0 and .NET 3.5 is:
Invoke-Command -ComputerName RDS02 -ScriptBlock { Add-Type -AssemblyName System.Core; [System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog('Microsoft-Windows-TaskScheduler/Operational') }

June 2, 2014 at 4:20 am

Thank you, i'm back from an extended holliday weekend and I can confirm this works