Running PowerShell script in a C# Windows Service App - c#

I am really not asking for any specific lines of code here, but more along the lines of someone being able to explain to me the idea of it to help me have a better understanding. I am very new to coding with PowerShell scripts let alone calling them in a C# app. Would anyone be able to explain or even point me in the right direction so I can learn about what I would need to do here. I have a basic "Hello World" script that I would like to call from a C# Windows Service using VS2010. I have seen examples around the internet, but they are very brief and don't really teach the concept behind it.
Any help would be much appreciated!
Thanks!

Here is a pretty good discussion on how to call Powershell from C#: http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C
I suggest you start with the RunScript function and add it to a VS2010 console application. Main() would then invoke RunScript something like RunScript( "echo 'hello from PS';get-date; get-culture; ").
Windows services have quite a few differences from a normal console application. You can read about how to write a windows service on MSDN, but if you've never coded before, you have a steep learning curve in front of you.
There is a service from an old Resource Kit call srvany.exe (Google it) which would run any console app as a service. However in Windows Vista (and above) services were restricted from accessing the desktop, so srvany could only be successful in Vista and above if you EXE doesn't need keyboard input or need to write to the display. However your EXE could read/write files.

This is what I was able to get to work for me.
I basically followed the information that I found from this post, but changed a couple of things to get it to work for me: Problem with calling a powershell function from c#
For example, for the ps.AddCommand("BatAvg") I just added my own function in my .ps1 file ps.AddCommand("PS1FunctionName") and didn't add any parameters since it was not necessary. The answer was staring me in the face the whole time, I just didn't know where I needed to place it in my windows service. I was able to figure that out and now I am cruising!
Also, don't forget to reference using System.Management.Automation and using System.Management.Automation.Runspaces in your code (for all the future people that may look at this).

Related

How do I integrate a python script into my WPF C# application?

I am currently building an app that displays the video feed of a webcam/external camera. I have written the GUI with C# and in WPF. I have been asked to integrate a python code which does image analysis of the displayed feed. I'm not sure exactly where to begin with this.
I have tried to look into IronPython but the documentation isn't too clear so I'm quite lost. If anyone can point me in the right direction, that would be greatly appreciated.
There are at least two approaches you can take,
Calling Python Script Using New Process Initialization
Calling the Python Script Using IronPython Interpreter, Hosted in Your .NET Application
Check the below post for more information:
https://betterprogramming.pub/running-python-script-from-c-and-working-with-the-results-843e68d230e5
Another option is just to separate your application to microservices, and use python as one.
check this: https://softwareengineering.stackexchange.com/questions/385936/how-can-i-integrate-python-code-with-c-code
When it comes to using Python inside .NET applications so Python code can interact with .NET objects, Python.NET and IronPython are two popular choices.
We have implemented our code writing/scripting/debugging solutions for both of these engines, you may read a bit about pros and cons here

Running Skypekit as Service

I have developed a Gui-app that uses skypekit which works great. I know have the requirement to make it a service or as "service like" as possible.
I would need the skypekit.exe to run in some shape or form as a service. I was looking into wrapping the exe as a service , but I would need to wrap the gui and skypekit exe as one!.
I only found
http://gocoding.com/page.php?al=servicecontrol,http://gocoding.com/page.php?al=gensvc
Which did not work for. I found a link on the skype fourm here:
http://devforum.skype.com/t5/SkypeKit-API/Windows-Skype-Runtime-as-Windows-Service/m-p/3782/highlight/true#M3306
But no success either.
What I would like to know is how I might go about making 2 exe's into 1 service: if that is possible how?, or any other techniques I might try to achieve the result. There is very little out there in the way of this I can find.
AFAIK to run Skype as a service you need to run it in a virtual machine or isolated desktop environment.
Here is one example:
https://sevabot-skype-bot.readthedocs.org/

To Develop an Application:Using Mobile Camera as a Web Cam

I have been tasked with doing this application:
So far: I have a J2SE application which builds a bluetooth service and waits for an incoming connection.
A J2ME application which searches for the service and when a match is found, it streams images clicking continuously with the phone's camera. The J2SE app accepts the images and displays them on the desktop machine.
But according to my project specification, I need the desktop application to be developed in C#.
What I tried?
I tried to read the J2SE code line by line and reproduce the same in C#. After googling around I found that 32Feet library was a good choice. So I went ahead and read the documentation which has code samples in VB, and tried to implement it in C#. It started well with me discovering the local device successfully but eventually i got stuck in the part related to the creation of Bluetooth service.
While most of the terminology is same with ServiceRecord, UUID/GUID etc...but I have failed miserably. Moreover I tested some code which works in VB but fails in C#. Even I tried OBEX...(a small file transfer test) which worked yesterday but fails today with an exception.
Now I am contemplating of starting from scratch all over again. Can someone help me please in suggesting ways to proceed further. I mean in what tools/library/environment should i opt for. What is the best and reliable way to develop the desktop app in C# with the J2ME app already developed.
Help required please. All suggestions even small ones are welcome. I am relatively new to both Bluetooth and C#. I have some experience in Java and J2ME.
Its sad to see that you wish to leave back those libraries you did used and spend alot worty time tinkering with it.
Rather than leaving that effort, try to resolve it. Let us know what exceptions/errors your facing with those libs.
Many experts here shall surely help you with it. Or talk to the developer of those libs for any help needed.
Hope this kind of helps Bluetooth in C#, Which stack, Which SDK?

Getting input into a process

