Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to run this powershell command from C#

PS> test-Dtc -LocalComputerName "$env:COMPUTERNAME" -RemoteComputerName "myRemotePC" -ResourceManagerPort 17100 -Verbose

and put output in list2

What I have tried:

I tried several recommendations from google.including


PowerShell ps = PowerShell.Create();
ps.AddCommand("Test-Dtc");
ps.AddArgument("-LocalComputerName \"$env:COMPUTERNAME\" -RemoteComputerName \"Remotepc\" -ResourceManagerPort 17100 -Verbose");

foreach (PSObject result in ps.Invoke())
{


listBox2.Items.Add(result);


} // End foreach.

but no luck so far.
Any help would be greatly appreciated
Posted
Updated 16-Sep-17 18:19pm
Comments
Richard MacCutchan 16-Sep-17 4:39am    
but no luck so far.
What is that supposed to mean?

1 solution

Yes, you should be seeing a runtime exception:
Quote:
System.Management.Automation.RuntimeException occurred
HResult=0x80131501
Message="-LocalComputerName "$env:COMPUTERNAME" -Verbose is not a valid local computer name."
A quick Google Search[^] found this: Executing PowerShell scripts from C# – Keith Babinec's Development Blog[^].

Now, the script will look something like:
C#
using (PowerShell PowerShellInstance = PowerShell.Create())
{
    // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
    // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
    PowerShellInstance.AddScript("Test-Dtc -LocalComputerName param($param1) -RemoteComputerName param($param2) -Verbose");

    // use "AddParameter" to add a single parameter to the last command/script on the pipeline.
    var localComputer = Environment.GetEnvironmentVariable("COMPUTERNAME");
    PowerShellInstance.AddParameter("param1", localComputer);

    // use "AddParameter" to add a single parameter to the last command/script on the pipeline.
    var remoteComputer = "[Remote Compter Name Goes Here]";
    PowerShellInstance.AddParameter("param2", remoteComputer);

    // invoke execution on the pipeline (collecting output)
    Collection<PSObject> PSOutput = PowerShellInstance.Invoke();

    // loop through each output object item
    foreach (PSObject outputItem in PSOutput)
    {
        // if null object was dumped to the pipeline during the script then a null
        // object may be present here. check for null to prevent potential NRE.
        if (outputItem != null)
        {
            //TODO: do something with the output item 
            // outputItem.BaseOBject
        }
    }
}

Full documentation found here: Microsoft Documents - Powershell > Test-Dtc[^]
 
Share this answer
 
v3
Comments
picasso2 17-Sep-17 13:46pm    
Many thanks, I placed the code inside a button (to triggered it)
using (PowerShell PowerShellInstance = PowerShell.Create())
{

PowerShellInstance.AddScript("Test-Dtc -LocalComputerName param($param1) -RemoteComputerName param($param2) -Verbose");


var localComputer = Environment.GetEnvironmentVariable("MyLocalPC");
PowerShellInstance.AddParameter("param1", localComputer);


MessageBox.Show("variable remotecomputer");
var remoteComputer = "MyRemoteSrv";
PowerShellInstance.AddParameter("param2", remoteComputer);


Collection<psobject> PSOutput = PowerShellInstance.Invoke();

MessageBox.Show("start loop");

foreach (PSObject outputItem in PSOutput)
{
MessageBox.Show("inside the foreach loop");

if (outputItem != null)
{


MessageBox.Show(outputItem.BaseObject.ToString());

}

else
{
MessageBox.Show(outputItem.BaseObject.ToString());

}

}
}

Nothing is output so I added few MessageBoxs . I noticed that message "start loop" is executed but NO code inside the foreach loop is ever gets executed.

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