Kaseya Community

Can't execute powershell command from Kaseya to another remote computer

This question is answered

I need a kaseya procedure that stop and starts a service, the thing is this is a patch management procedure and certain things need to started, stopped and restarted in a certain order. First the procedure needs to reboot server sql1, then reboot sql2, it then needs to reboot the app1 server, stop services on app2 server, reboot app2 server, then reboot app1 server, then finally reboot web1 server.

I want most if not all of this in one procedure, so I have been playing around with remote powershell commands, restarting servers and stopping services. These commands all work from 1 server, which is the web1 that needs to be restarted last, if the powershell window has been run as an admin. However, I cannot for the life of me figure out how to execute these commands through kaseya. 

I have tried: impersonate user, use credentials, going through shell command and executing powershell commands, executing as user in a 64 bit shell, nearly everything I've found in these forums. 

As an a jumping off point I have been trying to stop the audio service "AudioSrv" on the app2 server through web1 through kaseya. Here is the command i have been using and its variants:

ExecuteShellCommand() ->PowerShell.exe -Command 'stop-service -inputobject $(get-service -ComputerName 'vssvrts1' -Name 'AudioSrv')'

ExecutePowershell() -> stop-service -inputobject $(get-service -ComputerName 'vssvrts1' -Name 'AudioSrv')

In all of these cases the command execute and returns no warning however when the service is checked it's still running. 

Verified Answer
  • Why not use the built in commands to stop/start services and reboot that Kaseya has instead of using powershell commands?  When you use something executepowershellcommand, it will come back as a success because it is only checked to see if it sent the command. It would be tough to do this all in 1 procedure given you are trying to do things on multiple machines.  You could do something like have a main procedure that schedules reboot procedures on other machines along with doing your services.  

All Replies
  • Why not use the built in commands to stop/start services and reboot that Kaseya has instead of using powershell commands?  When you use something executepowershellcommand, it will come back as a success because it is only checked to see if it sent the command. It would be tough to do this all in 1 procedure given you are trying to do things on multiple machines.  You could do something like have a main procedure that schedules reboot procedures on other machines along with doing your services.  

  • I would do this one of two ways:

    1) Create a PowerShell script (ps1 file) and execute that. I think that is what you are trying to do, but I would group it all in a script vs. individual commands so you can add extra logic to confirm a server comes back up after reboot before proceeding for example.

    2) Other option would be multiple Agent Procedures that get scheduled on a timed delay and pause. If you want me to elaborate on that, let me know, but it doesn't sound like that is what you want.

    For the PowerShell script, we've had better luck using the ExecuteFile() function in the AP and using Hidden Start (hstart.exe) to lauch it:

    hstart.exe /NOCONSOLE /ELEVATED /WAIT /SILENT "%SYSTEMROOT%\System32\WindowsPowershell\v1.0\powershell.exe -nologo -executionpolicy bypass -noprofile -file <pathToPs1File>"

    I think the new version has a /NOUAC which might help. Even with UAC disabled I've had better luck using hstart than executing powershell.exe directly. I've never got ExecutePowershell() to work with a PS1 file, especially if arguments are required.

    Another option might be using Invoke-Command. Since Stop-Service doesn't have a -ComputerName parameter, I'd be leery of using it the way you are. Example usage:

    Invoke-Command -ComputerName 'vssvrts1' -ScriptBlock {Stop-Service -Name 'AudioSrv'}

    I've also seen using PSSession for remoting, but I've only got it to work in a live PowerShell session, not in a PS1 script. Can't remember why though, but I think it is a limitation of the cmdlets.

  • I can say that I've done similar things with simply using multiple Kaseya scripts chained together.  If you really have to wait to reboot the next server until the first server is all the way back  up then it takes a few additional measures into account, but it all works.  I'm going to simplify your example a little to just the two SQL servers for an example of something I've done in the past.

    Procedure #1 is scheduled on sql1, it has two lines:

    #1 - Schedule Procedure #2 to run on SQL1 in 2 minutes.  (Yes on sql1... This procedure is delayed by just enough to keep it from running *before* sql1 reboots..  So it will actually run as soon as sql1 comes back online.

    #2 - Reboot

    Procedure #2 runs on sql1 as soon as it starts checking back in.  It has one line.

    #1 - Schedule Procedure #3 to run on sql2 immediately.

    Procedure #3 is a simple reboot procedure that reboots sql2

    Expand on that as needed.  

    Can it get pretty messy?  Yes it can.

    Does it work? yes it does.

  • I had been looking for a way to somehow have a master procedure that just launches certain procedures on certain computers at a specified time, there's a way to do that through Kaseya scripting?

  • I think i'm going to cut my losses for right now and just do your second option, scheduling procedures on a timed delay that just happen to work with each other, it's ugly but it's what's needed right now.

  • So just to reiterate on what you're suggesting, procedure #1 has the ability to schedule other kaseya procedures on other machines? Or are you suggesting carefully timed procedures that run relative to each other? I think the carefully timed procedures is what i'm going to have to work with.

  • I just found the ScheduleProcedure() and executeprocedure() commands on the kaseya script list, I had no idea these existed. I apologize because now this question is irrelevant, however now I know that it is difficult to use kaseya to interface with a server which interfaces with other servers.

  • yeah, sorry I've been away from a computer since my post on here :).

    Using those commands is exactly what I had in mind.  Like I suggested in my post, if you *really* need something to wait until "server1" is back up before doing something to server2, then I would have the reboot procedure on server1 schedule a different procedure *on server1* for a minute or two out, and then that won't run until server1 is fully back online, and you then have that procedure schedule or execute your other procedure on server2.

    It gets a bit messy, but it really can work.  I used to do this pretty often to deal with

    1.) Patching on a hyperv server... shutdown all of the virtualized servers before patching. then bring them all back up after the hyperv reboots.

    2.) A couple of apps I had where when I reboot a SQL server these other servers had to be rebooted after the SQL was all the way back up in order to reset those apps.

  • I have many problems with executing powershell commands or scripts. Something it worked , sometimes it didn't work. There is no rule for it. On some machines it works as expected , on some it doesn't work even if it works when you run it locally. If anyone knows what exactly to do in order ps scripts always work with Kaseya  let us know.