Click here to Skip to main content
15,886,752 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
//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();


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:

VB
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
Updated 26-Apr-13 0:10am
v3

1 solution

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: http://blogs.technet.com/b/configmgrteam/archive/2013/03/27/powershell-connecting-to-configuration-manager.aspx[^])
So I suggest you try adding
C#
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.
 
Share this answer
 
Comments
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);
...rest 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: http://stackoverflow.com/questions/14508921/invoking-a-x86-powershell-script-from-an-x64-c-sharp-application

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900