While talking with a friend over yahoo messenger, I told him would be really cool to make a bot to answer with generic messages when someone starts a conversation. Upon thinking about what I told him, I realized it would be quite interesting to do something like that. The problem is that I don't know much about win32.
So my question is this: how do you 'link' a process to both another one and the windows environment? The goal would be to have an application running in the background which makes some sort of a query to see what windows are opened and when a new yahoo messenger conversation window appears it should send a list of keystroke events to that window.
I could use either C# or VC++ for the programming part and I can use any help: either specific answers or tips that could help me - e.g.: what to google for. So far my google research only came up with some apps/dlls/code that do that for you and some scripting stuff and I'm not exactly searching for that. I want to do all the work myself so I can learn from it.
It seems like you basically want to control other applications.
There are roughly 2 ways to do this on windows
1 - Use the low level windows API to blindly fire keyboard and mouse events at your target application.
The basic way this works is using the Win32 SendInput method, but there's a ton of other work you have to do to find window handles, etc, etc
2 - Use a higher level UI automation API to interact with the application in a more structured manner.
The best (well, newest anyway) way to do this is using the Microsoft UI Automation API which shipped in windows vista and 7 (it's available on XP as well). Here's the MSDN starter page for it.
We use the microsoft UI automation API at my job for automated UI testing of our apps, and it's not too bad. Beware though, that no matter how you chose to solve this problem, it is fraught with peril, and whether or not it works at all depends on the target application.
Good luck
Not quite the same domain as what you're looking for, BUT this series of blog posts will tell you what you need to know (and some other cool stuff).
http://www.codingthewheel.com/archives/how-i-built-a-working-poker-bot
If you really want to learn everything from scratch, then you should use C++ and native WIN32 API functions.
If you want to play a bit with C#, then you should look the pinvoke.net site and Managed Windows API project.
What you'll surely need is the Spy++ tool.
http://pinvoke.net/ seems to be the website you are looking for. The site explains how to use Windows API functions in higher level languages. Search on pinvoke for any of the functions I've listed below and it gives you the code necessary to be able to use these functions in your application.
You'll likely want to use the FindWindow function to find the window in which you're interested.
You'll need the process ID, so use GetWindowThreadProcessId to grab it.
Next, you'll need to use OpenProcess allow for reading of the process's memory.
Afterwards, you'll want to use ReadProcessMemory to read into the process's memory to see what happening with it.
Lastly, you'll want to use the PostMessage function to send key presses to the window handle.
Welcome to the wonderful world of Windows API programming.
Check out Autohotkey. This is the fastest way to do what you want.

Can an exe compiled from C# be integrated into a website?

I'm a beginner in programming. I've just made a program called "Guessing Game". And it seems to work fine. Can I integrate it into a website? The CMS that I'm using is Mambo.
===
additional info's
Thanks for all your suggestions.
I still don't have any background about Silverlight, WPF and Java Script which I think sounds good. I'm using Windows and I programmed my "Guessing Game" from Microsoft Visual Studio 2008 and it's using Window application forms.
Yes I guess, for the moment I let it be and start to learn Silverlight or Java Script so that I can integrate it on my website:-)
Thanks for all your input guys:-)
Cheers
A standalone executable cannot be directly integrated into a website. You have a few choices though:
Allow your users to download the executable and run it locally for themselves
Rewrite your program in JavaScript to have it run directly inside of an HTML page, though this could obviously involve a fair amount of reworking
Use Microsoft's Silverlight technology, which allows you to code in C# and produce a web-based frontend similar to Adobe Flash. Your program logic should remain the same and you should only have to change the UI code. In fact if you're already using WPF for the front end, the transition will be even easier.
There are several questions that you still need to answer.
What is your server running? If its not Windows, your exe will not run at all unless it is compatible with Mono or a similar framework for your server's operating system.
How does your "Guessing game" interact with the user? If it is through a WinForms GUI, it will you will not be able to use that GUI on the web. If your game is a WPF application your easiest route may be to port it to Silverlight and serve it up on a web page.
It is typically not trivial to make a regular windows application run in a web environment since on on the web you are really running in the browser, not on Windows.
Yes - in general, when you're talking about software, anything is possible. The question is, how difficult will it be?
To understand that, you have to give us more details about "Guessing Game" including how it is designed, what it's interfaces are, how readily extensible it is, and how prepared you are to change or extend it.
For example, if it is a Windows Forms GUI app, then it will be diifficult to integrate into a web app. If it is a console app, then it will be a little easier. If you can modify it to run as a Windows Service, then a little easier. If you can modify it to accept input from the network (as opposed to getting input solely from the keyboard + mouse), still easier.
You may be able to use reflection to load your assembly into the web application, but most likely, the answer is no.
Your best solution is probably to re-write the game in javascript.
The short answer to your question is now. I'm presuming that since you're running Mambo you're web environment is a LAMP stack. However, you're "Guessing Game" is most likely a Windows application from the sound of it. For a beginner in programming, there is no integration path you're going to be able to take that will allow you to have your game running on your website.
However, here are avenues you can take, which will require a significant amount of time to learn. I'm not saying you shouldn't take time to learn, by all means you should! I'm simply trying to illustrate the fact that this is not something that is going to be doable in a couple of hours.
Silverlight - allows you to run C# code with a WPF like interface on your client's browser and can integrate with your web site through javascript.
Let your client download it from your website and run it off of their PC. This would actually be fairly trivial and would be your quickest option, but it sounds like it's not the kind of integration you were looking for.

Categories