currently I am writing a deployscript for our build system.
I have an ec2 machine that i want to push my installer to, install the program and then run the program (exe). The program is in fact a server that should run forever. I already tried doing this with Powershell Remoting, this is not satisfying because a permanent connection is needed (if the powershell remote session disconnects, the process terminates).
Now I am looking into Invoke-WMIObject, is this really the best way to go, or am I missing something obvious?
Requirement is that the whole build process can be run from a powershell script (which is executed by our build server).
The server is running windows 2008 Server R2
kind regards
You could run your server program as a Windows Service - just wrap it with one of available free service wrapper utilities like nssm. You can use PSRemoting to recreate and start the service on the remote server each build by running Stop-Service, & nssm remove, & nssm install and Start-Service.
Alternatively (though probably not so easily managed in your build), you could remotely create and run a task in Task Scheduler with Scheduled Tasks Cmdlets.
You can use PsExec from SysInternals, but that's a standalone utility. However, you can still embed the logic in your PS script. If you are still in need of WMI, then you might wanna check this. It includes snippets of VB scripts and a detailed explanation of what to do.
Related
Is there a way to install services remotely, without necessarily resorting to .msi packages or full-blown installers? I'm currently using a method similar to the one discussed here:
How to install a windows service programmatically in C#?
to install a service locally, and it works fine. However, I also need to be able to do the same thing remotely. I appreciate any insight.
If you can copy the service binary (the exe file) to the destination computer you can install the service exactly in the same way as you do this locally. The only distinguish is that during the usage of OpenSCManager function (see http://msdn.microsoft.com/en-us/library/ms684323.aspx) you should use destination computer as the first parameter (lpMachineName) and in CreateService (see http://msdn.microsoft.com/en-us/library/ms682450.aspx) as a lpBinaryPathName you should place the path to your service exe how it looks like on the remote computer.
You can use sc.exe utility to do the installation (type" sc create /?" in the command prompt to receive help). The remote installation of the service which you can do with sc.exe you can implement with native Windows API like I short explain above.
I have a windows server 2003 up in the internet.
But sometime I need to restart it.
After restart, I want one of the applications to run.
I want to do this all programatically.
I can now remotely restart the server.
But the question is how can I ask that piece of software to be executed (more precisely, I want to execute a .BAT file to ask a tomcat to run)?
Because I don't want to manually log in to the machine and start that application. That is time consuming. Is there any possible way, once the machine is started, my application will be run as well?
If you're developing an application that should always be running on the server, you probably need to implement it as a Windows service. For C#, see the classes in the System.ServiceProcess namespace -- you will need to inherit from ServiceBase.
Alternatively, you can set the program to be run as a scheduled task on boot. See the Task Scheduler API to do this.
You can install Cygwin and then do it the same way we'd do it on a Linux box: via ssh, using keys.
OpenSSH is not part of the default Cygwin install, so be sure to select it. It's in the Net category.
Then, after you've installed Cygwin and sshd, read /usr/share/doc/Cygwin/openssh.README to learn how to set up sshd as a service, so it will answer requests automatically, without you having to start the ssh daemon manually.
Finally, set up keys, as described in the link above.
Part of the ssh protocol is a way to ask a remote machine to launch a program. Setting it up with keys lets you do it without needing a password.
You could try xCmd, which is a freeware app to run a command on a remote machine.
I've a PS script which I use to keep track of my VMWare ESX servers. I need to run it as a service so that I'm not permanently logged on. Unfortunately, the script runs more slowly if I use a runspace inside a service rather than just running the script through the powershell console. It's taking 2-5 minutes to make calls to the VMWare web service instead of a second or so.
Is there some sort of magic I should be using when I invoke the runspace?
You can run the script as a scheduled task.
Without seeing what you're doing, I can't think of many things that would make that big of a difference:
Do you have profile customization that might be helping it out when it runs in PowerShell (test with PowerShell -noprofile)
Are you counting the time it takes to initially import a module or snapin in your service, but not in PowerShell.
Are you re-creating the whole runspace each time in your service?
Have you tried using the 2.0 API of PowerShell with PowerShell.Create() to run it?
Finally, you could also take a look at some of the open source PowerShell hosts like PoshConsole or bgHost on CodePlex ... if they don't run it slowly, then you could follow the process they use for creating their runspace.
Does the service authenticate as a user with the same rights you do? I suspect something is timing out when it runs as the service, which you don't see when you run the script yourself.
The script in question runs equally slowly whether it runs in a service using its
usual service account, or if I run as myself in a very basic console app (create
runspace, add script, execute).
Apart from the usual suspects (account rights, Set-ExecutionPolicy RemoteSigned, and firewall configuration) I can't think of a good reason why PowerShell shouldn't work exactly the same in a Windows Services as it does in a console application, which makes me think it might be something environmental. It's not something silly like poor DNS resolution, is it? Assuming you're running this on a server, does it also perform slowly on your development machine too?
PS: I wonder if this question might be more appropriate for ServerFault?
I've been trying to get psexec to run executables on remote machines from custom build tasks in visual studio. All of these commands work from the command line but running it from an application seems to be a problem. Some commands work, on other psexec hangs and consequently so do msbuild and visual studio 2005. I'm calling SharePoint's stsadm.exe in this case, but this problem occurs with a lot of programs, when running psexec from an application. There are lots of people having this problem, but there seems to be no solution, so my question is: Does anyone know a working alternative to psexec?
I've experienced 'hangs' when executing PSEXEC against a remote system, but I always attributed it to the security context in which the remote process was running under.
From PSEXEC help:
If you omit a username the remote
process runs in the same account from
which you execute PsExec, but because
the remote process is impersonating it
will not have access to network
resources on the remote system. When
you specify a username the remote
process executes in the account
specified, and will have access to any
network resources the account has
access to. Note that the password is
transmitted in clear text to the
remote system.
If your executing a process remotely, that then needs to access the database (stsadm.exe), then it could be failing trying to access the network resource, depending on how PSEXEC was executed. If thats the case, I'd imagine it would eventually time out and give some sort of resource unavailable message.
There are two things that generally done when executing deployment steps against a remote machine to prevent the behavior your describing:
Like rifferte mentioned, make sure all assets needed to
deploy are local to the remote
machine (copy files, etc) before
using PSEXEC to execute the script (*.bat, *.vbs, *.ps, etc) -
so that everything runs 'local' to
the remote machine.
Run PSEXEC using
a domain username/password when
executing it - note that this
information is passed in clear text
to the remote server.
There's always RCE.
RemCom is a small (10KB upx packed)
remoteshell / telnet replacement that
lets you execute processes on remote
windows systems, copy files on remote
systems, process there output and
stream it back. It allows execution of
remote shell commands directly with
full interactive console without
having to install any client software.
On local machines it is also able to
impersonate so can be used as a silent
replacement for Runas command.
You can try to have psexec call a bat program that executes what you need on the remote machine. I ran into this issue with installutil.exe. A simple bat file on the remote machine resolved it.
You should also share your experience on the sysinternals board. There may be something they can do in a future revision.
I have an application running only on Windows and a batch file that launches it.
I want to invoke this batch file from Linux, meaning something like Linux batch will launch the windows batch with parameters and this in its turn run my application.
Can I do that? How?
You could install an ssh server in the windows box (Cygwin has one), then from linux do something like:
ssh user#windows-box c:/path/to/batch.cmd
and that should launch your application in the windows box.
The most direct way is probably to install an ssh server on the windows box. Cygwin includes an ssh server.
Depending on how precise your timing needs are, you might be able to have an "at" job on the windows box that runs periodically (every 5 minutes?) and runs if it sees that a particular file exists, deleting the file. Then you could use Samba/smbclient to create the file. You would need to turn on filesharing on the windows box for this to work.
If the windows box has a web server, you could write a CGI, and trigger it using wget or cURL.
Our build process currently goes the other way: a windows sever kicks off things on the Linux server using plink (part of PuTTY). You might be able to set something similar up.
This may cause a security issue. Our information security person did not allow me to invoke any programs directly.
The safer way is to set up server on Windows computer. This can be a web-server for example. And then invoke your process inside PHP/Perl/Python script.
Also look at winexe that allows you to execute windows commands/batch scripts without running ssh server.