Server Fault Asked by Pizza on December 13, 2020
I have a Windows server 2016 machine that needs to run an file that uses the cli, remotely using SSH.
It is known that logging out of an SSH connection terminates processes that were started by the ssh user. On linux machines, one may use solutions such as nohup
or screen
, resulting a process not being killed once logging off.
Such solutions won’t work on windows machines though, and I’m looking for a way to prevent a process from getting terminated once logged out.
Get-Process -Name proc_name
shows the process is indeed runningGet-Process
after SSH logout yields no such processThings I also tried:
.exe
file using cmd
..bat
and .ps
files that starts the .exe
with its arguments.Is there a way to tackles this problem on a windows machine?
After spending much time digging for solutions, I found the absolute correct way of doing so using PowerShell.
It seems like it is one of those cmdlets less used or documented online, and is almost impossible to figure out on your own using the MSDN documentation, unless you're already very familiar with the huge world of Wmi Objects
.
Assuming I have a program foo.exe
that needs to run on background with the arguments -a
, -b
and bar
, I should be using the exact command:
Invoke-WmiMethod -Path 'Win32_Process' -Name Create -ArgumentList 'C:UsersfooDesktopfoo.exe -a -b bar'
Of course if your exe is a "known" exe (either by default, such as notepad or ping) or added by you, a full path is not needed when using the -ArgumentList
and exe name would be sufficient (notepad, ping, and etc..).
Invoke-WmiMethod
supports additional parameters such as -Credential
, -ComputerName
(a good way to use a local PS to Invoke
something on a remote machine without using SSH) , -Impersonation
and many more documented here.
-Path
argument directs to a WmiObject
name. There are dozens of WmiObjects
, each with many Methods
and Properties
.-Name
argument directs the used Method
.Wmi-Objects
use Get-WmiObject -List
(very difficult to locate your needed object this way).Win32_Process
Methods
and Properties
useGet-WmiObject -List |where{$_.name -match '^Win32_Process$'}
This will return an output of this structure:
NameSpace: ROOTcimv2
Name Methods Properties
---- ------- ----------
Win32_Process {Create, Terminat... {Caption, CommandLine, CreationClassName, CreationDate...}
And of course using |Select-Object -ExpandProperty (_your_property_here_)
will reveal all available Methods
such as Create
we used in the command above, and all available Properties
(which we did not use).
Correct answer by Pizza on December 13, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP