开发者

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

Is it possible to create a string in a C# .exe and use this string in开发者_开发百科 PowerShell?


Yes.

Your question is quite vague - are you using a powershell script to create an object from the c# assembly, then using that object? If so, simply set a property to the string you want and access it as normal from powershell.

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

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

If you are running the c# exe, you can simply output it with writeline 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.


In the event you are hosting PowerShell within a C# app and you want to access a string variable defined in C# from the PowerShell script that you run, here's how to do that:

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

You can then reference this variable in your PowerShell script as $foo.


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

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

when dealing with a static method.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