Tag: powershell v2.0

在PowerShell v2中编译新类型 – Cookie Aware WebClient

我正在尝试编译WebClient类的“ cookie aware ”版本 – 但我似乎无法克服使用PowerShell v2中添加的Add-Type cmdlet的一些障碍。 这是我试图编译的代码: PS C:\> $def = @” public class CookieAwareWebClient : System.Net.WebClient { private System.Net.CookieContainer m_container = new System.Net.CookieContainer(); protected override System.Net.WebRequest GetWebRequest(System.Uri address) { System.Net.WebRequest request = base.GetWebRequest(address); if (request is System.Net.HttpWebRequest) { (request as System.Net.HttpWebRequest).CookieContainer = m_container; } return request; } } “@ PS C:\> Add-Type […]

如何从C#以编程方式调用CmdLet时捕获Powershell CmdLet的详细输出

背景 我在Windows 7上使用Powershell 2.0。 我在Powershell模块中编写了一个cmdlet(“模块”是Powershell 2.0的新增function)。 为了测试cmdlet,我在Visual Studio 2008中编写unit testing,以编程方式调用cmdlet。 参考 MSDN上的这篇文章名为“如何从Cmdlet中调用Cmdlet”,展示了如何从C#调用cmdlet。 来源代码 这是我的实际代码的简化版本 – 我已尽可能小,以便您可以清楚地看到我遇到的问题: using System; using System.Management.Automation; namespace DemoCmdLet1 { class Program { static void Main(string[] args) { var cmd = new GetColorsCommand(); foreach ( var i in cmd.Invoke()) { Console.WriteLine(“- ” + i ); } } } [Cmdlet(“Get”, “Colors”)] public class […]

Powershell命令通过C#代码

我想通过C#代码添加Powershell命令或脚本(什么是正确的?)变量声明,默认值存储在C#变量中。 例如,在Powershell中我输入以下行 $user = ‘Admin’ 我想在C#代码中添加这一行。 powershell.AddScript(String.Format(“$user = \”{0}\””, userName)); 要么 powershell.AddCommand(String.Format(“$user = \”{0}\””, userName)); 我尝试使用AddCommand()但它抛出exception。 我用的是PS 2.0。

使用C#使用V2方法使用命令行参数执行PowerShell脚本

我正在编写一个C#应用程序,它需要使用命令行参数执行Powershell脚本并检索输出(也就是我期望在PowerShell ISE的输出窗口中看到的内容 – 包括exception信息) 我找到了许多使用PowerShell V1对象完成此任务的代码示例。 这些示例创建了运行空间,管道等(例如, 使用命令行参数从C#执行PowerShell脚本 ) 我已经看到一些使用PowerShell V2的不同方法的一些参考。 (这里的最佳答案: 在Pipeline.Invoke抛出之后在C#中捕获Powershell输出 )使用V2似乎更简单。 不要乱用运行空间,piplines或其中任何一个。 像这样的东西: PowerShell powerShell = PowerShell.Create(); powerShell.AddScript(script); var results = powerShell.Invoke(); 有没有使用PowerShell V2对象从C#代码中执行脚本的好工作示例? 是否有任何已知的PowerShell V2(甚至V3)对象的良好文档以及如何使用它们的最佳实践? 来自微软的官方文档应该是什么? 一个好的书或网站,一个一个地分解System.Management.Automation组件?

Powershell / C#:异步调用管道并显示结果

根据此示例 – http://msdn.microsoft.com/en-us/library/windows/desktop/ee706590(v=vs.85).aspx , 我试图以异步方式调用我的脚本。 但是,与此同时,我希望在发生的操作集上向GUI提供反馈,即想要在GUI上并行地吐出幕后发生的Write-verbose东西。 我很难实现这一点 – 因为我看到PipelineReader对象上有一个DataReady事件? 有可能以某种方式消耗上面的MSDN示例,以便我可以在GUI上显示反馈吗? 从概念上讲,我无法将此示例与DataReady事件相关联。

提供.NET方法作为委托回调

将.NET方法作为代理回调传递给PowerShell中的.NET对象的语法是什么? 例如: C#: public class Class1 { public static void MyMethod(Action obj) { obj(“Hey!”); } } public class Class2 { public static void Callback(object obj) { Console.Writeline(obj.ToString()); } } 电源shell: [Class1]::MyMethod([Class2]::Callback) 这不起作用。

如何编写自定义PowerShell主机

类似于nuget 希望在钻研调试器之前寻找任何入门材料

在PowerShell中调用generics静态方法

如何在Powershell中调用自定义类的通用静态方法? 鉴于以下课程: public class Sample { public static string MyMethod( string anArgument ) { return string.Format( “Generic type is {0} with argument {1}”, typeof(T), anArgument ); } } 这被编译成一个程序集’Classes.dll’并加载到PowerShell中,如下所示: Add-Type -Path “Classes.dll” 调用MyMethod方法最简单的方法是什么?

PowerShell – 如何在Runspace中导入模块

我试图在C#中创建一个cmdlet。 代码看起来像这样: [Cmdlet(VerbsCommon.Get, “HeapSummary”)] public class Get_HeapSummary : Cmdlet { protected override void ProcessRecord() { RunspaceConfiguration config = RunspaceConfiguration.Create(); Runspace myRs = RunspaceFactory.CreateRunspace(config); myRs.Open(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(myRs); scriptInvoker.Invoke(“Set-ExecutionPolicy Unrestricted”); Pipeline pipeline = myRs.CreatePipeline(); pipeline.Commands.Add(@”Import-Module G:\PowerShell\PowerDbg.psm1″); //… pipeline.Invoke(); Collection psObjects = pipeline.Invoke(); foreach (var psObject in psObjects) { WriteObject(psObject); } } } 但是尝试在PowerShell中执行此CmdLet会给我这个错误: 术语Import-Module不被识别为cmdlet的名称 […]