我可以用 C# 和 Powershell 做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/742262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What can I do with C# and Powershell?
提问by Kredns
I have a decent understanding of C# and a very basic understanding of powershell. I'm using Windows PowerShell CTP 3, which has been really fun. But I want to go beyond writing scripts/functions. Is there any cool stuff to do with C#?
我对 C# 有很好的了解,对 powershell 有非常基本的了解。我正在使用 Windows PowerShell CTP 3,这真的很有趣。但我想超越编写脚本/函数。C# 有什么很酷的东西吗?
采纳答案by JaredPar
I think the most interesting thing you can do with C# and PowerShell is to build CmdLet's. These are essentially plugins to PowerShell that are written in managed code and act like normal functions. They have a verb-noun pair and many of the functions you already use are actually cmdlets under the hood.
我认为你可以用 C# 和 PowerShell 做的最有趣的事情是构建 CmdLet。这些本质上是 PowerShell 的插件,它们是用托管代码编写的,就像普通函数一样。它们有一个动词-名词对,并且您已经使用的许多功能实际上都是底层的 cmdlet。
回答by Srikar Doddi
Answer is 'It depends'. You can do a variety of stuff with c# (build windows, web clients, and mobile clients).
答案是“视情况而定”。您可以使用 c# 执行各种操作(构建窗口、Web 客户端和移动客户端)。
You can invoke powershell scripts from C#. Look at this site ==> link
您可以从 C# 调用 powershell 脚本。看看这个网站 ==>链接
You can even convert your c# code to powershell ==> link
您甚至可以将您的 c# 代码转换为 powershell ==>链接
回答by Scott Weinstein
At the highest level you have two different options You can from a C# program host PowerShell and execute PowerShell commands via RunSpaces and pipelines.
在最高级别,您有两个不同的选项 您可以从 C# 程序托管 PowerShell 并通过 RunSpaces 和管道执行 PowerShell 命令。
Or you can from within PowerShell run C# code. This can be done two ways. With a PowerShell snapin, a compiled dll which provides PowerShell cmdlets and navigation providers, or via the new cmdlet Add-Type, which lets you dynamically import C#, VB, F# code. From the help
或者您可以从 PowerShell 中运行 C# 代码。这可以通过两种方式完成。使用 PowerShell 管理单元,即提供 PowerShell cmdlet 和导航提供程序的已编译 dll,或通过新的 cmdlet Add-Type,您可以动态导入 C#、VB、F# 代码。从帮助
$source = @"
public class BasicTest
{
public static int Add(int a, int b)
{
return (a + b);
}
public int Multiply(int a, int b)
{
return (a * b);
}
}
"@
Add-Type -TypeDefinition $source
[BasicTest]::Add(4, 3)
$basicTestObject = New-Object BasicTest
$basicTestObject.Multiply(5, 2)
回答by Thomas Lee
You can look at it one of two ways:
您可以通过以下两种方式之一查看它:
- How can you leverage PowerShell inside your C# program
- How can you leverage C# programming inside PowerShell.
- 如何在 C# 程序中利用 PowerShell
- 如何在 PowerShell 中利用 C# 编程。
To some degree, they are quite different questions with different answers.
在某种程度上,它们是完全不同的问题,有不同的答案。
From C# you can leverage the PowerShell engine, runspaces, pipe-lines, etc. As is done with Exchante, you can use C# to do all the GUI stuff, then invoke a PowerShell cmdlet to do all the hard stuff. This option is appropriate if you can find PowerShell cmdlets or scripts to leverage.
在 C# 中,您可以利用 PowerShell 引擎、运行空间、管道等。与 Exchante 一样,您可以使用 C# 来完成所有 GUI 的工作,然后调用 PowerShell cmdlet 来完成所有困难的工作。如果您可以找到要利用的 PowerShell cmdlet 或脚本,则此选项是合适的。
From PowerShell, you use C# to expand what you can do in PowerShell. You can create cmdlts and providers to enable others to access application data. Or you can just create objects that can be used within a PowerShell script. This option is the way you help to open up your application to be managed in a more automated way.
在 PowerShell 中,您可以使用 C# 来扩展您可以在 PowerShell 中执行的操作。您可以创建 cmdlts 和提供程序以允许其他人访问应用程序数据。或者,您可以只创建可在 PowerShell 脚本中使用的对象。此选项可帮助您打开以更自动化的方式管理的应用程序。
So depending on what you are looking to do, you have options.
因此,根据您要做什么,您有多种选择。
回答by mP.
Scott Hanselman aka Hanselminuteshas several podcasts about Powershell, CmdLets, C# and more. It's the best if you want to learn what it is, how it works and more. Do a search on his website to grab the podcast.
Scott Hanselman aka Hanselminutes有几个关于 Powershell、CmdLets、C# 等的播客。如果您想了解它是什么,它是如何工作的等等,这是最好的。在他的网站上搜索以获取播客。
List of PS related podcasts on his site (in reverse chronological order):
他网站上与 PS 相关的播客列表(按时间倒序排列):
#190: State of Powershell/Lee Holmes & Jason Shirk
#162: Powershell 2.0
#49: Powershell/Bruce Payette
#36: Jeffrey Snover, Powershell architect
#24: Windows Powershell (MONAD), Part II
#190:Powershell 状态/Lee Holmes 和 Jason Shirk
#162:Powershell 2.0
#49:Powershell/Bruce Payette
#36:Jeffrey Snover,Powershell 架构师
#24:Windows Powershell(MONAD),第二部分