CLR profiling: Trying to understand how to fetch variables.

Vivek Saini 0 Reputation points
2025-10-21T06:22:40.61+00:00

I have been experimenting with a .NET CLR profiler for the purposes of building a powershell monitoring AV tool. I wanted to intercept powershell code (IL) using the profiler right before it is JIT compiled using API functions like JITCompilationStarted, GetFunctionInfo, GetMethodName, GetILFunctionBody.

For testing purpose i have used the following simple commands from my cmd (where i have set the environment variables for profiling) for testing.

  1. powershell.exe -c "Write-Host 'HelloWorld'"
  2. powershell.exe -c "(New-Object Net.WebClient).DownloadString('http://example.com')"

My intent is to intercept the strings "HelloWorld" and "example.com" before they are actually executed by powershell.

I am using the IMetaDataImport::GetUserString and IMetaDataImport::GetMethodsProps to resolve the string literals (like ldstr "HelloWorld") and function names (for example callvirt Runspace.InvokeCommand)

But I am still unable to locate the strings.

Kindly tell me how to profile powershell in such a way as too retrive IL code corresponding to the entered command?

Sysinternals
Sysinternals
Advanced system utilities to manage, troubleshoot, and diagnose Windows and Linux systems and applications.
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.