Invoking PS script from C# code (Visual studio 2017) - c#

I am trying to invoke my PS script from C# code.
**PS code. -- Try.ps1**
Write-Host "HelloHost"
Write-Debug "HelloDebug"
Write-Output "HelloOutput"
echo "tofile" > $PSScriptRoot/a.txt
**C# code**
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;
namespace TryitOut
{
class Program
{
static void Main(string[] args)
{
using (PowerShell pshell = PowerShell.Create())
{
string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
pshell.AddScript(path + "\\Try.ps1");
IAsyncResult result = pshell.BeginInvoke();
while (result.IsCompleted == false)
{
Console.WriteLine("Waiting for PS script to finish...");
Thread.Sleep(1000);
}
Console.WriteLine("Finished!");
Console.ReadKey();
}
}
}
}
When I run "Try.ps1" independently it runs ok and creates a.txt ans shows console output as expected.
But, it not getting invoked/executed via this C# code.
Question 1. can you please help, what I am doing wrong?
Q 2. How can I see PS console output when invoking Try.ps1 via C# code
Thank you for your time :).

Dealing with output from the PowerShell object is done in two ways. One is the direct return values of the Invoke call, and the other is via various streams. In both cases, you are responsible for dealing with the data; the console does not automatically output them as when you run a script in the standard PowerShell window. For example, to get the results from Write-Object, you can add the following to your C# code after your while loop:
foreach(PSObject pso in pshell.EndInvoke(result))
{
Console.WriteLine(pso);
}
This works fine for the simple strings in your example, but for complex objects with multiple properties, you need to pull the individual properties by name. For example:
string propValue = pso.Members["PropertyName"].Value.ToString();
Have a look at a previous answer of mine to see one way to convert complex objects to your own type: Dealing with CimObjects with PowerShell Inside C#
Handle the debug, information, verbose, etc like this:
foreach(DebugRecord dbr in pshell.Streams.Debug)
{
Console.WriteLine(dbr);
}
You will need to add this to the start of your script as well:
$DebugPreference = 'continue'

Related

Powershell addscript and invoke from C# not displaying result on command line

