I have a powershell script file that contains multiple functions. I would like to call one of the functions using C#. When I invoke my command, it throws an error
"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."
Here is some sample code:
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());
}
AddScript()should be commands instead of a path. Try either dot-sourcing with.AddScript(".\myfunctions.ps1")or useImport-Modulelike.AddScript("Import-Module myfunctions.ps1")instead. Or check out one of the other suggestions in this question: stackoverflow.com/questions/6266108