Can you write Windows services in Powershell - powershell

I have written a program in PowerShell that loops and checks stuff.
I would like to convert this into a Windows service.
I've created a Windows service (in Admin->Services) but I can't start it.
I'm pretty sure I'm missing the proper interface that the system needs to call into in order to start/stop/pause/etc the service.
I can find plenty of examples when it comes to doing it in VB/C#/MS-lang but nothing about how to do it using PowerShell. Is there any documentation (or preferably code examples) out there to help with this?

There are a few items at issue here:
Are you sure that you really need a service? I agree with the comments about using the scheduler for running periodic tasks. The scheduler can start tasks as Administrator.
Services call a special set of APIs to communicate with the Service Control Manager, so an ordinary program can't be used directly.
The Service Control Manager uses CreateProcessAsUser to start the service process, so you need to point the SCM to an executable binary, as opposed to a script.
If you can't use the scheduler (though I strongly encourage you to try), I suppose that you could write an executable binary that acts as a service. It would then execute PowerShell and your script on your behalf. I'm thinking something like the srvany program that used to be included with the Windows Server 2003 Resource Kit could bootstrap the service for you.

If you absolutly want to execute your PowerShell code into a service, I think you can write a C# service wrapper for PowerShell code.
You found examples of how to create a service with C#, and it's simple to call Powershell from C#. So I'am surprised that as small SrvAny oriented PowerShell does not exists yet.
My advice here, you better rewrite your code in C# as a service.

You can use the Compile Script into Service option of PowerGUI: http://documents.software.dell.com/DOC144271

I think what you are looking for is SrvAny.exe from Microsoft (I don't know that they support it any longer, but it has been around in the Windows 2000 Resource Kit for years. It will essentially turn just about any executable (.exe, .cmd, etc...) into a service. I have VBScripts running as quasi-services using Srvany.exe and it works on Windows 2003 and Windows 2008 (even 64 bit). It is a little limited in that it does not provide 100% service functionality (stopping a running script may be difficult) but if you now a bit about programming and the Windows environment you can adapt it to work pretty well (i.e. it will launch the program after server Restart/Startup as you would likely need from a service. There is some documentation with SrvAny.exe (although it can be a little obtuse and difficult to find). It is however Free and built for Windows so it should work for you.
I agree for simplicity that the Scheduled Task idea is worth thinking about as it is even more simplistic that srvany.exe

http://www.firedaemon.com/
Will allows you start littery anything as a service. There are also free alternatives to this application, that does the same.

Related

REST Server without GUI

I've started working with delphi quite some while ago but I would say I'm still a newbie in all this.
So basically I tried creating REST Server, which can validate license keys. I got in working with Indy, but one thing bothers me. The GUI. The Server shouldn't have any kind of gui so it can work on any OS (Win, Linux, etc). Is there a way to make a REST Server without any GUI/FMX/VCL?
BTW: Working in Delphi 10.2.3 Professional.
Any advice is appreciated.
EDIT: I forgot to mention one thing: the server is supposed to run on an independent Data Center away from any user.
You can create the WebServer as a Windows Service.
You can use DelphiMVCFramework or any other Framework to create it.
With DMVC you can create console application, Windows Service, Linux daemon, Apache module (Windows and Linux) and IIS ISAPI (Windows).
With Intraweb you can also create Services.
Take a look at our mORMot Open Source REST framework, which works on Delphi but also on FPC/Lazarus.
FPC support ensures that you can target Linux with this free compiler. No need to upgrade to a newer version of Delphi Architect, which supports Linux, and is very pricey - and less stable (to my knowledge) since Linux support is quite new.
As you requested, the mORMot REST server has no UI part. You define your services as interface and class - like you do e.g. with DotNet - and you will have full JSON/REST support generated.
mORMot is used on production since years for very high performance and stability, hosted on both Windows and Linux. A version 2 is on its way, which would be even easier to use for new projects.
And you can create a Windows service or Linux daemon without using any third party framework. Delphi include everything you need. However, it is possible that third party framework will facilitate your programming. Don't forget you'll have to learn those third party framework.
Creating a Linux daemon service in Delphi
Creating a Windows Service in Delphi
In both cases, you can use the sample code you've found that make use of TIdTCPServer.

How do I prepare a program for deployment?

I'm a complete novice regarding software deployment. I don't have the infrastructure to experiment with servers and networks, and so I don't really know how software is deployed in enterprise.
Let's suppose I've created a very simple Windows .exe program. (You open it, and it shows a "Hello World" message box, together with a 'close' button). The program has no external dependencies and therefore is fully 'portable'.
Now let's suppose my program is wanted by a variety of different companies. They each wish to deploy my program across all of their Windows machines. Each company has some mechanism by which it can deploy software automatically to all of their client machines, but this mechanism may be different in each case. (Speaking as a complete novice, I don't really know what types of 'mechanisms' exist.)
What should I do to prepare my program for easy deployment?
Also, could anyone please describe what the most common deployment 'mechanisms' are? Thanks!
I've worked for both ISV's (12 years) and Enterprise IT departments (5 years) so I understand the nature of your question.
At a minimum, you need to create an installer that supports silent (non-interactive) installation, upgrading, and uninstall. You can technically do this using a wide variety of tools but your customers are going to prefer you create an MSI. They prefer this because Windows Installer (.MSI) provides a standardized mechanism with consistent command lines, logging, transactional installation (rollback changes on failure), is rich in metadata ( observable; no black box) and is transformable ( end user can modify the MSI using an onion skin approach to do things like change the name or location or existence of a shortcut, install a service using a specific username/password and so on ).
So as long as you are creating properly authored MSI's you can ignore the deployment method to a certain degree because you are abstracted from it. The trick is to understand the deployment requirements of your application ( easy in your example) and how to implement these requirements in Windows Installer (a somewhat steep learning curve).
I personally use InstallShield and Windows Installer XML (WiX). I also have written a tool called IsWiX (open source on CodePlex) that provides an InstallShield like authoring experience on top of WiX. The scenario you describe can easily be achieved using InstallShield LE (Limited Edition) which is free to Visual Studio customers. More complicated scenarios require more knowledge and advanced tools.