I've the below powershell script file
C:\user\deskptop\script1.ps1
The contents of the script is just the below:
get-process
i'm trying to create a C# console app to get the output of this script on console. When i execute the script outside C# it runs fine but when i execute it inside C# it doesn't produce anything. i'm trying use addscript and invoke.
C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Collections.ObjectModel;
namespace InvokePowerShellScriptFrmCsharp1
{
class Program
{
static void Main(string[] args)
{
string script = #"C:\\Users\\Desktop\\script1.ps1";
PowerShell shell = PowerShell.Create();
shell.AddScript(script);
//shell.AddCommand("get-process");
shell.Invoke();
//shell.AddScript(script).Invoke();
Collection<PSObject> pSObjects = shell.Invoke();
foreach (PSObject p in pSObjects)
{
Console.WriteLine(p.ToString());
}
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
}
}
When i execute the above i get the console with 'Press any key to continue' but no output before that.
but if i just try the below i get the result
shell.addcommand("get-process");
I want to get this working with addscript coz in the future if there is more than one command in the powershell script then i need to be able to execute the script from C# for desired results.
i've tried many links to try research but don't seem to be getting it to work.
https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/
https://www.reddit.com/r/csharp/comments/692mb1/running_powershell_scripts_in_c/
Could someone please let me know where i could be going wrong.
Try loading the script contents first, then passing that to the AddScript method:
string script = File.ReadAllText(#"C:\Scripts\script1.ps1");

Sikuli Integrator C#

I want to use SikuliIntegrator in C#.
I run VS as administrator, install SikuliIntegrator throught NuGet manager and want to test him on simple task.
Heres my code
using SikuliModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SikuliTrainingNet
{
class Program
{
static void Main(string[] args)
{
string MyPicture = #"c:\111\Sik\MyPicture.png";
SikuliAction.Click(MyPicture);
}
}
}
after running code (and have prepared MyPicture on screen), all i get is exception "###FAILURE" any idea why?
I dont wanna use Sikuli4Net, becose its look like it work on web aps and I need just few simple clicks on desktop aplication.
I try sikuli in Java, and there it works with no problem. But I need to make my program in C#.
I Used This Code For Sikuli4Net in C#,It Was Working For Me First You need add the References please see this link for Reference
http://interviews.ga/angularjs/sikulic/
static void Main(string[] args)
{
APILauncher launch = new APILauncher(true);
Pattern image1 = new Pattern(#"C:\Users\Ramesh\Desktop\Images\userName.png");
Pattern image2 = new Pattern(#"C:\Users\Ramesh\Desktop\Images\password.png");
Pattern image3 = new Pattern(#"C:\Users\Ramesh\Desktop\Images\Login.png");
launch.Start();
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Url = "http://gmail.com";
Screen scr = new Screen();
scr.Type(image1, "abc#gmail.com", KeyModifier.NONE);
scr.Type(image2, "12345", KeyModifier.NONE);
scr.Click(image3, true);
Console.ReadLine();
}
I used this code and it was working fine. First you should open the webpage on which you want to click and then give a path of the image(it should be a part of the webpage)
here is my code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SikuliModule;
using OpenQA.Selenium;
namespace WordPressAutomation.DifferentTests
{
[TestClass]
public class Sikuli
{
[TestMethod]
public void TestMethod1()
{
driver.Initialize();
driver.instance.Navigate().GoToUrl("https://www.google.co.in");
SikuliAction.Click("E:/img.png");
}
}
}
To use SikuliInyegrator, you need to check the execution results in these files:
C:\SikuliExceptionLog.txt
C:\SikuliOutputLog.txt
Also you need to:
Have installed JRE7 or superior
Have environment variable PATH with the location of the bin folder
See installed in your “control panel > program and features> visual C++ 2010 Redistributable Package” at x86 and x64 bits according to your java JRE runtime platform. If not, then download and install the Redistributable Package form Microsoft site.

system ("pause") in C# won't work. Alternatives?

I am building a program for the purposes of displaying Smartgraph3 readings. Whenever I start debugging, the command line opens but it then immediately disappears. I know ctrl + f5 works, but I was looking for a solution where I would not have to enter the same command to keep it from disappearing.
I have used System("pause"); but it keeps coming up with a blue line under System, and in the error list says 'System' is a 'namespace' but is used like a 'variable'. Does anybody know what is wrong?
Also, I have heard System("pause") should not be used, so does anybody have an alternative that's just as effective?
Here is a copy of my code. Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Infowerk.SmartGraph3.SmartGraph3API;
//using CSTestClient.SmartGraph;
namespace CSTestClient
{
class Program
{
static void Main(string[] args)
{
TestAPI();
}
static void TestAPI()
{
System.ServiceModel.Channels.Binding service_binding = new BasicHttpBinding();
EndpointAddress endpoint_address = new EndpointAddress("http://localhost:8000/SmartGraph3API/APIV01");
SmartGraph3APIClient client = new SmartGraph3APIClient(service_binding, endpoint_address);
List<SG3APIDeviceIdentification> device_list = client.GetDeviceList();
foreach (SG3APIDeviceIdentification device_identification in device_list)
{
Console.WriteLine("device id: {0}", device_identification.DeviceId);
System("pause");
}
}
}
}
Go ahead and simply use:
Console.ReadKey();
Set a breakpoint on the closing brace of your Main(). Or use an IDE which streams console output to a debug window which persists after the process exits (e.g., Eclipse, I assume basically anything other than VS). No reason to force everyone who actually wants to run your program the normal way to invoke it like:
:; ./myProgram.exe < /dev/null; exit $?
.\myProgram.exe < NUL
See https://stackoverflow.com/a/20487235.

C# Console Application Does NOT read from the command line, as it should

I have spent a good while trying to get this simple code to read from the command line:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
if(args.Length > 0)
{
for (int i = 0; i < args.Length; ++i)
System.Console.WriteLine(args[i]);
}
else System.Console.WriteLine("NO COMMAND INPUT DETECTED");
System.Console.ReadLine();
}
}
}
When typing the command:
ConsoleApplication3.application "pleasework"
I get the following message in the command line:
NO COMMAND INPUT DETECTED
indicating that the command line is not working properly. Any thoughts? I am very bad with Visual Studio (this is 2012) so I imagine there is some special property I need to change or something ridiculous.
Thanks!
Using this:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(args.Length);
Console.Read();
}
}
}
I was able to go to my command prompt and run:
C:\ProjectPath\ConsoleApplication1\bin\debug\ConsoleApplivation1.exe "Test" "Test2"
With a result of:
2
Edit: It looks like you are running a ClickOnce Console Application. This complicates what you want to do, but it isn't impossible. Here are several resources that discuss this particular issue:
Processing Command Line Arguments in an Offline ClickOnce Application
Harvesting ClickOnce Command Line Arguments
How to pass arguments to an offline ClickOnce application
Your code seems to be okay.
You're probably not passing the argument properly.
Take the following steps:
Right-Click on your Project => Properties => Debug => insert "Pleasework" into the command line arguments => save and debug your code step-by-step.
And you'll see:

wmic via .NET or C#

All,
Please forgive my ignorance of C#/.NET, I am absolutely new to both (mostly Java, C/C++) and find myself tasked with creating some code that performs the same thing as the "wmic qfe" and "wmic os" commands available at the DOS prompt.
Can this be done? If so, any help is appreciated. I am trying to working my way through the System.Management.Instrumentation namespace since wmic is an acronym for Windows Management Instrumentation Command (according to Google), but have as yet to discover anything useful.
Thanks in advance,
Toddw
Instead of using the Management class, you can simply invoke the wmic.exe which is a WMI console as you can figure out from the exe name itself. Here is the code that I used to open notepad on a remote computer using WMIC invoked through C#.net. Try it out.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace RemoteProcessDemo
{
class Program
{
static void Main(string[] args)
{
string strComputerName = "";
string strProcString = "";
try
{
strComputerName = args[0];
Process.Start("wmic", "/node:" + strComputerName + " process call create notepad.exe");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Regards,
Tushar
You will indeed need to use the System.Management namespace to perform WMI queries. There is lots info on using WMI from C#, Microsoft's is (archived) here
For your specific cases:
qfe - query the Win32_QuickFixEngineering class
os - query the Win32_OperatingSystem class

Categories