4

I have a small c# class, that does some logging stuff and that is called from a powershell scripting framework using:

[System.Reflection.Assembly]::LoadFrom("$ExtensionsPath\LogWriter.dll") 
$Log = New-Object LogWriter($LogFile, $addTimeStamp, $logLevel, $overWrite)

Writing into the log file goes like this

$Log.AddInfo("myText")

Works fine so far.

What I am thinking about for some time is, if I am able to use stringexpansion in the AddInfo() method of my LogWriter class?

Look at the example:

$ModulesPath = ‘C:\temp\modules’
$test = ‘This is a text and I want to expand $ModulesPath in my c# LogWriter class’
$Log.AddInfo($test)

The c# class shall now expand the $modulespath in $test as powershell does. I already know that in c# I have access to the powershell runspace from which the c# class was called using System.Management.Automation Namespace. But then I am lost how to really expand the variable.

The entry written into the logfile should look like this:

This is a text and I want to expand C:\temp\modules in my c# LogWriter class

Of course I know I can do this in my script using

$Log.AddInfo(($ExecutionContext.InvokeCommand.ExpandString($test)))

But this is nasty because it looks ugly and if I forget to add this statement no expansion is done.

So I thought of retrieving the current Runspace in my c# class and do the ExpandString-Command there to get the expanded variable but I fail. This is beyond my knowledge.

Anyone here to tell my if this is possible? I already think of some other tasks where to use this so please do not start a flame war about if this makes sense or not.

Rgds

Jan

1
  • Your c# class has a method with the following signature: LogWriter.AddInfo(string info). How do you expect to expand "I want to expand $ModulesPath in my c#" inside this class? You are not passing any information whatsoever regarding what to expand ($ModulesPath) and what to expand to (C:\temp\modules). You'd need to think of a completely different signature that somehow passes along all the required information which just makes the whole thing a lot more clunkier than simply expanding before the call to AddInfo. Commented Dec 16, 2015 at 15:11

2 Answers 2

1

How can the value for $ModulesPath be known outside of your script? If you want it to be expanded in C#, then you have to send it, may be as a second Parameter to AddInfo like:

$Log.AddInfo($test, $ModulesPath)

Now it's known and the replacement could be done by:

string sNew = sTest.Replace("$ModulesPath", sModulesPath);

where sTest and sModulesPath are the parameters.

Sign up to request clarification or add additional context in comments.

2 Comments

And how does AddInfo($test, $ModulesPath) know that it has to expand the string $ModulesPath and not, lets say, $Hello? Or should it just replace the first string preceded by $ it encounters? You'd need to add one more argument to the signature: AddInfo($test, $tagName, $tagValue) which is simply awful. And what happens if a certain string has more than one expansion? This isn't a good solution, its more of a hack that has problems written all over it.
Well, yes, totaly right. But all that is your design decission. If you know that you have only one paramter for the string, then you can do it this way. But if you know you can have x Parameters, then may be an array or something like it can do the job? So now, you should define, what exactly is necessary for your solution.
0

not sure if this is what you're asking about, but please try using double quotes on the $test string:

$test = "This is a text and I want to expand $ModulesPath in my c# LogWriter class"

1 Comment

Hi, thanks for your aswer. I know that I can use double qutoes but I explicitely want to do the Expansion in my c# class. Just thinking if this is possible to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.