Is this a practical use of a service?

I need to run an application which processes hooks in background. The problem is that I have to start it automatically on systemstart as administrator. I've tried to use the windows task scheduler but on some systems it does not start the program as administrator which causes strange behaviours.
I've never written an windows service but would that be a solution to create windows service? And if yes: Does it start automatically as administrator on systemstart? How can i communicate with the service from an configuration program? And as last question: If I am right, I need to install a service through an installer like a msi-package. How would you create such a msi-package. Visual Studio 2012 does not support that anymore.
It would be very nice if someone could help me. Sorry for my bad English but I am giving my best. If you don't understand some parts of my question, just ask me.
1) Yes, this sounds a lot like you should write a service.
2) If you configure it so, by default they start with a special service account.
3) You can find a lot of good information about communicating with a service here: How to communicate with a windows service from an application that interacts with the desktop?
4) You don't have to. Do you use C# for example? You can then make use of a nice library called TopShelf to write your service.

What guarantees are there about the Windows Azure runtime components?

What's the minimal guaranteed list of functionality a Windows Azure instance will provide?
For example, I want to use Powershell from inside my Azure role. Currently all Windows Azure instances do have Powershell. What if Microsoft suddenly decides to stop deploying Powershell by default?
Is there a list of guaranteed components and functionality that I may assume unconditionally available inside Windows Azure instances?
There is no guarantee with respect to features currently available in Windows Azure. Obviously, Microsoft being the late-comer to the cloud game and an underdog, will not likely be cutting features out but only adding features in, but there is certainly no guarantee.
We do know that Azure instances are running Win2008 and you can lock them down to a certain version of a patch so that nothing is removed.
Interesting post, not sure if MSFT have a complete list of what's on offer (I do remember this being talked about in the early days when it went to general release), be nice to see a list. (*cough mark russinovich!)
Re: Powershell, its not going anywhere fast, its a core tenant of the Windows o/s and I could bet my house on it.
Startup tasks are your ideal preference to ensure you have what you need in Azure, for instance I install the Java JVM on the box (VM role) during a startup task, it's not there naively but it is when my code runs!
http://www.davidaiken.com/2011/01/19/running-azure-startup-tasks-as-a-real-user/

Is Make-Shell deprecated?

A couple of years ago I used Make-shell to create custom shells with custom cmdlets.
Later I used snap-in technique and I forgot about that option.
Now I should deploy a set of custom cmdlets to a big set of customers, and it would be better to provide a zero-configuration solution; hence, I remembered the custom shell solution and I'm wondering if it is still the best solution in such scenario. It would be helpful also because my cmdlets use WF 4.0, so that the shell should run under latest framework version.
Anyway, I'm a bit scared about support for it: is it still working with Powershell 2.0 functionalities?
Are there alternative ways to easily deploy cmdlets on customers' machines?
Thank you
In PowerShell V2 the alternative are modules.
You can write script modules, binaries modules, manifest modules. Simple to deploy. They are deployed by a simple directory copy. And theoretically may be shared on the Network in a path writen in the environement variable PSModulePath.
According to this StackOverflow entry PowerShell (the engine) runs fine under .NET 4.0.
Make-shell is no longer provided with PowerShell, you can find it if you install the Windows SDK. This article present it as deprecated. But I'am not able to find the information on Microsoft site.
Not sure why you would use makeshell when you can create your own PowerShell "shell" called a host. Have you seen Joel Bennet's PoshConsole which uses .NET 4?
http://poshconsole.codeplex.com/
That said if you really want to use makeshell, I have more recent blog post where I built a new sqlps host with it even
http://sev17.com/2010/05/the-truth-about-sqlps-and-powershell-v2/