Click here to Skip to main content
15,897,371 members

Running Powershell within C# and capturing return value

MitchG92_24 asked:

Open original thread
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!!
Tags: C#, PowerShell, Webservice

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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