Is it possible to create a string in C # and use it with PowerShell?

Is it possible to create a line in C # .exe and use this line in PowerShell?

+3
source share
4 answers

Yes.

Your question is pretty vague: do you use a powershell script to create an object from a C # assembly, and then use that object? If so, just set the property of the line you need and access it as usual from powershell.

Or, if it's a static method, you can just call it

   [My.Assembly.Type]::GetSomeString()

If you use C # exe, you can simply output it using a string and powershell will pick it up:

.\myexe.exe > $someVar

Or you can write it as a cmdlet , and your C # code can participate in the pipeline.

+5

PowerShell # , # PowerShell script, , :

string foo = "some string defined in C# to be used in PowerShell";
runspace.SessionStateProxy.SetVariable("foo", foo);

PowerShell script $foo.

+4

Yes. You can use .NET classes in powershell, for example, you can use:

PS C:\> [System.Environment]::GetLogicalDrives()

when working with the static method.

+1
source

Powershell uses the .net Framework under covers, all objects are CLR objects.

0
source

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


All Articles