Click here to Skip to main content
Rate this: bad
good
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();
            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 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 26-Apr-13 0:44am
Edited 26-Apr-13 1:10am
v3

1 solution

Rate this: bad
good
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: http://blogs.technet.com/b/configmgrteam/archive/2013/03/27/powershell-connecting-to-configuration-manager.aspx[^])
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.
  Permalink  
Comments
MitchG92_24 at 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ő at 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 at 26-Apr-13 6:23am
   
Thank you, that worked perfectly! My scirpt is now running fine.
thanks for the help :)
MitchG92_24 at 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ő at 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)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 26 Apr 2013
Copyright © CodeProject, 1999-2014
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