在C#中运行Powershell并捕获返回值 [英] Running Powershell within C# and capturing return value
问题描述
大家好,
我创建了一些C#代码,用于将参数传递给powershell脚本,并尝试调用管道来获取返回代码。
Hi All,
I have created some C# code that passes parameters to a powershell script and trys to invoke the pipeline to get the return code.
//Create space for the session to run.
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
RunspaceInvoke runspaceInvoker = new RunspaceInvoke(runspace);
//Create a pipeline to send variables
Pipeline pipeline = runspace.CreatePipeline();
//Insert a value into exiting variables in script:
Command myCommand = new Command(psScriptPath, false);
CommandParameter param = new CommandParameter
("ComputerName", "TestingComputerName");
CommandParameter param3 = new CommandParameter
("MacAddress", "TestingMacAddress");
CommandParameter param1 = new CommandParameter
("CollectionName", "TestingColelctionName");
CommandParameter param2 = new CommandParameter
("SiteCode", "TestingSiteCode");
myCommand.Parameters.Add(param);
myCommand.Parameters.Add(param1);
myCommand.Parameters.Add(param2);
myCommand.Parameters.Add(param3);
pipeline.Commands.Add(myCommand);
//Return the output from the script
//int i = pipeline.Invoke().Count;
Collection<PSObject> returnObjects = pipeline.Invoke();
runspace.Close();
我可以将参数传递给它,但是Collection< psobject> ; returnObjects = pipeline.Invoke();这是问题所在。我收到错误消息:
I can pass parameters to it fine but the "Collection<psobject> returnObjects = pipeline.Invoke();" is the problem here. I get the error message :
The term 'Get-CMDevice' 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.
这是因为在System.Management.Automation.CommandNotFoundException中,它无法识别powershell脚本中使用的此特定cmdlet。我在网上看到它可能是由于C#exe指向哪个平台但是我已经尝试了x86和x64都没有占上风。
我的powershell脚本为如下:
This is because of the System.Management.Automation.CommandNotFoundException, it does not recognise this particular cmdlet used in the powershell script. I have read online that it can be due to which platform the C# exe is pointed at but i have tried both x86 and x64 to no prevail.
My powershell script as as follows:
Import-Module 'C:\Program Files\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' -force
$ComputerName = $args[0]
$MacAddress = $args[1]
$CollectionName = $args[2]
$SiteCode = $args[3]
$SiteDir = $SiteCode + ':'
cd $SiteDir
#If the computer dosent exist add it
$Device = Get-CMDevice -Name $ComputerName
if ($Device -eq $null) {
Import-CMComputerInformation -CollectionName $CollectionName -ComputerName $ComputerName -MacAddress $MacAddress
return 1
} else {
#Pop it in the collection
Add-CMDeviceCollectionDirectMembershipRule -CollectionName $CollectionName -Resourceid $device.ResourceID
return 2
}
是否有其他原因导致此错误?
有没有办法忽略这个错误并继续收集其余的值?
还有另一种方法可以返回实际的返回值并在c#中捕获它as .Invoke()做的工作比需要多吗?
非常感谢 - 我期待听到您的回复!!
Is there somehting else causing this error?
Is there a way to ignore this error and carry on collecting the rest of the values?
Is there another way to return the actual return value and capture it within the c# as .Invoke() is doing more work than needed?
Thank you very much - i look forward to hearing your replies!!
推荐答案
ComputerName =
ComputerName =
args [ 0 ]
MacAddress =
MacAddress =
这篇关于在C#中运行Powershell并捕获返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!