Click here to Skip to main content
13,147,344 members (31,495 online)
Rate this:
Please Sign up or sign in to vote.
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();
            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");
            //Return the output from the script
            //int i = pipeline.Invoke().Count;
            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.

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

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!!
Posted 25-Apr-13 23:44pm
Updated 26-Apr-13 0:10am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

What you are trying to run won't run in an out-of-the box powershell session. The missing cmdlet is one for the Microsoft System Center 2012 Configuration Manager SP1 console. So if you run a lecay powershell, you will have to import ConfiguratioManager module (see:[^])
So I suggest you try adding
pipeline.Commands.Add("Import-Module").AddArgument("C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1");

before adding your own command to the pipeline.
MitchG92_24 26-Apr-13 6:08am
Sorry Zoltan - forgot to include my powershell script. I import the module within the script itself. Do you think it is better practice to do it via c#?
I have included my script in the question now.
Zoltán Zörgő 26-Apr-13 6:16am
Still, it looks like your module is not loaded. Remove import from script.
1) Try importing as I suggested before, so you can see any exception
2) Try this one:
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { @"path to psd1"});
Runspace runspace = RunspaceFactory.CreateRunspace(iss); of your code
MitchG92_24 26-Apr-13 6:23am
Thank you, that worked perfectly! My scirpt is now running fine.
thanks for the help :)
MitchG92_24 26-Apr-13 8:08am
Correction, realised i had the cmdlets it was erroring on commented out (to see that the variables were going in ok). Still have the same problem :( Is there a specific way to ensure that the c# app runs a 32-bit version of powershell on a 64-bit OS?
Zoltán Zörgő 26-Apr-13 13:18pm
Are you sure you need to run x86 PS? If you start System Center Console, is it running in x64 or x86 mode?
Check this, might help:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 26 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100