首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# Powershell从脚本文件执行自定义函数

C# Powershell从脚本文件执行自定义函数
EN

Stack Overflow用户
提问于 2021-07-29 15:10:44
回答 1查看 184关注 0票数 0

我有一个包含多个函数的powershell脚本文件。我想使用C#调用其中一个函数。当我调用命令时,它会抛出一个错误。

代码语言:javascript
复制
    "The term 'LogIn-System' is not recognized as the name of a cmdlet, 
function, script file, or operable program. Check the spelling of the name, 
or if a path was included, verify that the path is correct and try again."

下面是一些示例代码:

代码语言:javascript
复制
using (PowerShell powerShellInstance = PowerShell.Create())
{
   powerShellInstance.Runspace = runSpace;

   powerShellInstance
     .AddScript("myfunctions.ps1")
     .Invoke();
   powerShellInstance
     .AddCommand("LogIn-System")
     .AddParameter("SystemName", "Connect");
   Collection<PSObject> PSOutput = await Task.Run(() => 
     powerShellInstance.Invoke());
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-03 20:15:41

您必须使用两个单独的调用。一个用来导入模块,另一个是您的脚本。因为Import是来自上一个加载模块的命令,所以使用AddCommand调用它。使用AddParameter函数指定要导入的模块名。例如:

代码语言:javascript
复制
PS
   .AddCommand("Import-Module")
   .AddParameter("Name", "ExchangeOnlineManagement")
   .Invoke();

PS
   .AddScript([your script name])
   .Invoke();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68578487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档