从 Powershell Cmdlet 调用函数

发布于 2024-09-28 10:25:14 字数 136 浏览 0 评论 0原文

我正在尝试创建一个调用 Powershell 函数的 cmdlet。这可以做到吗?

想法是使用静态 cmdlet 枚举一组数据,然后调用定义的函数为每个项目执行某些操作。我总是可以复制 - 粘贴枚举部分的基本模板,但在修改参数等时很容易出错。

I'm trying to create a cmdlet that calls Powershell function. Can this be done?

Idea is to have static cmdlet that enumerates a set of data and then calls defined function to do something for each item. I can always copy - paste a base template for the enumaration part, but it really easy to make errors while making modification to parameters etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2024-10-05 10:25:14

当然,请使用 PSCmdlet 上的 InvokeCommand 属性(假设您从该基类派生),例如:

Collection<PSObject> result = this.InvokeCommand.InvokeScript("somefunc", true,
                                 PipelineResultTypes.None, null, new[] {1,2,3});

请注意,在本例中 somefunc 采用三个参数 (1,2,3) 并且没有管道输入(通过 null 第四个参数)。

Sure, use the InvokeCommand property on PSCmdlet (assuming you derive from this base class) e.g.:

Collection<PSObject> result = this.InvokeCommand.InvokeScript("somefunc", true,
                                 PipelineResultTypes.None, null, new[] {1,2,3});

Note that in this case somefunc takes three parameters (1,2,3) and no pipeline input (pass null for the fourth parameter).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文