Remoting and Assembly

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?

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.

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

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")
}

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

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

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.

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.

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’) }

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