Tag: powershell

在C#中运行Powershellscript

我试图在Windows窗体中通过C#运行PowerShell脚本。 问题是我有两个枚举,我无法在代码中得到它们: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Runspaces; namespace WindowsFormsApp6 { static class Program { /// /// Der Haupteinstiegspunkt für die Anwendung. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } *here } 我理解,我是否必须在静态void下添加以下内容? (在这儿): using (PowerShell PowerShellInstance = PowerShell.Create()) { } […]

在Visual Studio中运行PowerShell脚本时无法获取Active Directory终端服务属性

我遇到了一个奇怪的问题,也许有人可以帮助我。 我试图在使用Windows 10的计算机上使用C#从Active Directory用户检索终端服务属性。我这样做是通过在我的应用程序中运行PowerShell脚本,如下所示: var script = $@”Import-module ActiveDirectory $user=[ADSI]””LDAP://192.111.222.33:389/CN=SomePerson,DC=Domain,DC=local”” $user.psbase.Username = “”administrator”” $user.psbase.Password = “”adminPassword”” $user.psbase.invokeget(“”TerminalServicesProfilePath””)”; using (var runspace = RunspaceFactory.CreateRunspace()) { runspace.Open(); using (var pipeline = runspace.CreatePipeline()) { pipeline.Commands.AddScript(script); var test = pipeline.Invoke(); Console.WriteLine(“Success: “); return true; } } 我得到这个例外: System.Management.Automation.MethodInvocationException:’exception调用“InvokeGet”带有“1”参数:“未知名称。(HRESULTexception:0x80020006(DISP_E_UNKNOWNNAME))”’ 当我在使用Windows Server 2012作为操作系统的计算机上运行Visual Studio 2015中的上述代码时,它工作正常! 我确保我的Windows 10机器也安装了RSAT。 奇怪的是,当我从Windows 10机器上的PowerShell控制台运行脚本时,它可以工作! 以下是我的PowerShell脚本的确切代码: $user = […]

如何在“Get-Help”中显示C#Cmdlet参数HelpMessage

我已经启动了PowerShell cmdlet,并希望为参数提供帮助消息。 我已经尝试过使用ParameterAttribute.HelpMessage : [Cmdlet(VerbsCommon.Get, “Workspace”, SupportsShouldProcess = true)] public class GetWorkspace : PSCmdlet { [Parameter( Mandatory = true, Position = 1, HelpMessage = “The path to the root directory of the workspace.”)] public string Path { get; set; } protected override void ProcessRecord() { base.ProcessRecord(); } } 但是,当我使用PowerShell Get-Help命令时,它不会显示参数的HelpMessage: get-help Get-Workspace -det NAME Get-Workspace […]

使用Powershell RunspacePool从C#multithreading到远程服务器

我正在尝试使用Powershell RunspacePool创建一些C#代码到远程服务器。 当maxRunspaces设置为1时,一切正常,但将其设置为2会带来麻烦。 var connectionInfo = new WSManConnectionInfo(target, shellUri, credential); using (RunspacePool pool = RunspaceFactory.CreateRunspacePool(1, 2, connectionInfo)) { pool.ApartmentState = System.Threading.ApartmentState.STA; pool.ThreadOptions = PSThreadOptions.UseNewThread; pool.Open(); var tasks = new List(); for (var i = 0; i < 12; i++) { var taskID = i; var ps = PowerShell.Create(); ps.RunspacePool = pool; ps.AddCommand("Get-NAVServerInstance"); var task = […]

C#System.Management.Automation.Powershell类在Invoke()调用时泄漏内存

我有一个C#测试应用程序的代码,它使用PowerShell查询系统信息,在其中我有一些简单的函数,如下所示: public void GetSystemInfo() { using (PowerShell ps = PowerShell.Create()) { ps.AddCommand(“Get-Disk”); // Get-Disk is an example; this varies foreach (PSObject result in ps.Invoke()) { // … // Do work here. Process results, etc… // … } } } 这很简单,主要来自MSDN示例,例如: https : //msdn.microsoft.com/en-us/library/dd182449(v = vs。85).aspx 问题是,每次调用此函数时,我都可以看到应用程序的内存占用增长。 足迹在调用ps.Invoke()时正在增长,并且永远不会收缩。 我如何处理数据并不重要; 我甚至可以完全评论foreach循环的主体,并且内存永远不会被垃圾收集。 从我看到PowerShell类接口可以看出,似乎没有办法强制它自我清理,并且Dispose()显然不起作用,因为资源在使用后仍然在内存中泄漏阻止退出。 这是C#PowerShell类实现中的已知错误还是PowerShell本身? 是否有人知道解决方案,或者可能的替代方案? 编辑 我现在也试过了,结果相同: public […]

