问题标签 [powershell-hosting]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
609 浏览

.net - 了解 PowerShell 托管

我已经完成了一些托管 PowerShell 的工作并阅读了大量内容,但我看到了奇怪的行为,这让我想知道我是否像我想象的那样不了解主机。

我正在使用 RunspaceFactory 创建一个运行空间:

我在主机的整个执行过程中使用相同的运行空间。当我第一次启动主机时,我调用了 Import-Module 命令:

“目录”是一个不是默认模块目录的目录。我可以在 PowerShell 命令窗口中使用具有完全相同语法的相同 import-module 命令,它工作正常。该命令似乎在我的自定义主机中成功完成。在执行的后期,我尝试在模块中调用一个 cmdlet:

但是在调用时,我得到一个例外,即“Get-Stuff”不是 cmdlet..etc。

我的理解是 Runspace 会保持这种状态。不是这样吗?我在另一个项目中成功创建了主机。两个主要区别是该主机使用默认模块目录 (Documents\Modules\),我调用 CreateRunspace() 方法如下:

var runspace = Runspace.CreateRunspace(customHost);

我是否必须定义一个 PSHost 才能保持状态?

0 投票
2 回答
918 浏览

asp.net - hosting powershell like cli in asp.net mvc to perform maintaince

I need CLI for our asp.net mvc application to perform maintenance tasks and some status tasks.

I think powershell is designed to provide CLI. But i do not know anything about it other than name.

How can i host powershell in asp.net mvc running in iis to provide CLI for custom tasks?

0 投票
1 回答
2549 浏览

c# - 在 Web 应用程序中托管 powershell 运行空间

我正在编写一个执行 powershell 脚本(活动目录、目录管理等)的 Web 服务。

现在,Runspace实例是根据 Web 请求创建的。由于 Runspace 初始化是一项耗时的操作,而且我经常需要导入模块,例如 ActiveDirectory,这些操作也很慢。

在这篇博客文章使用 C# 管理 Exchange 2007 收件人中,运行空间实例保存在静态字段中。

我徘徊如果我将 Runspace 实例保留在静态字段中,它会是线程安全的吗?也许这样做还有其他缺点?

谢谢

0 投票
1 回答
257 浏览

powershell - 我可以使用 Powershell 命令窗口与 Powershell 托管应用程序交互吗?

我正在开发一个嵌入 Powershell 的应用程序。我想知道是否有一种方法可以启动单独的 Powershell 命令窗口,并通过此 Powershell 窗口与我的应用程序进行交互?谁能指出一些参考?非常感谢。

0 投票
1 回答
618 浏览

.net - 使用自定义 cmdlet 托管受限 Powershell

我在我的应用程序中托管 powershell 并设置了一个受限的运行空间池,它基本上是空的(据我所知)。

显然,当我运行这段代码时......

它失败是因为没有可用的“Import-Module”cmdlet。所以,我的问题是如何在没有“Import-Module”cmdlet 可用的情况下导入模块?

这是我收到的错误消息...

术语“Import-Module”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。

0 投票
2 回答
7131 浏览

powershell - 托管 PowerShell:PowerShell vs. Runspace vs. RunspacePool vs. Pipeline

我试图在我的应用程序中添加一些相当有限的 PowerShell 支持:我希望能够定期运行用户定义的 PowerShell 脚本并显示任何输出,并且(最终)能够处理进度通知和用户提示请求。我不需要命令行样式的交互支持,或者(我认为)远程访问或运行多个同时脚本的能力,除非用户脚本在我托管的 shell 中自行完成。我最终希望异步运行脚本或在后台线程上运行脚本,并且可能使用一些初始变量和可能的 cmdlet 为 shell 播种,但这与此功能可能获得的一样“花哨”。

我一直在阅读有关编写主机应用程序代码的 MSDN 文档,但是虽然它愉快地解释了如何创建PowerShell对象,或Runspace,或RunspacePool,或Pipeline,但没有任何迹象表明为什么人们会选择这些方法中的任何一种而不是另一种。

我只能选择这两种方法中的一种,但我喜欢一些关于哪种方法更好的反馈:

或者:

(必需的PSHost派生类方法之一是EnterNestedPrompt(),我不知道我运行的用户定义脚本是否会导致它被调用。如果可以,那么我将负责“启动一个新的嵌套输入循环”(根据这里)......如果这会影响上面采取的路径,那也很高兴知道。)

谢谢!

0 投票
1 回答
161 浏览

powershell-hosting - 为什么我的 PowerShell 主机在 64 位机器上运行为 32 位?

我正在 VS 2012 中构建 PowerShell 主机,目前在 C# (.NET 4.5) 控制台项目中。该项目配置为任何 CPU,并且未选中“首选 32 位”。我通过添加手动添加了对 System.Management.Automation 的引用

到 ItemGroup 与 .csproj 文件中的其他引用。我从注册表中读取到奇怪的行为(缺少键等),我意识到它正在查看 x86 部分。所以我在主机中运行了这个脚本:

然后回来了

考虑到这种行为,这并不令人惊讶,但考虑到配置,这并不令人惊讶。有人有什么想法吗?托管的 PowerShell 有什么必须是 x86 的吗?

0 投票
1 回答
66 浏览

c# - 我可以在我托管的 PowerShell 中创建变量并启动我的类的方法吗?

PowerShell 4.0

我想在我的应用程序中托管 PowerShell 引擎,并能够在托管的 PowerShell 中使用我的应用程序的 API。我在文档中阅读了PowerShell 类及其成员的描述。在PowerShell.exePowerShell_ISE.exe主机中,我可以创建变量、循环、启动类的静态和实例方法。我可以通过PowerShell课堂做同样的事情吗?我找不到关于它的例子。

这是我的简单尝试:

0 投票
1 回答
298 浏览

c# - 如何创建用于处理静态成员的 PowerShell 变量?

PowerShell 4.0

在我的应用程序中,Application该类具有一组重要的属性、方法和事件。我想通过appPowerShell 变量与这些成员一起工作(它就像类的别名)。但是Runspace.SessionStateProxy.SetVariable期望第二个参数中的类实例:

我该怎么做?

0 投票
1 回答
435 浏览

c# - Add-Type cmdlet: is it possible to point the set of the DLL files instead of Assembly names?

I need to launch the cmdled in PowerShell is hosted inside of AutoCAD. Assemblies of AutoCAD (it is host of PowerShell) are not in GAC. How can I correctly to point the assemblies of AutoCAD? Is it possible to point the set of the DLL files instead of Assembly names? All necessary assemblies already loaded in the current AppDomain.

This is code of the ../Resources/example.cs file:

But I get errors:

The name of type or namespace of "DatabaseServices" is absent in a namespace of "Autodesk.AutoCAD" (the assembly reference is passed?).
The name of type or namespace of "Application" is absent in a namespace of "Autodesk.AutoCAD.ApplicationServices" (the assembly reference is passed?)

How can I fix it?