Tag: powershell

使用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#方法而不提供可选参数

我有一个ac#方法我从一个带有可选字符串参数的dll加载,默认为null 。 例如 public void foo(string path, string new_name = null, bool save_now = true) { if(name == null) new_name = Path.GetFileNameWithoutExtension(path); … if(save_now) Save(); } 我想在powershell脚本中调用它,而不是为new_name提供值,而是为save_now提供一个值。 根据这个看似非常相似的问题,我试过了 $default = [type]::Missing $obj.foo($path, $default, $false) 但这会导致new_name在函数中被设置为”System.Reflection.Missing” 。 另外我试过了 $obj.foo($path, $null, $false) 但这会导致new_name设置为空字符串,仍然不为null 。 我可以将默认值设置为空字符串,但我想知道是否有任何好方法可以使用默认值。

从powershell-script登录到csharp-program(log4net-logfile)

我用C#编写了一个程序,它创建了一个日志文件并使用log4net填充它。 该程序启动PowerShell脚本。 脚本也应该使用log4net。 我想监视脚本在运行时记录到log4net的内容,并在我的日志文件中写入此信息。 您知道我是如何做到这一点的,或者我在哪里获得有关我的问题的信息/帮助? 谢谢

我可以从PowerShell访问My Custom .NET Class吗?

关于PowerShell和.NET类,我有几个疑问和疑问。 我正在尝试编写一个类’foo’,它将调用Rest Web服务并执行一些任务。 如果我在GAC中部署该类,那么我可以从PowerShell调用它吗?

会话0隔离的启动过程

在Windows 8.1上,我有一个启动PowerShell脚本的服务。 该服务在Session 0 Isolation中作为“nt authority \ system”运行。 我从PowerShell中生成的任何进程在Session 0 Isolation中作为“nt authority \ system”运行。 我需要运行会话0之外的用户帐户下的脚本,而不是系统帐户。 我试过这个 Start-Process “$PsHome\PowerShell.exe” -Credential $pp -ArgumentList $script -wait 和PsExec使用“-I 1”参数指定我想要的会话。 & PsExec.exe “Install.bat” -i 1 -accepteula -u “domain\user” -p “awesomePassword” -w “startdir” -h 我尝试过设置“允许服务与桌面交互”。 当我尝试从PowerShell或c#服务启动该过程时,我不断收到Access被拒绝的错误。 当我尝试使用服务上的c#进行转义时,这是一个示例exception。 System.ComponentModel.Win32Exception (0x80004005): Access is denied at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) 如何逃离会话0? 我可以重新编写c#代码以在不同的用户下启动进程。 或者我可以重新编写调用的PowerShell脚本以作为用户启动另一个进程。 无论我尝试什么,我似乎都无法突破会话0。

如何创建C#async powershell方法?

所以我想创建一种异步运行PowerShell脚本的方法。 下面的代码是我到目前为止,但它似乎不是异步,因为它锁定应用程序,输出不正确。 public static string RunScript(string scriptText) { PowerShell ps = PowerShell.Create().AddScript(scriptText); // Create an IAsyncResult object and call the // BeginInvoke method to start running the // pipeline asynchronously. IAsyncResult async = ps.BeginInvoke(); // Using the PowerShell.EndInvoke method, get the // results from the IAsyncResult object. StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject result […]

Powershell在读取文件时保持文本格式

我相信这是一个简单的问题,但我无法绕过它。 我想在Windows上的命令shell中执行诊断命令。 像这样: $cmd = “ipconfig >> c:\test.txt” $message = Invoke-Expression($cmd) [String]$message = Get-Content c:\topsecret\testme.txt 然后我希望能够读取文件并保留格式,最后通过api将其发布到pastebin。 我尝试过,但无论我做什么,我似乎都失去了格式,这可能吗?

Powershell Cmdlet缺少组装Google Api

只在尝试创建Powershell Cmdlet时遇到问题。 我可以在控制台应用程序中的进程记录中执行相同的代码。 想法是App.config没有重定向程序集,但没有太大的成功使其工作。 我使用nuget包来引用Google-api-dotnet-client 示例代码在这里 http://tinyurl.com/mbx3yle 错误 http://sofzh.miximages.com/c%23/xiEjwAR.png Get-GAppsUsers : Could not load file or assembly ‘System.Net.Http.Primitives, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The system cannot find the file specified. PsTest.cs using System.ComponentModel; using System.Collections.Generic; using Google.Apis.Admin.Directory.directory_v1.Data; using System.Management.Automation; using System; using Google.Apis.Admin.Directory.directory_v1; using Google.Apis.Authentication.OAuth2; using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; using Google.Apis.Services; using Google.Apis.Util; using […]

将另一种语言转换为PowerShell或使用powershell中的语言

我找到了这段代码 // “url” is the full destination path (including filename, ie https://mysite.sharepoint.com/Documents/Test.txt) // “cookie” is the CookieContainer generated from Wichtor’s code // “data” is the byte array containing the files contents (used a FileStream to load) System.Net.ServicePointManager.Expect100Continue = false; HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest; request.Method = “PUT”; request.Accept = “*/*”; request.ContentType = “multipart/form-data; charset=utf-8”; […]

“尝试使用模拟在远程计算机上运行PowerShell脚本时,不允许”请求注册表访问“

这是我第一次尝试从C#应用程序执行PowerShell脚本。 我正在使用PowerShell,因为我需要从远程计算机上执行的.exe输出。 我能够使用WMI在远程计算机上运行.exe,但是我无法获得所需的输出。 无论如何,过去一天左右我一直在这里,我已经浏览了网络,并在SO处寻找类似的问题,但似乎无法弄清楚问题。 我正在尝试从远程计算机上的.NET 4.0应用程序运行一个简单的PowerShell命令。 当我以管理员身份运行Visual Studio 2013时,以下代码执行正常: PowerShell ps = PowerShell.Create(); ps.AddScript(@”Invoke-Command {c:\path\to\file.exe /p} -computername “); results = ps.Invoke(); 我得到了预期的结果。 但是,当我作为非管理员运行VS时,代码似乎执行正常(没有例外),但我没有得到任何结果。 环顾四周之后,我添加了如下假冒: using (var impersonator = new Impersonator(“username”, “domain”, “password”)) { PowerShell ps = PowerShell.Create(); ps.AddScript(@”Invoke-Command {c:\path\to\file.exe /p} -computername “); results = ps.Invoke(); } 但是,ps.Invoke方法开始抛出System.Security.SecurityException – “不允许请求的注册表访问”。 这是堆栈跟踪: 在System.Management的System.Management.Automation.ModuleIntrinsics.GetExpandedEnvironmentVariable(String name,EnvironmentVariableTarget target)的System.Environment.GetEnvironmentVariable(String variable,EnvironmentVariableTarget target)上的Microsoft.Win32.RegistryKey.OpenSubKey(String […]