Tag: powershell

使用C#中的参数调用PowerShell脚本

我有一个存储在文件中的PowerShell脚本。 在Windows PowerShell中,我执行脚本为 .\MergeDocuments.ps1 “1.docx” “2.docx” “merge.docx” 我想从C#调用脚本。 目前我使用Process.Start如下工作: Process.Start(POWERSHELL_PATH, string.Format(“-File \”{0}\” {1} {2}”, SCRIPT_PATH, string.Join(” “, filesToMerge), outputFilename)); 我想使用Pipeline类运行它,类似于下面的代码,但我不知道如何传递参数(请记住我没有命名参数,我只是使用$ args) // create Powershell runspace Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace); runSpaceInvoker.Invoke(“Set-ExecutionPolicy Unrestricted”); // create a pipeline and feed it the script text (AddScript method) or use the filePath (Add method) […]

如何修改前一行控制台文本?

我想实现这样的目标: Time consuming operation…OK Another time consuming operation… And another one, but it completed, so…OK 我显示了3行文本,每行都与一个可以迟早结束的post相关。 但如果第二个比第三个完成,我会得到这样的东西: Time consuming operation…OK Another time consuming operation… And another one, but it completed, so…OKOK 这当然是不可接受的。 我知道如何回到目前的路线,但有没有办法上线? 我发誓我已经在某个地方见过它,虽然它可能是一个Linux控制台:) 算了吧。 见远程文件管理器! 它适用于Windows控制台,甚至可以在PowerShell中运行! 如何制作这样的东西? 最酷的部分是退出后恢复控制台状态。 所以也许我应该问 – 如何直接访问控制台缓冲区? 我假设我需要一些本机代码来完成这个技巧,但也许还有另外一种方法呢? 我想到每次更新清除控制台,但这似乎有点矫枉过正。 或许它不是? 它会眨眼吗?

如何将windows phone 10应用程序部署到设备?

我正在使用诺基亚lumia630设备,它使用最新的Windows 10内部预览版本。 我创建了一个示例Windows UWP应用程序并采用了相同的构建。构建的输出是一个Appx。 如何将此Appx安装到我的设备上? 谁有任何想法?

如何托管Powershell脚本或应用程序,以便可以通过WSManConnectionInfo访问它? (如Office 365)

我知道连接到远程运行空间的唯一方法包括以下参数 WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, “localhost”, 80, “/Powershell”, “http://schemas.microsoft.com/powershell/Microsoft.Exchange”, credential); 要么 WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, “localhost”, 5985, “/wsman”, “http://schemas.microsoft.com/powershell/Microsoft.Powershell”, credential); 如何设置我自己的自定义Powershell对象,以便通过HTTP公开它? 使用的正确参数是什么以及如何设置它们?

从C#应用程序运行PowerShell脚本

我正在尝试从ac#应用程序执行PowerShell脚本。 该脚本必须在特殊的usercontext下执行。 我尝试过不同的场景,有些人正在努力: 1.来自PowerShell的直接调用 我直接从ps-console调用脚本,该控制台在正确的usercredentials下运行。 C:\Scripts\GroupNewGroup.ps1 1 结果:成功运行脚本。 2.来自ac#console应用程序 我从ac #consoleapplication调用了脚本,该脚本是在正确的usercredentials下启动的。 码: string cmdArg = “C:\\Scripts\\GroupNewGroup.ps1 1” Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.ApartmentState = System.Threading.ApartmentState.STA; runspace.ThreadOptions = PSThreadOptions.UseCurrentThread; runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript(cmdArg); pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output); Collection results = pipeline.Invoke(); var error = pipeline.Error.ReadToEnd(); runspace.Close(); if (error.Count >= 1) { string errors = “”; foreach (var […]

从C#以管理员身份执行PowerShell

我有以下C#代码 using (RunspaceInvoke invoker = new RunspaceInvoke()) { invoker.Invoke(“Set-ExecutionPolicy Unrestricted”); // … } 这给了我一个例外 访问注册表项“HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ PowerShell \ 1 \ ShellIds \ Microsoft.PowerShell”被拒绝。 据此,解决方案是以管理员身份启动PowerShell。 通常,可以通过右键单击PowerShell并选择“以管理员身份运行”来完成此操作。 有没有办法以编程方式执行此操作?

在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方法最简单的方法是什么?

在Pipeline.Invoke抛出后在C#中捕获Powershell输出

我正在从C#应用程序运行Powershell测试脚本。 由于cmdlet错误导致管道失败,导致pipe.Invoke()抛出exception。 我能够捕获关于exception所需的所有信息,但是我希望能够显示脚本的输出到那时为止。 我没有运气,因为抛出exception时结果似乎为null。 有什么我想念的吗? 谢谢! m_Runspace = RunspaceFactory.CreateRunspace(); m_Runspace.Open(); Pipeline pipe = m_Runspace.CreatePipeline(); pipe.Commands.AddScript(File.ReadAllText(ScriptFile)); pipe.Commands.Add(“Out-String”); try { results = pipe.Invoke(); } catch (System.Exception) { m_Runspace.Close(); // How can I get to the Powershell output that comes before the exception? }

在C#中导入PowerShell模块

我正在尝试编写一些C#代码以使用PowerShell与Lync交互,我需要在执行Lync cmdlet之前导入Lync模块。 但是,我的代码似乎没有导入模块,我不断收到“找不到get-csuser命令”exception。 这是我的代码: PowerShell ps = PowerShell.Create(); ps.AddScript(@”import-module Lync”); ps.Invoke(); ps.Commands.AddCommand(“Get-csuser”); foreach (PSObject result in ps.Invoke()) { Console.WriteLine(result.Members[“Name”].Value); } 不知道如何导入Lync模块?

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的名称 […]