StringExpansion in C # called from Powershell

I have a small C # class that makes some entries and which is called from within a powershell script framework using:

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

Writing to the log file is as follows

$Log.AddInfo("myText")

While working fine.

What I have been thinking about for some time is if I can use stringexpansion in the AddInfo () method of my LogWriter class?

Take a look at an example:

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

Now the C # class now extends the path of $ modules to $ test, as powershell does. I already know that in C # I have access to the powershell space from which the C # class was called using System.Management.Automation Namespace. But then I get lost how to really expand the variable.

The entry written to the log file should look like this:

, C:\temp\modules # LogWriter

, , script,

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

, , , .

, , Runspace # ExpandString , . .

- , ? , , , , , .

Rgds

Jan

+4
2

$ModulesPath script? , #, , AddInfo, :

$Log.AddInfo($test, $ModulesPath)

:

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

sTest sModulesPath - .

+1

, , , , , $test:

$test = " , $ModulesPath # LogWriter"

0

Source: https://habr.com/ru/post/1620357/


All Articles