我有一个包含多个函数的powershell脚本文件。我想使用C#调用其中一个函数。当我调用命令时,它会抛出一个错误。
"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."下面是一些示例代码:
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());
}发布于 2021-08-03 20:15:41
您必须使用两个单独的调用。一个用来导入模块,另一个是您的脚本。因为Import是来自上一个加载模块的命令,所以使用AddCommand调用它。使用AddParameter函数指定要导入的模块名。例如:
PS
.AddCommand("Import-Module")
.AddParameter("Name", "ExchangeOnlineManagement")
.Invoke();
PS
.AddScript([your script name])
.Invoke();https://stackoverflow.com/questions/68578487
复制相似问题