如何通过* .csproject文件查找引用路径

我想制作一个自动的PowerShell脚本,它报告项目的引用和引用路径。 当未填写.csproj中的提示路径时,我找不到获取引用路径的方法。

在C#中更快地调用PowerShell cmdlet

我可以使用C#中的Powershell cmdlet从O365获取用户详细信息。 问题是取货时间。 那太慢了。 每个用户需要2秒钟,因此如果我拥有大量用户,则会导致时间问题。 在这里,我只想打印所有用户的信息,如名称,组详细信息,许可证。 我怎样才能更快地完成? 尝试过一个: Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); Pipeline UserDetailsPipe = runspace.CreatePipeline(); UserDetailsPipe.Commands.AddScript(“Get-AzureADUser”); foreach (PSObject info in UserDetailsPipe.Invoke()) /////////******* { ArrayList Groups = new ArrayList(); // to hold memberOf ArrayList Licenses = new ArrayList(); // to hold of licenses string UserPrincipalName = info.Members[“UserPrincipalName”].Value.ToString(); string DisplayName = info.Members[“DisplayName”].Value.ToString(); //Getting MemberOf Pipeline […]

c#中的powers powershell命令如何

目标是获取Exchange 2010站点的最小数据库,因此我尝试从c#运行以下powershell命令, Get-MailboxDatabase -server Exchange2010 -Status | select-object Name,DatabaseSize 我正在努力的问题是 – 如何管道Select子句命令。 这是我的尝试, WSManConnectionInfo wsConnectionInfo = new WSManConnectionInfo(new Uri(“https://” + ExchangeSite + “/powershell?serializationLevel=Full”), “http://schemas.microsoft.com/powershell/Microsoft.Exchange”, getCredential()); wsConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; wsConnectionInfo.SkipCACheck = true; wsConnectionInfo.SkipCNCheck = true; rsRemoteRunspace = RunspaceFactory.CreateRunspace(wsConnectionInfo); rsRemoteRunspace.Open(); Pipeline pipeLine = rsRemoteRunspace.CreatePipeline(); Collection DatabaSize = null; Command myCommand = new Command(“Get-MailboxDatabase”); myCommand.Parameters.Add(“Server”, “Exchange2010”); myCommand.Parameters.Add(“Status”, null); […]

Powershell C#异步执行

我想在C#中异步执行Powershell脚本。 我已经使用了BeginInvoke方法,但即使我为它们附加了委托,我也无法获得输出/错误。 这是我的C#代码: using(ps=PowerShell.Create()) { PSDataCollection output=new PSDataCollection(); output.DataAdded+=output_DataAdded; ps.AddScript(RenewalScript); SecureString ss = new SecureString(); for (int i = 0; i < Password.Length; i++) ss.AppendChar(Password.ElementAt(i)); PSCredential cred = new PSCredential(Username, ss); ps.AddParameter("Credential", cred); ps.AddParameter("BuildServer", Server); ps.AddParameter("CAServer", CAServer); ps.AddParameter("CAName", CAName); ps.Streams.Error.DataAdded += Error_DataAdded; var result=ps.BeginInvoke(null, output); } void output_DataAdded(object sender, DataAddedEventArgs e) { //display output in […]

将剪贴板中的文本发送到应用程序,如记事本(C#或Powershell)

我希望能够将剪贴板(在Windows中)上的文本发送到应用程序。 例如,我正在写一个记事本中的文本文件,我想将一部分复制到一个新文件中..我想将它复制到剪贴板然后使用热键启动发送的应用程序或powershell脚本将文本复制到记事本的新实例。 如何在C#或Powershell中实现这一目标? 解决方案:使用AutoHotKey ^+c:: Send ^c Run Notepad WinWait Untitled – Notepad WinActivate Send ^v return