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.
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
RunspaceInvoke runspaceInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
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);
Collection<PSObject> returnObjects = pipeline.Invoke();
runspace.Close();
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.
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
$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
}
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!!